PreparedStatement继承自Statement,可以预先创建好SQL语句,然后传入参数值
public class TestPreparedStatement{
public void testPreparedStatement() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbc",
"root","root");
String sql = "insert into bank values(?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 10000);
pstmt.setString(2, "张三");
pstmt.setInt(3, 50000);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
本文详细介绍了如何利用PreparedStatement预编译SQL语句并传入参数,以提高MySQL操作的效率和安全性。通过实例演示了设置参数和执行更新操作的过程。
881

被折叠的 条评论
为什么被折叠?



