PreparedStatement 和 Statement的区别:
- PreparedStatement 支持预编译,对于批量处理可以大大提高效率;Statement
不支持预编译
- PreparedStatement可以写参数化查询,比Statement能获得更好的性能。
- Prepared Statement可以操作Bobl类型的数据,
- Prepared Statement可以实现更高效率的批量操作。
- 可以解决Statement的拼串,sql注入问题。
JDBC 连接数据库的步骤
- 加载JDBC驱动程序
- 读取配置文件中的4个信息(root,password,url,driveClass)
2、获得 Connection con;DriverManager.getConnection(url , uname , password )。
3、创建 statement stmt;con.prepareStatement(sql)。
4、执行 Sql;stmt.executeQuery(sql)。
5、处理 ResultSet 结果集。
6、关闭 JDBC 对象。