Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82bb7d4

Browse files
author
JesusBetaX
committed
init 7
1 parent 90737ee commit 82bb7d4

File tree

2 files changed

+108
-63
lines changed

2 files changed

+108
-63
lines changed

‎src/com/jx/Demo.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public void select() throws SQLException {
2929
}
3030
}
3131
}
32+
33+
public int count() throws SQLException {
34+
try (DataBase db = DataBaseConfig.getDataBaseMySQL()) {
35+
return db.count("producto", null);
36+
}
37+
}
3238

3339
public int insert(Producto p) throws SQLException {
3440
try (DataBase db = DataBaseConfig.getDataBaseMySQL()) {
@@ -77,6 +83,7 @@ public static void main(String... args) throws SQLException {
7783
demo.delete(4);
7884

7985
demo.select();
86+
87+
System.out.println("count:" + demo.count());
8088
}
81-
8289
}

‎src/com/jx/db/DataBase.java‎

Lines changed: 100 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,92 @@ public PreparedStatement prepareStatement(String sql, int... i) throws SQLExcept
7272
return getConnection().prepareStatement(sql, i);
7373
}
7474

75+
public int executeInsert(String sql, Object... params) throws SQLException {
76+
try (PreparedStatement ps = prepareStatement(sql,
77+
Statement.RETURN_GENERATED_KEYS)) {
78+
for (int i = 0; i < params.length; i++) {
79+
ps.setObject(i + 1, params[i]);
80+
}
81+
if (ps.executeUpdate() == 1) {
82+
try (ResultSet rs = ps.getGeneratedKeys()) { //obtengo las ultimas llaves generadas
83+
if (rs.next()) {
84+
return rs.getInt(1);
85+
}
86+
}
87+
}
88+
return -1;
89+
}
90+
}
91+
92+
public int executeUpdate(String sql, Object... params) throws SQLException {
93+
try (PreparedStatement ps = prepareStatement(sql)) {
94+
for (int i = 0; i < params.length; i++) {
95+
ps.setObject(i + 1, params[i]);
96+
}
97+
return ps.executeUpdate();
98+
}
99+
}
100+
101+
/**
102+
* Ejecuta sentencias a la base de datos.
103+
*
104+
* @param sql sentencia a ejecutar
105+
* @param params [opcional] parametros del query
106+
*
107+
* @return @true resultado obtenido
108+
*
109+
* @throws SQLException
110+
*/
111+
public boolean execute(String sql, Object... params) throws SQLException {
112+
return executeUpdate(sql, params) == 1;
113+
}
114+
115+
/**
116+
* Ejecuta consultas a la base de datos.
117+
*
118+
* @param sql query a ejecutar
119+
* @param params [opcional] parametros del query
120+
*
121+
* @return ResultSet con el resultado obtenido
122+
*
123+
* @throws SQLException
124+
*/
125+
public ResultSet query(String sql, Object... params) throws SQLException {
126+
PreparedStatement ps = prepareStatement(sql);
127+
try {
128+
for (int i = 0; i < params.length; i++) {
129+
ps.setObject(i + 1, params[i]);
130+
}
131+
return ps.executeQuery();
132+
} finally {
133+
//ps.closeOnCompletion();
134+
}
135+
}
136+
137+
/**
138+
* Valida si un registro existe.
139+
*
140+
* @param tabla donde se buscaran las existencias
141+
* @param whereClause condicion
142+
* @param whereArgs parametros del whereClause
143+
*
144+
* @return numero de existencia
145+
*
146+
* @throws SQLException
147+
*/
148+
public int count(String tabla, String whereClause, Object... whereArgs) throws SQLException {
149+
String sql = "SELECT COUNT(*) AS COUNT FROM " + tabla;
150+
if (whereClause != null && !whereClause.isEmpty()) {
151+
sql += " WHERE " + whereClause;
152+
}
153+
try (ResultSet rs = query(sql, whereArgs)) {
154+
if (rs.next()) {
155+
return rs.getInt("COUNT");
156+
}
157+
}
158+
return -1;
159+
}
160+
75161
/**
76162
* Inserta un registro en la base de datos.
77163
*
@@ -149,71 +235,23 @@ public int update(String tabla, Map<String, Object> datos, String whereClause, O
149235
return executeUpdate(sql.toString(), bindArgs);
150236
}
151237

152-
public int delete(String tabla, String whereClause, Object... whereArgs) throws SQLException {
153-
String sql = "DELETE FROM " + tabla + " WHERE " + whereClause;
154-
return executeUpdate(sql, whereArgs);
155-
}
156-
157238
/**
158-
* Ejecuta consultas a la base de datos.
159-
*
160-
* @param sql query a ejecutar
161-
* @param params [opcional] parametros del query
162-
*
163-
* @return ResultSet con el resultado obtenido
164-
*
165-
* @throws SQLException
239+
* Elimina un registro de la base de datos.
240+
*
241+
* @param tabla donde se eliminara
242+
* @param whereClause condicion
243+
* @param whereArgs parametros del whereClause
244+
*
245+
* @return
246+
*
247+
* @throws SQLException
166248
*/
167-
public ResultSet query(String sql, Object... params) throws SQLException {
168-
PreparedStatement ps = prepareStatement(sql);
169-
try {
170-
for (int i = 0; i < params.length; i++) {
171-
ps.setObject(i + 1, params[i]);
172-
}
173-
return ps.executeQuery();
174-
} finally {
175-
//ps.closeOnCompletion();
176-
}
177-
}
178-
179-
public int executeInsert(String sql, Object... params) throws SQLException {
180-
try (PreparedStatement ps = prepareStatement(sql,
181-
Statement.RETURN_GENERATED_KEYS)) {
182-
for (int i = 0; i < params.length; i++) {
183-
ps.setObject(i + 1, params[i]);
184-
}
185-
if (ps.executeUpdate() == 1) {
186-
try (ResultSet rs = ps.getGeneratedKeys()) { //obtengo las ultimas llaves generadas
187-
if (rs.next()) {
188-
return rs.getInt(1);
189-
}
190-
}
191-
}
192-
return -1;
193-
}
194-
}
195-
196-
public int executeUpdate(String sql, Object... params) throws SQLException {
197-
try (PreparedStatement ps = prepareStatement(sql)) {
198-
for (int i = 0; i < params.length; i++) {
199-
ps.setObject(i + 1, params[i]);
200-
}
201-
return ps.executeUpdate();
249+
public int delete(String tabla, String whereClause, Object... whereArgs) throws SQLException {
250+
String sql = "DELETE FROM " + tabla;
251+
if (whereClause != null && !whereClause.isEmpty()) {
252+
sql += " WHERE " + whereClause;
202253
}
203-
}
204-
205-
/**
206-
* Ejecuta sentencias a la base de datos.
207-
*
208-
* @param sql sentencia a ejecutar
209-
* @param params [opcional] parametros del query
210-
*
211-
* @return @true resultado obtenido
212-
*
213-
* @throws SQLException
214-
*/
215-
public boolean execute(String sql, Object... params) throws SQLException {
216-
return executeUpdate(sql, params) == 1;
254+
return executeUpdate(sql, whereArgs);
217255
}
218256

219257
@Override

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /