JDBC和连接池
大纲
- JDBC
- 连接数据库的方式
具体案例
JDBC
需求:满足Java程序能对多个不同的数据库进行操作,而创建了一种接口,实现对数据库的规范
连接数据库的方式
1.方法1
先创建一个Driver对象,然后设置连接到的数据库的地址,然后创建一个properties对象,在里面设定好账户密码,然后通过driver的connect方法,创建出connect连接
public class jdbc01 {public static void main(String[] args) throws SQLException {// 前置工作,在项目下创建文件夹,然后将jar文件拷贝到该目录下,// 然后将其加入到项目中// 1.注册驱动Driver driver = new Driver();// 2.得到连接// (1)jdbc:mysql://表示表示规定好的协议// (2)localhost 应该是ip地址(这里是主机的ip地址)// (3)3306表示MySQL监听的端口// (4)test db 是指连接到MySQL的哪个数据库// (5)本质上是进行socket连接String url = "jdbc:mysql://localhost:3306/test01";// 将用户名和密码封装到一个Properties对象中Properties properties = new Properties();// user和password是规定好的,后面的值根据实际情况properties.setProperty("user","root");properties.setProperty("password"," ");Connection connect = driver.connect(url, properties);// 3.执行sql语句String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110')";// Statement 用于执行静态sql语句并返回生成的结果的对象Statement statement = connect.createStatement();int rows = statement.executeUpdate(sql);// 如果是dml语句,返回的就是影响到的行数System.out.println(rows > 0? "成功":"失败");//4.关闭连接statement.close();connect.close();}
}
缺点:driver是第三方的,依赖性强,灵活性差
2.使用反射机制
public class jdbc02 {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");Driver driver = (Driver) aClass.newInstance();String url = "jdbc:mysql://localhost:3306/test01";Properties properties = new Properties();properties.setProperty("user","root");properties.setProperty("password","");Connection connect = driver.connect(url, properties);System.out.println(connect);}
}
3.使用DriverManager替换Driver
这种方法具有更好的拓展性
public class jdbc03 {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");Driver driver = (Driver) aClass.newInstance();String url = "jdbc:mysql://localhost:3306/test01";String user = "root";String password = "";// 也可以还是使用properties来存储账户和密码,最后在DriverManager的getConnection方法里传入url和properties;DriverManager.registerDriver(driver);Connection connection = DriverManager.getConnection(url, user, password);System.out.println(connection);}
}
4.自动注册,简化操作(推荐使用)
在反射时,完成了类的加载,在静态代码块里实现了自动注册
public class jdbc04 {public static void main(String[] args) throws ClassNotFoundException, SQLException {Class.forName("com.mysql.jdbc.Driver");// 可以不写String url = "jdbc:mysql://localhost:3306/test01";String user = "root";String password = "lei2483034010";Connection connection = DriverManager.getConnection(url, user, password);System.out.println(connection);}
}
5.使用配置文件(最推荐)
在4方法的基础上,使用配置文件来存储账户和密码,更加的灵活
public class jdbc05 {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {Properties properties = new Properties();properties.load(new FileInputStream("src\\mysql.properties"));String user = properties.getProperty("user");String password = properties.getProperty("password");String driver = properties.getProperty("driver");String url = properties.getProperty("url");Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection(url, user, password);System.out.println(connection);}
}
执行sql语句
实际开发中,基本不使用statement,因为它不能预防sql注入
所以使用preparedStarement来防止sql的注入
使用这个类的好处
public class PreparedStatement {public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {Scanner myScanner = new Scanner(System.in);System.out.println("请输入账号");String account = myScanner.nextLine();System.out.println("请输入密码");String pwd = myScanner.nextLine();Properties properties = new Properties();properties.load(new FileInputStream("src\\mysql.properties"));String user = properties.getProperty("user");String password = properties.getProperty("password");String driver = properties.getProperty("driver");String url = properties.getProperty("url");Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection(url, user, password);String sqlSelect = " select name,pwd from admin where name =? and pwd =?";java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sqlSelect);// 赋值preparedStatement.setString(1,account);preparedStatement.setString(2,pwd);ResultSet resultSet = preparedStatement.executeQuery();// 得到一个查询到resultSet集if (resultSet.next()){System.out.println("恭喜,登录成功");}else {System.out.println("对不起,登录失败");}resultSet.close();preparedStatement.close();connection.close();}
}