PrepareStatement的步骤

(1)导入jar并加载驱动
Class.forName("com.mysql.jdbc.Driver");
(2)获取连接
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest","root","root");
(3)写sql语句---?
String sql="select * from user where uname=? and password=?";
(4)生成PrepareStatement
PreparedStatement prepareStatement=connection.prepareStatement(sql);
(5)设置参数
prepareStatement.setString(1, username);
prepareStatement.setString(2, usermm);
(6)执行sql操作
prepareStatement.executeUpdate();//增删改,返回影响行数
ResultSet resultSet=prepareStatement.executeQuery();//查询
(7)关闭资源---后开的先关

上一篇:String字符串连接性能优化StringBuilder(阿里Java开发手册规范)


下一篇:JDBC学习笔记--ResultSetMetaData