JdbcUtil工具类两种实现方式

1.首先我们需要导入3个包(mvnrepository.com搜这个网址下载)

commons-dbcp-1.2.1.jar(apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件)

commons-pool.jar(连接池jar包)

mysql-connector-java-5.1.40-bin.jar(连接数据库的jar包)

2.在mysql数据库中创建名为jdbcDB的表

3.导入db.properties,放在src目录下(这里用的是mysql数据库)

jdbc.url=jdbc:mysql://localhost:3306/jdbcDB?useSSL=true
jdbc.username=root
jdbc.password=root
jdbc.maxActive=100
jdbc.initialSize=20
jdbc.maxWait=5000

jdbc.url=jdbc:mysql://localhost:3306/jdbcDB?useSSL=true(连接数据库的地址)
jdbc.username=root(连接数据库的用户名)
jdbc.password=root(连接数据库的密码)
jdbc.maxActive=100(最大连接数据库连接数)
jdbc.initialSize=20(初始化连接数据库连接数,同时最多有20个数据库连接)

jdbc.maxWait=5000(最大连接时间毫秒数:ms超出等待时间会出错误信息)

4.创建JdbcUtil类

(第一种方式,不使用db.properties)

package jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcUtil {
	private static String url = "jdbc:mysql://localhost:3306/jdbcDB?useSSL=true";
	private static String username = "root";
	private static String password = "root";
	private JdbcUtil(){}
	public static Connection getConn() throws SQLException{
		return DriverManager.getConnection(url,username,password);
	}
	public static void close(Connection conn){
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) throws SQLException {
		System.out.println(getConn());
	}
}

(第二种方式,使用db.properties)

package jdbcday04;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
public class JdbcUtil {
	private static BasicDataSource dataSource = null;
	static{
		dataSource = new BasicDataSource();
		Properties prop = new Properties();
		try {
			prop.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
			String url = prop.getProperty("jdbc.url");
			String username = prop.getProperty("jdbc.username");
			String password = prop.getProperty("jdbc.password");
			String initialSizeStr = prop.getProperty("jdbc.initialSize"); 
			int initialSize = Integer.parseInt(initialSizeStr);
			String maxActiveStr = prop.getProperty("jdbc.maxActive");
			int maxActive = Integer.parseInt(maxActiveStr);
			String maxWaitStr = prop.getProperty("jdbc.maxWait");
			int maxWait = Integer.parseInt(maxWaitStr);
			try {
				Class.forName("com.mysql.jdbc.Driver");
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
			dataSource.setUrl(url);
			dataSource.setUsername(username);
			dataSource.setPassword(password);
			dataSource.setInitialSize(initialSize);
			dataSource.setMaxActive(maxActive);
			dataSource.setMaxWait(maxWait);	
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	public static Connection getConn() throws SQLException{
		return dataSource.getConnection();
	}
	public static void close(Connection conn){
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
	}
	public static void main(String[] args) throws SQLException {
		System.out.println(getConn());
		System.out.println(dataSource.getUrl()+dataSource.getUsername()+dataSource.getPassword()
				+dataSource.getInitialSize()+dataSource.getMaxActive()+dataSource.getMaxWait());
		System.out.println(getConn());
	}
}
到此结束!转载请注明出处!

猜你喜欢

转载自blog.csdn.net/weixin_41807943/article/details/80666728