【转载】使用注解和反射实现通用性…


接着上篇: 使用注解和反射实现通用性的 jdbc操作数据库之最简单的 jdbc 操作_1

3,得到connection 之后就可以使用它来获取PreparedStatement,然后执行 SQL 语句,请看下面的代码;

public class CoreDao {

 

      // 定义数据库的链接

      private Connection conn;

      // 定义sql语句的执行对象

      private PreparedStatement pstmt;

      // 定义查询返回的结果集合

      private ResultSet resultSet;

 

 

      public void list() {

            conn = DBConnUtils.getConnection();

            String sql = "select * from user";

            try {

                  //  预编译 SQL 语句

                  pstmt conn.prepareStatement(sql);

                  //  执行查询

                  resultSet pstmt.executeQuery();

                  while (resultSet.next()) {

                        System.out.println("username = " resultSet.getString("username"

                                    ", birth = " resultSet.getDate("birth"

                                    ", detail_time = " resultSet.getDate("detail_time"));

                  }

            catch (SQLException e) {

                  e.printStackTrace();

            }

      }

    

      

      public static void main(String[] args) {

            new CoreDao().list();

      }

}

运行 main 方法,看到如下打印信息;

【转载】使用注解和反射实现通用性的 <wbr>jdbc操作数据库之最简单的 <wbr>jdbc <wbr>操作_2

可以看到代码正确获取到数据库的数据库;

 

4关闭数据库链接释放资源

         有经验的开发者一眼就能看出上述代码的问题了!!!不知道你是否也发现了呢?代码并没有关闭 connection 释放资源。如果不释放并且随着访问量的增加 connection 会越来越多,导致服务器内存被占满,出现内存溢出问题!!所以记得一定要关闭数据库连接。

public void list() {

            conn = DBConnUtils.getConnection();

            String sql = "select * from user";

            try {

                  //…… 代码同上,不在列出

            catch (SQLException e) {

                  e.printStackTrace();

            finally  //关闭数据库连接释放资源

                  try {

                     if (null != pstmt) {

                          pstmt.close();

                     }

               catch (SQLException e) {

                     e.printStackTrace();

               }

               try {

                     if (null != pstmt) {

                          conn.close();

                     }

               catch (SQLException e) {

                     e.printStackTrace();

               }

            }

      }

 

最后给出完成的增上改查的代码示例;

package com.ubuntuvim.core;


 

public class CoreDao {

 

      // 定义数据库的链接

      private Connection conn;

      // 定义sql语句的执行对象

      private PreparedStatement pstmt;

      // 定义查询返回的结果集合

      private ResultSet resultSet;

      

      private int commId = 35;

 

      

      //  新增数据

      @Test

      public void add() {

            conn = DBConnUtils.getConnection();

            String sql = " insert into user(id, username, birth) values(?, ?, ?)";

            try {

                  //  预编译 SQL 语句

                  pstmt conn.prepareStatement(sql);

                  pstmt.setInt(1, commId);  //为了方便测试直接指定了 id,实际使用中通常不需要指定 id,默认设置为自增类型即可

                  pstmt.setString(2, "新增测试");  //1表示 SQL 语句中的第一个问号

                  pstmt.setDate(3, new Date(System.currentTimeMillis()));

                  //  执行SQL

                  int i = pstmt.executeUpdate();

                  // 提交事务

                  conn.commit();

                  if (i > 0)

                        System.out.println(新增成功...");

            catch (SQLException e) {

                  e.printStackTrace();

                  // 出错回滚事务

                  try {

                        conn.rollback();

                  catch (SQLException e1) {

                        e1.printStackTrace();

                  }

            finally  //关闭数据库连接释放资源

                  try {

                        if (null != pstmt) {

                              pstmt.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

                  try {

                        if (null != pstmt) {

                              conn.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

            }

      }

 

      //  测试查询

      public void list() {

            

            conn = DBConnUtils.getConnection();

            String sql = "select * from user";

            try {

                  //  预编译 SQL 语句

                  pstmt conn.prepareStatement(sql);

                  //  执行查询

                  resultSet pstmt.executeQuery();

                  while (resultSet.next()) {

                        

                        System.out.println("id = " resultSet.getInt("id") + ", username = " resultSet.getString("username"

                                    ", birth = " resultSet.getDate("birth"

                                    ", detail_time = " resultSet.getObject("detail_time"));

                  }

            catch (SQLException e) {

                  e.printStackTrace();

            finally  //关闭数据库连接释放资源

                  try {

                        if (null != pstmt) {

                              pstmt.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

                  try {

                        if (null != pstmt) {

                              conn.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

            }

      }

      

      //  测试更新

      public void update() {

            conn = DBConnUtils.getConnection();

            String sql = " update user set username = ? where id = ?";

            try {

                  //  预编译 SQL 语句

                  pstmt conn.prepareStatement(sql);

                  pstmt.setString(1, "update_test"); //

                  pstmt.setInt(2, commId);  //

                  //  执行更新

                  int i = pstmt.executeUpdate();

                  if (i > 0)

                        System.out.println(更新成功...");

                  

                  //记得要提交

                  conn.commit();

            catch (SQLException e) {

                  e.printStackTrace();

                  //  如果更新出错,执行回滚

                  try {

                        conn.rollback();

                  catch (SQLException e1) {

                        e1.printStackTrace();

                  }

            finally  //关闭数据库连接释放资源

                  try {

                        if (null != pstmt) {

                              pstmt.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

                  try {

                        if (null != pstmt) {

                              conn.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

            }

      }

      

      //  测试删除,删除的代码与更新的代码基本是一样的,

      //  只是 SQL 的不同而已,所以可以把更新和删除合并为一个方法,

      //  会在后续的文章实现合并

      public void del() {

            conn = DBConnUtils.getConnection();

            String sql =  delete from user where id = ?";

            try {

                  //  预编译 SQL 语句

                  pstmt conn.prepareStatement(sql);

                  pstmt.setInt(1, commId);  //删除 id 35的数据

                  //  执行更新

                  int i = pstmt.executeUpdate();

                  if (i > 0)

                        System.out.println(删除成功...");

                  

                  //记得要提交

                  conn.commit();

            catch (SQLException e) {

                  e.printStackTrace();

                  //  如果更新出错,执行回滚

                  try {

                        conn.rollback();

                  catch (SQLException e1) {

                        e1.printStackTrace();

                  }

            finally  //关闭数据库连接释放资源

                  try {

                        if (null != pstmt) {

                              pstmt.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

                  try {

                        if (null != pstmt) {

                              conn.close();

                        }

                  catch (SQLException e) {

                        e.printStackTrace();

                  }

            }

      }

    

      

      public static void main(String[] args) {

            new CoreDao().list();

            new CoreDao().add();

            new CoreDao().list();

            new CoreDao().update();

            new CoreDao().list();

            new CoreDao().del();

            new CoreDao().list();

      }

}

 

 【转载】使用注解和反射实现通用性的 <wbr>jdbc操作数据库之最简单的 <wbr>jdbc <wbr>操作_2

到此相信你已经学会最简单的 jdbc 操作数据了。对数据库的操作基本上都是在增删改查的基本上添加复杂的业务逻辑。当然,使用 jdbc 也可调用存储过程,但是本系列文章暂时不讨论,本系列文章主要讲讲怎么从简单的 jdbc 一步步提升为基于注解的通用性 jdbc 操作。

      最终想达到的目标是:

1.   查询能返回 list  model对象数组

2.   新增方法只需要传入 model对象即可

3.   不需要手动一个个写 getter/setter,sptmt.setXxx()

4.   怎么使用 java 注解并解析定义的注解(仿照 java 的 Persistence 类)

如果需要完成的项目请从我的 github 空间上下载,网址为:https://github.com/ubuntuvim/jdbc_db_converstion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值