1.对比Statement与PreparedStatement
①Statement存在sql注入问题,PreparedStatement解决了sql注入问题。
②Statement是编译一次执行一次,PreparedStatement是编译一次,可执行n次。PreparedStatement效率较高一些。
③PreparedStatement会在编译阶段做类型的安全检查。
综上所述,PreparedStatement使用较多,只有极少的情况下会使用Statement。
什么情况下要使用Statement呢?
业务方面要求必须支持sql注入的时候。Statement支持sql注入,凡是业务方面要求需要进行sql语句拼接的,必须使用Statement。
2.PreparedStatement完成增加
package test;
import java.sql.*;
import java.util.ResourceBundle;
public class jdbcTest07 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("JDBC");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
PreparedStatement ps = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
String sql = "insert into test(神魔恋) values(?)";
ps = con.prepareStatement(sql);
ps.setString(1,"大明都亡了!");
int count = ps.executeUpdate();
System.out.println(count == 1 ? "修改成功" : "修改失败");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(ps != null)
ps.close();
}
catch (SQLException e) {
e.printStackTrace();
}
try {
if(con != null)
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.PreparedStatement完成修改
只需要更改少量代码:
String sql = "update test set 神魔恋=? where id=?";
ps = con.prepareStatement(sql);
ps.setString(1,"你好");
ps.setInt(2,7);
完成删除原理一样,在此不再赘述。
4.JDBC事物机制
自动提交是很不安全的,我们要将自动提交转换为手动提交。
5.JDBC工具类,简化JDBC编程
package test.utils;
import com.mysql.cj.AppendingBatchVisitor;
import java.sql.*;
public class DBUtil {
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://192.168.8.120:3306/ourleetcode?" +
"useUnicode=true&characterEncoding=utf8&serverTimezone=GMT","root", "10086");
}
public static void close(Connection con, Statement stmt, ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
6.测试DBUtil是否好用
package test;
import test.utils.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class jdbcTest08 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
String sql = "update test set 神魔恋=? where id=?";
ps = con.prepareStatement(sql);
ps.setString(1,"你sei呀?");
ps.setInt(2,7);
int count = ps.executeUpdate();
System.out.println(count == 1 ? "修改成功" : "修改失败");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBUtil.close(con, ps, rs);
}
}
}
可以看到,简化了非常多的代码。
但是请注意:用这个的前提是你默写的十分熟练了(在IDEA中不算,要达到在记事本中默写的一字不差才可以)!!!!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)