I:JDBC对DB一张表的专用的查询一条数据的操作
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
- 调用 executeQuery() 方法,获取了封装结果集的 ResultSet 接口的实现类对象
- 调用 next() 方法,检测下一行是否有效,若有效该方法返回 true,且指针下移
- 指针下移所指向的那一行,通过调用 getXxx(int index) 或 getXxx(int columnName) 获取每一列的值
- 每一列的值应使用一个 Java 类的对象一一对应的属性装配,此处体现了 ORM 编程的思想
public void testQuery() throws Exception {
Connection conn = JDBCUtils.getConnection();
String sql = "select id,name,email,birth from customers where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, 8);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String email = rs.getString(3);
Date birth = rs.getDate(4);
Customer cust = new Customer(id, name, email, birth);
System.out.println(cust);
}
JDBCUtils.closeResource(conn, ps, rs);
}
II:JDBC对DB一张表的专用的查询多条数据的操作
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
public void testQueryAll() throws Exception {
Connection conn = JDBCUtils.getConnection();
String sql = "select id,name,email,birth from customers where id < ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, 30);
ResultSet rs = ps.executeQuery();
List<Customer> list = new ArrayList<Customer>();
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String email = rs.getString(3);
Date birth = rs.getDate(4);
Customer cust = new Customer(id, name, email, birth);
list.add(cust);
}
JDBCUtils.closeResource(conn, ps, rs);
list.forEach(System.out::println);
}
III:JDBC对DB一张表的通用的查询一条数据的操作
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
- 通过结果集 rs 调用 getMetaData() 获取结果集的元数据
- 通过结果集的元数据 rsmd 调用 getColumnCount() 获取结果集的列数
- 使用循环,循环的次数即为列的个数,然后获取每一列的值,注意索引仍从 1 开始
- 再使用 rsmd 调用 getColumnLabel(index i) 方法获取每一列的列名
- 给 cust 对象指定的 columnLabel 属性,赋值为 columnValue,通过反射机制
public Customer universalForData(String sql, Object... args) throws Exception {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
Customer cust = new Customer();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String ColumnLabel = rsmd.getColumnLabel(i + 1);
Field field = Customer.class.getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(cust, columnValue);
}
return cust;
}
JDBCUtils.closeResource(conn, ps, rs);
return null;
}
IV:JDBC对DB一张表的通用的查询多条数据的操作
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
public List<Customer> universalForAllData(String sql, Object... args) throws Exception {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
List<Customer> list = new ArrayList<Customer>();
while (rs.next()) {
Customer cust = new Customer();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String ColumnLabel = rsmd.getColumnLabel(i + 1);
Field field = Customer.class.getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(cust, columnValue);
}
list.add(cust);
}
JDBCUtils.closeResource(conn, ps, rs);
return list;
}
V:JDBC对DB所有表的通用的查询一条数据的操作
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
public Object universalForTable(Class<?> clazz, String sql, Object... args) throws Exception {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
Object ObjInstance = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String ColumnLabel = rsmd.getColumnLabel(i + 1);
Field field = clazz.getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(ObjInstance, columnValue);
}
return ObjInstance;
}
JDBCUtils.closeResource(conn, ps, rs);
return null;
}
VI:JDBC对DB所有表的通用的查询一条数据的操作升级为泛型方法
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
public <T> T universalForTable(Class<T> clazz, String sql, Object... args) throws Exception {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
if (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String ColumnLabel = rsmd.getColumnLabel(i + 1);
Field field = clazz.getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
return t;
}
JDBCUtils.closeResource(conn, ps, rs);
return null;
}
VII:JDBC对DB所有表的通用的查询多条数据的操作
为简洁查看,这里使用 throws 解决异常,若在使用中,以下代码的异常应该使用 try-catch-finally 处理
public <T> List<T> universalForAllTable(Class<T> clazz, String sql, Object... args) throws Exception {
Connection conn = JDBCUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
List<T> list = new ArrayList<T>();
while (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1);
String ColumnLabel = rsmd.getColumnLabel(i + 1);
Field field = clazz.getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
list.add(t);
}
JDBCUtils.closeResource(conn, ps, rs);
return list;
}
备注:JDBC中所使用的技术和思想有哪些?
- 关于 ORM 的编程思想
- 一个 数据表 与一个 Java类 对应
- 表中的一个 列 与Java类的一个 属性 对应
- 表中的一个 行 与Java类的一个 对象 对应
- 关于元数据 ResultSetMetaData 接口
- 可以获取对应的 ResultSet 有多少列,每一列的列名都是什么
- 如何获取 ResultSetMetaData 接口的实现类: 调用 ResultSet 的 getMetaData() 方法
- 获取 ResultSet 中有多少列:调用 ResultSetMetaData 的 getColumnCount() 方法
- 获取 ResultSet 每一列的列的别名是什么:getColumnLabel() 方法