mysql-connector-java-x.x.x-bin.jar
驱动包,也可以参考 https://blog.csdn.net/github_38336924/article/details/82352869 文章// 动态加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mypro";
String user = "dadeity";
String password = "*****";
Connection conn = DriverManager.getConnection(url, user, password);
public static Connection getConnection() throws ClassNotFoundException {
String url="jdbc:mysql://192.168.137.142:3306/mypro";
String user="dadeity";
String password="*****";
String driverClass="com.mysql.jdbc.Driver";
Connection con=null;
Class.forName(driverClass);
try {
con=DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
src
目录下创建一个以.properties
结尾的配置文件,内容如下driver:com.mysql.jdbc.Driver
url:jdbc:mysql://192.168.137.142:3306/mypro
user:dadeity
password:****
InputStream inputStream = DemoTwo.class.getClassLoader().getResourceAsStream("db.properties");
Properties
对象Properties pro = new Properties();
pro.load(inputStream);
String driver = pro.getProperty("driver");
String url = pro.getProperty("url");
String user = pro.getProperty("user");
String password = pro.getProperty("password");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
public static Connection getConnection() throws IOException, ClassNotFoundException {
// 读取类路径下的jdbc.propreties文件(配置文件)
InputStream inputStream = DemoTwo.class.getClassLoader().getResourceAsStream("db.properties");
// 创建 Properties 对象
Properties pro = new Properties();
// 装载
pro.load(inputStream);
// 解析
String driver = pro.getProperty("driver");
String url = pro.getProperty("url");
String user = pro.getProperty("user");
String password = pro.getProperty("password");
Connection conn = null;
// 注册驱动获取连接
Class.forName(driver);
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
c3p0
连接池连接数据库所需的jar包:c3p0-0.9.2.1.jar
,mchange-commons-java-0.2.3.4.jar
,mysql-connector-java-5.1.40-bin.jar
c3p0-*.jar
下载地址 http://www.java2s.com/Code/Jar/c/Downloadc3p00921jar.htmlmchange-commons-java-*.jar
下载地址http://www.java2s.com/Code/Jar/m/Downloadmchangecommonsjava0234jar.htmsrc
目录下新建c3p0-config.xml
文件,内容如下:<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloc3p0">
<!-- 连接数据源的基本属性 -->
<property name="user">dadeity</property>
<property name="password">*****</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://192.168.137.142/mypro</property>
<!-- 若数据库中连接数不足时,一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
<!-- c3p0数据库连接可以维护的statement的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的statement对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
// 定义全局变量
private static ComboPooledDataSource ds;
// 静态代码块,命名配置
static {
ds = new ComboPooledDataSource("helloc3p0");
}
// 获取数据源
public static DataSource getDataSource() {
return ds;
}
// 获取连接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
所需的 jar
包:commons-dbcp2-2.5.0.jar
和相关的 jar
包
commons-dbcp2-*.jar
下载地址 http://www-eu.apache.org/dist//commons/dbcp/binaries/commons-dbcp2-2.5.0-bin.tar.gzBasicDataSource bs=new BasicDataSource();
bs.setDriverClassName("com.mysql.jdbc.Driver");
bs.setUrl("jdbc:mysql://192.168.137.142:3306/mypro");
bs.setUsername("dadeity");
bs.setPassword("*****");
public static BasicDataSource getBasicDataSource(){
// 实例化BasicDataSource
BasicDataSource bs=new BasicDataSource();
// 设置BasicDataSource属性
// 设置四个属性
bs.setDriverClassName("com.mysql.jdbc.Driver");
bs.setUrl("jdbc:mysql://192.168.137.142:3306/mypro");
bs.setUsername("dadeity");
bs.setPassword("*****");
return bs;
}