数据库连接池
为什么使用数据库连接池?
如果不使用连接池每一次客户端发出的业务请求都需要对应一次数据库连接,如果有一万次请求就有一万次数据库连接的建立和断开,频繁的开关连接会影响程序的执行效率,通过数据库连接池可以将数据库连接进行复用,从而提高执行效率.
如何使用?
//创建连接池对象 DruidDataSource ds = new DruidDataSource(); //设置数据库连接信息 ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); //设置初始连接数量 ds.setInitialSize(Integer.parseInt(initSize)); //设置最大连接数量 ds.setMaxActive(Integer.parseInt(maxSize)); //从连接池对象中获取连接 异常抛出 Connection conn = ds.getConnection();
jdbc.properties
db.driver=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/newdb3?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true db.username=root db.password=123456 db.maxActive=10 db.initialSize=2
读取*.properties配置文件
//创建读取配置文件的属性对象 Properties p = new Properties(); //得到文件输入流 这种写法会自动去src/main/resources目录下找文件 InputStream ips = Demo03.class.getClassLoader() .getResourceAsStream("my.properties"); //把文件输入流交给属性对象 p.load(ips); //读取数据 String name = p.getProperty("name"); //getProperty方法只能读取字符串 String age = p.getProperty("age"); System.out.println(name+":"+age);
读取 jdbc.properties配置文件
public class DBUtils { private static DruidDataSource ds; //因为读取配置文件只需要读取一次,连接池对象也只需要创建一次 static { //读取配置文件里面的数据 Properties p = new Properties(); InputStream ips = DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); try { p.load(ips); } catch (IOException e) { e.printStackTrace(); } String driver = p.getProperty("db.driver"); String url = p.getProperty("db.url"); String username = p.getProperty("db.username"); String password = p.getProperty("db.password"); String maxActive = p.getProperty("db.maxActive"); String initialSize = p.getProperty("db.initialSize"); ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); // 设置初始连接数量 ds.setInitialSize(Integer.parseInt(initialSize)); // 设置最大连接数量 ds.setMaxActive(Integer.parseInt(maxActive)); } public static Connection getConn() throws SQLException { // 注册驱动 // 从连接池对象中获取连接 异常抛出 Connection conn = ds.getConnection(); System.out.println(conn); return conn; } }
创建用户表
use newdb3;
create table user(id int primary key auto_increment,username varchar(20),password varchar(20))charset=utf8;
注册功能
import java.sql.Connection; import java.sql.Statement; import java.util.Scanner; /** * 注册功能 * @author 海拥😋 * @date 2020年5月7日 下午2:04:42 */ public class Demo04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名"); String username = sc.nextLine(); System.out.println("请输入密码"); String password = sc.nextLine(); sc.close(); // 获取连接 try (Connection conn = DBUtils.getConn();) { Statement s = conn.createStatement(); String sql = "insert into user values(null,'"+username+"','"+password+"')"; s.executeUpdate(sql); System.out.println("注册成功"); } catch (Exception e) { e.printStackTrace(); } } }
登录功能
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; /** * 登录功能 * @author 海拥😋 * @date 2020年5月7日 下午2:11:29 */ public class Demo05 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名"); String username = sc.nextLine(); System.out.println("请输入密码"); String password = sc.nextLine(); //获取连接 try (Connection conn = DBUtils.getConn();) { Statement s = conn.createStatement(); // String sql = // "select count(*) from user where username='" // +username+"'and password='"+password+"'"; // ResultSet rs = s.executeQuery(sql); //通过PrepredStatement解决SQL注入问题 String sql = "select count(*) from user where username=? and password=?"; //在编译时已经将SQL语句的业务逻辑锁死 用户写的内容不会再影响逻辑 PreparedStatement ps = conn.prepareStatement(sql); //替换sql中的? ps.setString(1, username); ps.setString(2, password); //执行SQL ResultSet rs = ps.executeQuery(); //查询回来的是符合条件的数量 只有一条数据所以可以用if 如果查询回来的是 //多条数据必须用while if(rs.next()) { //取出查询回来的符合条件的数量 int count =rs.getInt(1); if(count>0) { System.out.println("登录成功"); }else { System.out.println("登录失败"); } } } catch (Exception e) { e.printStackTrace(); } } }