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实现数据的增删改查以及数据库连接池技术...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用升降桌打造一个舒适的居家高效办公环境用
- 下一篇: mysql 5.7.17 源码安装_my