Jdbc整合Druid
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dashu</groupId> <artifactId>JdbcDriud</artifactId> <version>V1.0.1</version> <dependencies> <!--引入druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.8</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies> </project>
package com.dashu.util; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * @Auther: DaShu * @Date: 2021/7/22 19:39 * @Description: */ public class ConnUtil { private ConnUtil() { } private static DataSource dataSource; static { InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("dataSource.properties"); Properties properties = new Properties(); try { properties.load(inStream); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } /* * @Target:获取连接 * @Author: DaShu * @Date: 2021/7/22 19:55 * @Result: */ public static Connection getConnection() { try { return dataSource.getConnection(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("获取数据库连接异常"); } } /* * @Target:释放资源 * @Author: DaShu * @Date: 2021/7/22 19:54 * @Result: */ public static void close(ResultSet rSet, PreparedStatement pStatement, Connection connection) { try { if (rSet != null) { rSet.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pStatement != null) { pStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } }
package com.dashu; import com.dashu.util.ConnUtil; import org.junit.Test; import java.sql.*; /** * @Auther: DaShu * @Date: 2021/7/22 20:00 * @Description: */ public class crud { @Test public void test() throws SQLException { Connection conn = null; try{ conn= ConnUtil.getConnection();//从连接池获取链接 conn.setAutoCommit(false);//开启事务 PreparedStatement statement = conn.prepareStatement("insert into tree (id,name,age) values(?,?,?)"); statement.setInt(1,7); statement.setString(2,"张天爱"); statement.setInt(3,90); Boolean result =statement.execute(); PreparedStatement stat = conn.prepareStatement("update tree set name = ?,age = ? where id = ?"); stat.setString(1,"张晓霞"); stat.setInt(2,89); stat.setInt(3,2); Boolean results =stat.execute(); conn.commit();//提交的那一刻数据就已经更新了。 conn.close(); } catch (Exception e) { conn.rollback(); e.printStackTrace(); } } }
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/tree username=root password=root