目录
- public class PreparedStatementUpdateTest {
- @Test
- public void testInsert() {
- Connection connection = null;
- PreparedStatement ps = null;
-
- try {
- //1.获取链接
- connection = JDBCUtils.getConnection();
- //2.预编译sql语句,返回PreparedStatement实例
- String sql = "insert into customers(name,email,birth)
- value(?,?,?)";
- ps = connection.prepareStatement(sql);
- //3.填充占位符
- ps.setString(1,"哪吒");
- ps.setString(2,"nezha@gmail.com");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- java.util.Date parse = sdf.parse("2001-03-21");
- long time = parse.getTime();
- //这个Date是sql中的Date
- ps.setDate(3,new Date(time));
- //4.执行操作
- ps.execute();
- } catch (Exception e) {
- e.printStackTrace();
- }
- //5.资源释放
- JDBCUtils.closeConnection(connection,ps);
- }
- }
- public class CustomerForQuery {
- public Customer commonQueryForCustomer(String sql,Object ...obj){
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet resultSet = null;
- try {
- //1.获取连接
- conn = JDBCUtils.getConnection();
- //2.预编译sql语句,返回PreparedStatement实例
- ps = conn.prepareStatement(sql);
- //3.填充占位符
- for (int i = 0; i < obj.length; i++) {
- ps.setObject(i+1,obj[i]);
- }
- //4.执行查询操作
- resultSet = ps.executeQuery();
- //5.获取描述结果集的对象
- ResultSetMetaData metaData = resultSet.getMetaData();
- int columnCount = metaData.getColumnCount();
- //6.处理结果集
- if(resultSet.next()){
- Customer customer = new Customer();
- for (int i = 0; i < columnCount; i++) {
- Object object = resultSet.getObject(i + 1);
- String columnName = metaData.getColumnName(i + 1);
- Field declaredField =
- Customer.class.getDeclaredField(columnName);
- declaredField.setAccessible(true);
- declaredField.set(customer,object);
- }
- return customer;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- //7.关闭资源
- JDBCUtils.closeConnection(conn,ps,resultSet);
- }
- return null;
- }
- @Test
- public void test(){
- String sql = "select name,email from customers where id=?";
- Customer customer = commonQueryForCustomer(sql, 2);
- System.out.println(customer);
- }
- }
用到的bean类:
- public class Customer {
- private int id;
- private String name;
- private String email;
- private Date birth;
-
- public Customer() {
- }
-
- public Customer(int id, String name, String email, Date birth) {
- this.id = id;
- this.name = name;
- this.email = email;
- this.birth = birth;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public Date getBirth() {
- return birth;
- }
-
- public void setBirth(Date birth) {
- this.birth = birth;
- }
-
- @Override
- public String toString() {
- return "Customer{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", email='" + email + '\'' +
- ", birth=" + birth +
- '}';
- }
- }
● 查询需要调用PreparedStatement 的 executeQuery() 方法,查询结果是一个ResultSet 对象
● ResultSet 对象以逻辑表格的形式封装了执行数据库操作的结果集,ResultSet 接口由数据库厂商提供实现● ResultSet 返回的实际上就是一张数据表。有一个指针指向数据表的第一条记录的前面ResultSet 对象维护了一个指向当前数据行的游标,初始的时候,游标在第一行之前,可以通
过 ResultSet 对象 的 next() 方法移动到下一行。调用 next()方法检测下一行是否有效。若有
效,该方法返回 true,且指针下移。 相当于Iterator对象的 hasNext() 和 next() 方法的结合体。 当指针指向一行时, 可以通过调用 getXxx(int index) 或 getXxx(int columnName) 获取每
一列的值。 例如: getInt(1), getString("name")
● 注意:Java与数据库交互涉及到的相关Java API中的索引都从1开始
◆ 可用于获取关于ResultSet对象中列的类型和属性信息的对象
◆ 通过ResultSet的getMetaData()方法获取
◆ 常用方法
getColumnName(int column)方法,获取指定列的名称
getColumnLabel(int column)方法,获取指定列的别名,如果不指定别名则返回名称
getColumnCount()方法,返回当前 ResultSet 对象中的列数
● 释放ResultSet, Statement,Connection。
● 数据库连接(Connection)是非常稀有的资源,用完后必须马上释放,如果Connection不能及时正确的关闭将导致系统宕机
● Connection的使用原则是尽量晚创建,尽量早的释放。 可以在finally中关闭,保证及时其他代码出现异常,资源也一定能被关闭。