-
加载驱动
Class.forName(“com.mysql.jdbc.Driver”); -
获得数据库连接
Connection conn=null;
conn=DriverManager.getConnection(“jdbc:mysql://localhost:端口号(3306)/要连接的数据库”,“用户名”,“密码”); -
创建预定义执行sql语句的对象 PreparedStatement
String sql=“updateuser
set uname=? where uid=?;”;
PreparedStatement pstmt=null;
pstmt= conn.preparedStatement(sql);
注意:SQL语句中涉及参数的都用?占位符代替 -
设置参数,并执行sql语句
pstmt.setString(参数位置从1开始,参数值);
pstmt.setInt(参数位置从1开始,参数值 );
pstmt.executeUpdate();//insert update delete
或者rs=pstmt.executeQuery();//select -
在finally里关闭各种资源
pstmt.close();
conn.close();
PreparedStatement使用步骤
最新推荐文章于 2022-12-02 14:02:16 发布