
Plasmar en la página web desde JSP los registros ya grabados de una BD de SQL Server (2da. parte)
Publicado por Percy (3 intervenciones) el 20/02/2016 07:44:06
Ya que no me permiten más de 15000 caracteres, esto es lo que sigue del mensaje anterior que he publicado
CODIFICACION de la clase Sql.java del PAQUETE=com.me.sql
del archivo NLib que es un Java Class Library:
CODIFICACION de la clase Sql.java del PAQUETE=com.me.sql
del archivo NLib que es un Java Class Library:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.me.sql;
import com.me.sql.ConectaDB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author alumno
*/
public class Sql {
static public String ejecuta(String sql) { // Insert, Delete y Update
String mensaje= null;
try {
ConectaDB db = new ConectaDB();
Connection cn = db.getConnection();
if (cn == null) {
mensaje = "No hay conexión a la base de datos!";
} else {
Statement st = cn.createStatement();
st.execute(sql);
cn.close();
}
} catch(SQLException e) {
mensaje= e.getMessage();
} catch(Exception e) {
mensaje= e.getMessage();
}
return mensaje; // returna null si Ok! sino mensaje de error
}
static public List consulta(String sql) { // Ejecuta Select
List<String[]> regs = new ArrayList<String[]>();
try {
ConectaDB db = new ConectaDB();
Connection cn = db.getConnection();
if (cn == null) {
regs = null;
} else {
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData rm = rs.getMetaData();
int numCols = rm.getColumnCount();
// Toma los títulos de las columnas
String[] titCols= new String[numCols];
for(int i=0; i<numCols; ++i) {
titCols[i]= rm.getColumnName(i+1);
}
// la fila 0 del vector lleva los títulos de las columnas
regs.add(titCols);
// toma las filas de la consulta
while(rs.next()) {
String[] reg= new String[numCols];
for(int i=0; i<numCols; i++) {
reg[i] = rs.getString(i + 1);
}
regs.add(reg);
}
cn.close();
}
} catch(SQLException e) {
regs= null;
} catch(Exception e) {
regs= null;
}
return regs; // returna null si falló
}
public static String consultaTXT(String sql) {
List qry = Sql.consulta(sql);
String data = "";
if(qry == null){
data = "No hay conexión o sentencias SELECT errónea";
} else {
for (int fil=0; fil<qry.size(); ++fil){
String[] fila = (String[]) qry.get(fil);
for (int col=0; col<fila.length; ++col){
data += fila[col] + "\t";
}
data += "\n";
}
}
return data;
}
//clase09 _java
//retornar columnas
public static String[] getColumna(String sql) {
//quiero tomar toda la columna
//debo retornar un arreglo de string
String[] columna = null; //reclarando el array
List qry = consulta(sql); ///ago una consulta y ago un lis
// qry dato tiene toda la data
if ( qry != null ){
int lenCol = qry.size() - 1; // el primer elemeto es el titulo
columna = new String[qry.size()]; //
//fil=1 ya no queremos el titulo
for(int fil=1; fil<=lenCol; ++fil){
String[] fila = (String[]) qry.get(fil); //tomo la primera fila
columna[fil -1] = fila[0]; // solo quiero la primera fila
}
}
return columna; // si retorna null fracasó
}
//// me retorna una fila
public static String[] getFila(String sql){
String[] fila = null;
List qry = consulta(sql);
if(qry != null){
if(qry.size() > 1){
fila = (String[]) qry.get(1);
}
}
return fila ;// si retorna null fracasó
}
public static String consultaHTML(String sql){
List qry = Sql.consulta(sql);
String tabla = "";
if( qry != null ){
tabla = "<table border='1' align ='center' width='90%'>";
for(int fil = 0 ; fil<qry.size(); ++fil){
tabla += "<tr>";
String[] fila = (String[]) qry.get(fil);
for(int col=0; col<fila.length; ++col){
tabla += "<td>" + fila[col] + "</td>";
}
tabla += "</tr>";
}
tabla += "</table>";
}else {
tabla = "SELECT mal hecho o no hay conexion!";
}
return tabla;
}
public static String comboHTML(String sql, String nameCombo){
List qry = Sql.consulta(sql);
String combo = "";
if (qry != null) {
combo = "<select name='"+ nameCombo +"'>";
for (int fil = 1; fil < qry.size(); ++fil) {
String[] fila = (String[]) qry.get(fil);
combo += "<option value='"+fila[0]+"'>"+fila[1]+"</option>";
}
combo += "</select>";
} else {
combo = "SELECT erróneo o no hay conexion!";
}
return combo;
}
// retorna un campo
public static String getCampo(String sql) {
String campo = null;
String[] fila = getFila(sql);
if(fila != null) {
campo = fila[0];
}
return campo;
}
}
Valora esta pregunta


0