Skip to content

Commit 4ad5845

Browse files
author
JesusBetaX
committed
init 15
1 parent f2ae81d commit 4ad5845

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

src/com/jx/library/Dao.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
*/
1919
public abstract class Dao<Model, Id> {
2020

21+
public static final Object[] EMPTY_ARRAY = new Object[0];
22+
23+
public static Object[] toArray(Object value)
24+
throws SQLException {
25+
if (value == null) {
26+
return EMPTY_ARRAY;
27+
}
28+
if (value instanceof Object[]) {
29+
return (Object[]) value;
30+
} else {
31+
return new Object[] { value };
32+
}
33+
}
34+
2135
/**
2236
* Busca un registro por su identificador
2337
*
@@ -29,7 +43,7 @@ public abstract class Dao<Model, Id> {
2943
*/
3044
public Model findById(Id id) throws SQLException {
3145
String sql = "SELECT * FROM " + getTableName() + " WHERE " + whereClause(id);
32-
return findById(sql, id);
46+
return findById(sql, toArray(id));
3347
}
3448

3549
/**
@@ -49,7 +63,7 @@ public Model findById(String sql, Object... params) throws SQLException {
4963
rs = db.query(sql, params);
5064
return rs.next() ? onRead(rs) : null;
5165
} finally {
52-
db.close(rs);
66+
DataBase.close(rs);
5367
}
5468
}
5569

@@ -91,7 +105,7 @@ public List<Model> find(String sql, Object... params) throws SQLException {
91105
return list;
92106

93107
} finally {
94-
db.close(rs);
108+
DataBase.close(rs);
95109
}
96110
}
97111

@@ -114,7 +128,7 @@ public long count() throws SQLException {
114128
* @throws SQLException
115129
*/
116130
public boolean exists(Id id) throws SQLException {
117-
return getDataBase().count(getTableName(), whereClause(id), id) > 0;
131+
return getDataBase().count(getTableName(), whereClause(id), toArray(id)) > 0;
118132
}
119133

120134
/**
@@ -146,7 +160,7 @@ public boolean update(Model m) throws SQLException {
146160
}
147161
Id id = getId(m);
148162
return getDataBase().update(getTableName(), values,
149-
whereClause(id), id) == 1;
163+
whereClause(id), toArray(id)) == 1;
150164
}
151165

152166
/**
@@ -192,7 +206,7 @@ public boolean delete(Model m) throws SQLException {
192206
*/
193207
public boolean deleteById(Id id) throws SQLException {
194208
return getDataBase().delete(getTableName(),
195-
whereClause(id), id) == 1;
209+
whereClause(id), toArray(id)) == 1;
196210
}
197211

198212
/**
@@ -289,6 +303,8 @@ protected Map<String, Object> onWrite(Model m) {
289303
* @param m modelo
290304
* @param id insertado
291305
*/
292-
protected abstract void insertId(Model m, long id);
306+
protected void insertId(Model m, long id) {
307+
308+
}
293309

294310
}

src/com/jx/library/DataBase.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.sql.SQLException;
88
import java.sql.Statement;
99
import java.util.Map;
10-
import javax.swing.JOptionPane;
1110

1211
/**
1312
*
@@ -337,7 +336,7 @@ protected void finalize() throws Throwable {
337336
}
338337
}
339338

340-
public void close(AutoCloseable ac) {
339+
public static void close(AutoCloseable ac) {
341340
try {
342341
if (ac != null) {
343342
ac.close();
@@ -347,30 +346,6 @@ public void close(AutoCloseable ac) {
347346
}
348347
}
349348

350-
public boolean log(SQLException e) {
351-
String msg = e.getMessage();
352-
if (e.getErrorCode() == 1049) {
353-
msg = "La base de datos: " + url + " no existe.";
354-
} else if (e.getErrorCode() == 1044) {
355-
msg = "El usuario: " + username + " no existe.";
356-
} else if (e.getErrorCode() == 1045) {
357-
msg = "Contraseña incorrecta.";
358-
} else if (e.getErrorCode() == 0) {
359-
msg = "La conexión con la base de datos no se puede realizar.\n Parece que el servidor de base de datos no esta activo.";
360-
}
361-
displayError(msg);
362-
return Boolean.FALSE;
363-
}
364-
365-
private void displayError(final String msg) {
366-
java.awt.EventQueue.invokeLater(new Runnable() {
367-
@Override
368-
public void run() {
369-
JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE);
370-
}
371-
});
372-
}
373-
374349
public String getDriverClassName() {
375350
return driverClassName;
376351
}

0 commit comments

Comments
 (0)