Java_JDBC及连接池
生活随笔
收集整理的這篇文章主要介紹了
Java_JDBC及连接池
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、JDBC介紹
jdbc是java操作數據庫的方式,本質是一套接口,由sun公司制定的規則,再由各個數據庫廠商去實現這個接口,我們成數據庫公司寫的這套實現類為驅動jar包
二、好處
學習一條API就能夠操作所有的關系型數據庫
三、基礎操作
其中executeUpdata()用來操作DML,返回值是數據庫受影響的行數;executeQuery()用來操作DQL(select語句)返回值是ResultSet集合,里面包含查詢到的數據。
使用prepareStatement:
setXxx的方法:只需要記住setObect(問號的編號, 問號的值)
編號從1開始
resultSet的操作:
Xxx表示列名的屬性。
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及连接池的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生命中不能拥有的
- 下一篇: python 爬虫——爬取百度文库VIP