用jdbc查询数据库表中的数据
自定义的连接数据库的工具类
1 package com.xing3.util; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 import java.util.Properties; 10 11 /** 12 * 13 * @Description 操作数据库的工具类 14 * @author shkstart Email:shkstart@126.com 15 * @version 16 * @date 上午9:10:02 17 * 18 */ 19 public class JDBCUtils { 20 21 /** 22 * 23 * @Description 获取数据库的连接 24 * @author shkstart 25 * @date 上午9:11:23 26 * @return 27 * @throws Exception 28 */ 29 public static Connection getConnection() throws Exception { 30 // 1.读取配置文件中的4个基本信息 31 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); 32 33 Properties pros = new Properties(); 34 pros.load(is); 35 36 String user = pros.getProperty("user"); 37 String password = pros.getProperty("password"); 38 String url = pros.getProperty("url"); 39 String driverClass = pros.getProperty("driverClass"); 40 41 // 2.加载驱动 42 Class.forName(driverClass); 43 44 // 3.获取连接 45 Connection conn = DriverManager.getConnection(url, user, password); 46 return conn; 47 } 48 /** 49 * 50 * @Description 关闭连接和Statement的操作 51 * @author shkstart 52 * @date 上午9:12:40 53 * @param conn 54 * @param ps 55 */ 56 public static void closeResource(Connection conn,Statement ps){ 57 try { 58 if(ps != null) 59 ps.close(); 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } 63 try { 64 if(conn != null) 65 conn.close(); 66 } catch (SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 /** 71 * 72 * @Description 关闭资源操作 73 * @author shkstart 74 * @date 上午10:21:15 75 * @param conn 76 * @param ps 77 * @param rs 78 */ 79 public static void closeResource(Connection conn,Statement ps,ResultSet rs){ 80 try { 81 if(ps != null) 82 ps.close(); 83 } catch (SQLException e) { 84 e.printStackTrace(); 85 } 86 try { 87 if(conn != null) 88 conn.close(); 89 } catch (SQLException e) { 90 e.printStackTrace(); 91 } 92 try { 93 if(rs != null) 94 rs.close(); 95 } catch (SQLException e) { 96 e.printStackTrace(); 97 } 98 } 99 }
表数据为:

将一个表封装成对象
1 package com.xing3.bean; 2 3 /* 4 ORM编程思想(object relational mapping) 5 一个数据表对应一个java类 6 表中的一条记录对应java类的一个对象 7 表中的一个字段对应java类的一个属性 8 */ 9 10 import java.sql.Date; 11 12 public class Customer { 13 private int id; 14 private String name; 15 private String email; 16 private Date birth; 17 18 public Customer() { 19 } 20 21 public Customer(int id, String name, String email, Date birth) { 22 this.id = id; 23 this.name = name; 24 this.email = email; 25 this.birth = birth; 26 } 27 28 public int getId() { 29 return id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public String getEmail() { 37 return email; 38 } 39 40 public Date getBirth() { 41 return birth; 42 } 43 44 @Override 45 public String toString() { 46 return "Customer{" + 47 "id=" + id + 48 ", name='" + name + '\'' + 49 ", email='" + email + '\'' + 50 ", birth=" + birth + 51 '}'; 52 } 53 }
具体查询实现
package com.xing3.preparedstatement.curd; import com.xing3.bean.Customer; import com.xing3.util.JDBCUtils; import org.junit.Test; import java.lang.reflect.Field; import java.sql.*; public class CustomerForQuery { @Test public void queryForCustomersTest(){ String sql = "select id,name,birth,email from customers where id = ?"; Customer customer = queryForCustomers(sql, 13); System.out.println(customer); sql = "select id,email from customers where name = ?"; Customer customer1 = queryForCustomers(sql, "周杰伦"); System.out.println(customer1); } //针对与customers表的同同查询操作 public Customer queryForCustomers(String sql,Object...args){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); ps = conn.prepareStatement(sql); for(int i = 0;i < args.length;i++){ ps.setObject(i + 1, args[i]); } rs = ps.executeQuery(); //获取结果集的元数据 :ResultSetMetaData ResultSetMetaData rsmd = rs.getMetaData(); //通过ResultSetMetaData获取结果集中的列数 int columnCount = rsmd.getColumnCount(); if(rs.next()){ Customer cust = new Customer(); //处理结果集一行数据中的每一个列 for(int i = 0;i <columnCount;i++){ //获取列值 Object columValue = rs.getObject(i + 1); //获取每个列的列名 // String columnName = rsmd.getColumnName(i + 1); String columnLabel = rsmd.getColumnLabel(i + 1); //给cust对象指定的columnName属性,赋值为columValue:通过反射 Field field = Customer.class.getDeclaredField(columnLabel); field.setAccessible(true); field.set(cust, columValue); } return cust; } } catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtils.closeResource(conn, ps, rs); } return null; } @Test public void testQuery1() { Connection conn = null; PreparedStatement ps = null; ResultSet resultSet = null; try { //获取连接 conn = JDBCUtils.getConnection(); String sql = "select id,name,email,birth from customers where id = ?"; //预编译SQL语句 ps = conn.prepareStatement(sql); ps.setObject(1,1); //执行 resultSet = ps.executeQuery(); //处理结果集 if (resultSet.next()) {//判断结果集的下一条是否有数据,如果有数据返回true,指针并下移,如果没有数据返回false,指针不动 //获取当前这条数据的各个字段值 int id = resultSet.getInt(1); String name = resultSet.getString(2); String email = resultSet.getString(3); Date date = resultSet.getDate(4); //方式一 // System.out.println("id = " + id + ",name = " + name + ",email = " + email + ",date = " + date); //方式二 // Object data = new Object[]{id,name,email,date}; //方式三 //将数据封装成一个对象(推荐) Customer customer = new Customer(id, name, email, date); System.out.println(customer); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn,ps,resultSet); } } }

浙公网安备 33010602011771号