mysql学习12( Statement对象 及 jdbc工具类 )

mysql学习12

  • Statement对象:

    • jdbc中的Statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可;

    • Statement对象的executeUpdate方法,用于向数据库发送增,删,改的SQL语句,executeUpdate执行完后,将会返回一个整数(即数据库执行的行数);

    • Statement.executeQuery()方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象;

       

 

  • 代码实现:

    • 提取工具类:

      • 工具类:


        import java.io.InputStream;
        import java.sql.*;
        import java.util.Properties;

        /**
        * JDBC工具类
        */
        public class JdbcUtils {

           private static String driver=null;
           private static String url=null;
           private static String username=null;
           private static String password=null;

           static {
               try {
                   InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
                   Properties properties = new Properties();
                   properties.load(in);

                   driver=properties.getProperty("driver");
                   url=properties.getProperty("url");
                   username=properties.getProperty("username");
                   password=properties.getProperty("password");

                   //1,驱动只用加载一次,
                   Class.forName(driver);

              } catch (Exception e) {
                   e.printStackTrace();
              }
          }

           //获取连接
           public static Connection getConnection() throws SQLException{
               Connection connection = DriverManager.getConnection(url, username, password);
               return connection;
          }


           //释放连接
           public static void release(Connection conn, Statement state, ResultSet res) {

              if(res !=null){
                  try {
                      res.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }

               if(state !=null){
                   try {
                       state.close();
                  } catch (SQLException e) {
                       e.printStackTrace();
                  }
              }

               if(conn !=null){
                   try {
                       conn.close();
                  } catch (SQLException e) {
                       e.printStackTrace();
                  }
              }

          }


        }

      • 配置文件:

        driver=com.mysql.jdbc.Driver
        url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true
        username=root
        password=root

         

    • 编写增删改测试类:

      • 增,删,改,都是一样的:executeUpdate


        import java.sql.Connection;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.sql.Statement;

        /**
        * 测试工具类
        */
        public class JdbcDemo04 {
           public static void main(String[] args) {


               Connection conn =null;
               Statement st=null;
               ResultSet rs=null;

               try {
                   conn = JdbcUtils.getConnection();//获取连接
                   st=conn.createStatement();//创建SQL执行对象
                   String sql="UPDATE `users` SET `NAME`='demmo' WHERE id=4";
                   int i=st.executeUpdate(sql);

                   if(i>0){
                       System.out.println("更新成功");
                  }


              } catch (SQLException e) {
                   e.printStackTrace();
              }finally {
                   JdbcUtils.release(conn,st,rs);
              }

          }
        }

         

    • 编写查询测试类:

      • 查询测试:executeQuery

        import java.sql.Connection;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.sql.Statement;

        /**
        * 测试工具类
        */
        public class JdbcDemo05 {
           public static void main(String[] args) {

               Connection conn =null;
               Statement st=null;
               ResultSet rs=null;

               try {
                   conn = JdbcUtils.getConnection();//获取连接
                   st=conn.createStatement();//创建SQL执行对象
                   String sql="SELECT * FROM `users` ";
                   rs=st.executeQuery(sql);

                   while (rs.next()){
                       System.out.println("id="+rs.getObject("id"));
                       System.out.println("name="+rs.getObject("NAME"));
                  }


              } catch (SQLException e) {
                   e.printStackTrace();
              }finally {
                   JdbcUtils.release(conn,st,rs);
              }

          }
        }

 

 

 

 

 

posted @   gzs1024  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示