ORIGIN

JDBC DAO

JDBC 4 mins667 words

This is a note.

DAO: Data Access Object, Including just CRUD(Create, Read, Update, Delete).

Base DAO Code

This code is considered using transaction.

Common Update, Insert, Delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void update(Connection conn, String sql, Object... args) {
PreparedStatement ps = null;
try {
// 1. get connection to database
conn = JDBCUtils.getConnection();
// 2. pre-complile sql
ps = conn.prepareStatement(sql);
// 3. fill the placehoder
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 4. execute
ps.execute();
// 5.close resource
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(null, ps);
}
}

Common Select One element

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
public <T> T getInstance(Connection conn, Class<T> clazz, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}

rs = ps.executeQuery();
// get metadata
ResultSetMetaData metaData = rs.getMetaData();
// get number of colums from ResultSetMetaData
int columnCount = metaData.getColumnCount();
if (rs.next()) {
T t = clazz.newInstance(); // The clazz must have a constructor without parameters
for (int i = 0; i < columnCount; i++) {
// get column value
Object columnValue = rs.getObject(i + 1);
// get column name for each column
// column name: getColumnName();
// alias name: getColumnLabel();
String columnName = metaData.getColumnLabel (i + 1);

// using reflect to set values for the Class
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(null, ps, rs);
}
return null;
}

Common get multiple instances

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
public <T> List<T> queryForList(Connection conn, Class<T> clazz, String sql, Object... args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}

rs = ps.executeQuery();
// get metadata
ResultSetMetaData metaData = rs.getMetaData();
// get number of colums from ResultSetMetaData
int columnCount = metaData.getColumnCount();
// create Set object
ArrayList<T> list = new ArrayList<T>();
while (rs.next()) {
T t = clazz.newInstance(); // The clazz must have a constructor without parameters
for (int i = 0; i < columnCount; i++) {
// get column value
Object columnValue = rs.getObject(i + 1);
// get column name for each column
// column name: getColumnName();
// alias name: getColumnLabel();
String columnName = metaData.getColumnLabel (i + 1);

// using reflect to set values for the Class
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, columnValue);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(null, ps, rs);
}
return null;
}

Common search for special value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public <E> E getValue(Connection conn, String sql, Object ...args) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
for(int i = 0; i < args.length; i ++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
if(rs.next()) {
return (E) rs.getObject(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(null, ps, rs);
}
return null;
}

These are the base function, we won’t be creating object base on this, so the wrapper class should be abstract;

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
public abstract class baseDAO<T> {
private Class<T> clazz = null;

{
Type genericSuperclass = this.getClass().getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
paramType.getActualTypeArguments(); // get parent class generic
Type[] typeArguments = paramType.getActualTypeArguments();
clazz = (Class<T>) typeArguments[0];
}

public static void update(Connection conn, String sql, Object... args) {
// ...
}

public T getInstance(Connection conn, Class<T> clazz, String sql, Object... args) {
// ...
}

public List<T> queryForList(Connection conn, Class<T> clazz, String sql, Object... args) {
// ...
}

public <E> E getValue(Connection conn, String sql, Object ...args) {
// ...
}
}

Will provide specific DAO according to each specific table.

TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|