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();
}
}