Java 连接 MySQL 数据库方法

版权声明:本文为博主原创文章,网络媒体转载请注明出处,否则属于侵权行为 https://blog.csdn.net/github_38336924/article/details/82746647

1、传统的连接方式

# 动态加载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);
  • Code
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;
    }

这里写图片描述

2、读取配置文件方式

  • 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);
  • Code
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;
    }

这里写图片描述

3、c3p0连接池连接数据库

所需的jar包:c3p0-0.9.2.1.jarmchange-commons-java-0.2.3.4.jarmysql-connector-java-5.1.40-bin.jar

<?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>
  • 获取连接Code
// 定义全局变量
private static ComboPooledDataSource ds;
// 静态代码块,命名配置
static {
	ds = new ComboPooledDataSource("helloc3p0");
}
// 获取数据源
public static DataSource getDataSource() {
	return ds;
}
// 获取连接
public static Connection getConnection() throws SQLException {
	return ds.getConnection();
}

这里写图片描述

4、数据库连接池

所需的jar包:commons-dbcp2-2.5.0.jar和相关的jar

BasicDataSource bs=new BasicDataSource();
  • 设置属性
bs.setDriverClassName("com.mysql.jdbc.Driver");
bs.setUrl("jdbc:mysql://192.168.137.142:3306/mypro");
bs.setUsername("dadeity");
bs.setPassword("*****");
  • Code
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;
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/github_38336924/article/details/82746647
今日推荐