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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java连接mysql数据库连接池_java使用原生MySQL实现数据的增删改查以及数据库连接池技术...

發布時間:2023/12/4 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java连接mysql数据库连接池_java使用原生MySQL实现数据的增删改查以及数据库连接池技术... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、工具類及配置文件準備工作

1.1 引入jar包

使用原生MySQL,只需要用到MySQL連接的jar包,maven引用方式如下:

mysql

mysql-connector-java

5.1.48

1.2 jdbc.properties文件配置

在resources文件夾根目錄,新增jdbc.properties配置文件,內容如下:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/mydb

user=root

password=123456

1.3 JDBCUtils工具類

在java文件夾中新增 util --> JDBCUtils.java 類,該類中獲取jdbc.properties中的值。

JDBCUtils工具類主要作用是簡化獲取MySQL配置文件、關閉資源。

private staticString url;private staticString user;private staticString password;static{

Properties properties= newProperties();try{

properties.load(Mytest.class.getClassLoader().getResourceAsStream("jdbc.properties"));

url= properties.getProperty("url");

user= properties.getProperty("user");

password= properties.getProperty("password");

Class.forName(properties.getProperty("driver"));

}catch (IOException |ClassNotFoundException e) {

e.printStackTrace();

}

}//1.獲取jdbc.properties配置文件中的數據庫連接

public static Connection getConnection() throwsSQLException {returnDriverManager.getConnection(url, user, password);

}//5.定義關閉資源的方法

public static voidclose(Connection conn, Statement stmt, ResultSet rs) {if (rs != null) {try{

rs.close();

}catch(SQLException e) {}

}if (stmt != null) {try{

stmt.close();

}catch(SQLException e) {}

}if (conn != null) {try{

conn.close();

}catch(SQLException e) {}

}

}public static voidclose(Connection conn, Statement stmt) {

close(conn, stmt,null);

}

二、原生MySQL實現增刪改查

2.1 語法說明

1、通過Connection獲取數據庫連接對象;

2、定義sql語句(一般可以在Navicat中直接執行);

3、通過獲取執行sql的對象 --PreparedStatement;

4、執行sql語句:增刪改使用conn的executeUpdate方法(成功返回值為int=1),查詢使用executeQuery方法(返回值為ResultSet,建議使用下文的查詢方法操作);

5、釋放資源(執行SQL時定義的stmt、獲取連接時的conn)。

2.2 新增數據 -- insertUser()

在java文件夾中新增MyService.java類,將獲取數據庫連接抽取出來,方法如下:

privateConnection conn;

{try{

conn=JDBCUtils.getConnection();

}catch(SQLException e) {

e.printStackTrace();

}

}

在MyService.java類中新增 insertUser方法,具體如下

String sql = "INSERT INTO user values (4, '騰訊科技', 'xinfeng37812', '2009-11-16', '廣東省深圳市')";

PreparedStatement stmt=conn.prepareStatement(sql);int count =stmt.executeUpdate(sql);

JDBCUtils.close(conn, stmt);return count;

2.2 修改數據 -- updateById()

String sql = "update user set password = 567875 where id = 2";

PreparedStatement stmt=conn.prepareStatement(sql);int count =stmt.executeUpdate(sql);

JDBCUtils.close(conn, stmt);return count;

2.3 刪除數據 -- deleteUser()

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

PreparedStatement stmt=conn.prepareStatement(sql);int count =stmt.executeUpdate(sql);

JDBCUtils.close(conn, stmt);return count;

2.4 查詢數據 -- findAll()

前提:新建 entity --> User.java 實體類,并獲取getter&setter、toSting方法;

String sql = "select * from user";

PreparedStatement stmt=conn.prepareStatement(sql);

ResultSet count=stmt.executeQuery(sql);

User user= null;

List arr = new ArrayList<>();while(count.next()){

Long id= count.getLong("id");

String username= count.getString("username");

String password= count.getString("password");

Date birthday= count.getDate("birthday");

String address= count.getString("address");

user= newUser();

user.setId(id);

user.setUsername(username);

user.setPassword(password);

user.setBirthday(birthday);

user.setAddress(address);

arr.add(user);

}

JDBCUtils.close(conn, stmt, count);return arr;

三、原生MySQL語句的缺點及數據庫連接池

3.1 原生MySQL語句的缺點

1、每一次查詢都要新增通道,關閉通道,效率太低。實際項目中都會用數據庫連接池進行優化;

2、實際項目中使用最多的就是查詢,但是將查詢的ResultSet結果,進行封裝的代碼過于臃腫。

3.2 c3p0和druid連接池技術

數據庫連接池其實就是一個容器,在java中,使用getConnection方法代替Connection,實現節約資源,用戶訪問高效目的,但是代碼本身與原生并無本質的減少。

3.2.1 c3p0使用

需要導入兩個jar包,maven引用方式如下:

com.mchange

c3p0

0.9.5.5

com.mchange

mchange-commons-java

0.2.15

配置文件必須在resources文件夾根目錄,且名稱必須為 c3p0.properties 或者 c3p0-config.xml,因此無需手動加載配置文件:

//1.創建數據庫連接池對象

DataSource ds = newComboPooledDataSource();//2. 獲取連接對象

Connection conn = ds.getConnection();

3.2.2 druid使用

只需要一個jar包,maven引入方式如下:

com.alibaba

druid-spring-boot-starter

1.1.9

配置文件名稱任意,但需要是.properties形式的,因此需要獲取配置文件位置,具體使用方式如下:

//1.加載配置文件

Properties pro = newProperties();

InputStream is= DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");

pro.load(is);//2.獲取連接池對象

DataSource ds =DruidDataSourceFactory.createDataSource(pro);//3.獲取連接

Connection conn = ds.getConnection();

3.3 JDBCUtils工具類的改造

以使用druid為例,在使用數據庫連接池時的工具類,主要有三種方法:

1. 獲取連接方法:通過數據庫連接池獲取連接

2. 釋放資源

3. 獲取連接池的方法

public classJDBCUtils {private staticDataSource ds;static{try{

Properties pro= newProperties();

pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));

ds=DruidDataSourceFactory.createDataSource(pro);

}catch(IOException e) {

e.printStackTrace();

}catch(Exception e) {

e.printStackTrace();

}

}public static Connection getConnection() throwsSQLException {returnds.getConnection();

}public static voidclose(Statement stmt, Connection conn) {

close(null, stmt, conn);

}public static voidclose(ResultSet rs, Statement stmt, Connection conn) {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();

}

}

}public staticDataSource getDataSource() {returnds;

}

}

總結

以上是生活随笔為你收集整理的java连接mysql数据库连接池_java使用原生MySQL实现数据的增删改查以及数据库连接池技术...的全部內容,希望文章能夠幫你解決所遇到的問題。

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