日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java_JDBC及连接池

發布時間:2024/1/1 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java_JDBC及连接池 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、JDBC介紹

jdbc是java操作數據庫的方式,本質是一套接口,由sun公司制定的規則,再由各個數據庫廠商去實現這個接口,我們成數據庫公司寫的這套實現類為驅動jar包

二、好處

學習一條API就能夠操作所有的關系型數據庫
三、基礎操作
其中executeUpdata()用來操作DML,返回值是數據庫受影響的行數;executeQuery()用來操作DQL(select語句)返回值是ResultSet集合,里面包含查詢到的數據。

使用prepareStatement:

setXxx的方法:只需要記住setObect(問號的編號, 問號的值)
編號從1開始

resultSet的操作:

  • resultSet.getXxx(String columnName)根據列名獲取列值
    Xxx表示列名的屬性。
  • boolean next():游標往下移動一行并返回該行是否有數據
    true有數據
    false沒有數據
  • executeUpdata的使用:

    //注冊驅動 //如果驅動是8.0版本以上的需要加上“cj”,5.0的不用加 Class.forName("com.mysql.cj.jdbc.Driver"; //建立鏈接 String url = "jdbc:mysql://localhost:3306/數據庫"; String username = "root"; String password = "root"; Connection connection = DriverManger.getConnection(url,username,password); //獲取執行者對象 String sql= "delete from emp where eid=?"; //使用prepareStatement可以用來防止sql注入 Statement pstmt = connection.preparStatement(sql); pstmt = setObect(問號的編號, 問號的值) //executeUpdate()返回數據庫受影響的行數 int row = pstmt.executeUpdate();

    executeQuery的使用:

    // 使用到了自己寫的工具類,免去了每次建立鏈接的煩惱Connection conn = JdbcUtile.getj().getConn();String sql = "select * from users1 where password = ?";PreparedStatement pstmt = conn.prepareStatement(sql);pstmt.setObject(1,"123");ResultSet resultSet = pstmt.executeQuery();while(resultSet.next()){int uid = resultSet.getInt("uid");String username = resultSet.getString("username");String password = resultSet.getString("password");String nickname = resultSet.getString("nickname");System.out.println(uid+" "+username+" "+password+" "+nickname);}resultSet.close();pstmt.close();conn.close();

    JdbcUtile工具類:

    /* * jdbc驅動工具類: * 省去jdbc的創建連接過程 * */ public class JdbcUtile {private static Connection conn;private JdbcUtile(){}private static JdbcUtile j = new JdbcUtile();public static JdbcUtile getj(){return j;}public Connection getConn(){return conn;}// 只需執行一次所以放進靜態代碼塊 static { // 獲取類加載器ClassLoader classLoader = JdbcUtile.class.getClassLoader(); //讀取properties的配置問價InputStream in = classLoader.getResourceAsStream("driver.properties");Properties properties = new Properties(); // 讀入properties集合中try {properties.load(in);} catch (IOException e) {e.printStackTrace();} // 獲取valueString url = properties.getProperty("url");String username = properties.getProperty("username");String password = properties.getProperty("password"); // 開啟驅動,獲得連接try {conn = DriverManager.getConnection(url, username, password);} catch (SQLException e) {e.printStackTrace();}} }

    properties配置文件:

    url=jdbc:mysql://localhost:3306/db4?serverTimezone=GMT%2B8&useSSL=false username=root password=root

    三、數據庫連接池

    解決頻繁創建鏈接和銷毀鏈接的煩惱節約時間提高效率

    配置連接池工具類DruidUtil:

    public class DruidUtil {private static DataSource ds;static {try { // 類加載器讀取配置文件Properties properties = new Properties();InputStream in = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties");properties.load(in); // 創建連接池ds = DruidDataSourceFactory.createDataSource(properties);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}} // 獲取連接public static Connection getConnection() throws SQLException {Connection connection = ds.getConnection();return connection;} // 釋放資源:DMLpublic static void close(Connection conn, Statement stmt){if (stmt!=null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn!= null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}} // 釋放資源:DQLpublic static void close(Connection conn, Statement stmt, ResultSet rs){if (rs!= null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt!=null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}} }

    連接池properties配置文件:

    url=jdbc:mysql://localhost:3306/homework?serverTimezone=GMT%2B8&useSSL=false username=root password=root driverClassName=com.mysql.cj.jdbc.Driver #初始化數據連接池 initialSize=5 #數據連接池中最多存在連接數量 maxActive=10 #最大存在時間 maxWait=3000

    測試類:

    public class DruidTest {@Testpublic void Druidtest() throws Exception{Connection connection = DruidUtil.getConnection();String sql = "select sid,sname,chinese from student where chinese > ?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setObject(1,"90");ResultSet rs = pstmt.executeQuery();while (rs.next()){int sid = rs.getInt("sid");String sname = rs.getString("sname");int chinese = rs.getInt("chinese");System.out.println("學號:"+sid+"姓名:"+sname+"語文成績:"+chinese);}DruidUtil.close(connection,pstmt,rs);}@Testpublic void DruidAdd() throws Exception{Connection connection = DruidUtil.getConnection();String sql = "insert into student(sid,sname,chinese) values(?,?,?)";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setObject(1,null);pstmt.setObject(2,"chen");pstmt.setObject(3,99);int i = pstmt.executeUpdate();if (i>0){System.out.println("插入成功");}else {System.out.println("插入失敗");}DruidUtil.close(connection,pstmt);}@Testpublic void DruidUpdate() throws Exception{Connection connection = DruidUtil.getConnection();String sql = "update student set sgender = ? where sname=?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setObject(1,"男");pstmt.setObject(2,"chen");int i = pstmt.executeUpdate();if (i>0){System.out.println("修改成功");}else {System.out.println("修改失敗");}}@Testpublic void DruidDelete() throws Exception{Connection connection = DruidUtil.getConnection();String sql = "delete from student where sname = ?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setObject(1,"chen");int i = pstmt.executeUpdate();if (i>0){System.out.println("刪除成功");}else{System.out.println("刪除失敗");}}

    總結

    以上是生活随笔為你收集整理的Java_JDBC及连接池的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。