PreparedStatement 与 ResultSet
来源:互联网 发布:冬季男装品牌 知乎 编辑:程序博客网 时间:2024/05/21 01:46
1. TestPreparedStatement类
package day17;import java.sql.ResultSet;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;/* * PreparedStatement比Statement速度快,而且安全性高 * username="a" and psw="123" * username="a1" or 1=1 or 1="" and psw="123" */public class TestPreparedStatement {public static void main(String[] args) throws Exception { DBManager dbManager=null;Connection conn=null;PreparedStatement pstmt=null;ResultSet rs=null;//注册驱动dbManager=new DBManager();//获取连接conn=dbManager.getConnection();String sql="select * from students";pstmt=(PreparedStatement) conn.prepareStatement(sql);rs=pstmt.executeQuery();//rs永远不为空while(rs.next()){System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getInt(3));}dbManager.closeResource(conn,pstmt,rs);} public void PreparedStatementInsert() throws Exception{ DBManager dbManager=null;Connection conn=null;PreparedStatement pstmt=null;//注册驱动dbManager=new DBManager();//获取连接conn=dbManager.getConnection();//组织sql语句:?从1开始一次增加String sqlinsert="insert into students(姓名,专业)values(?,?)";/* * PreparedStatement对象将sql语句发送到数据库,表示预编译的SQL语句的对象。 */pstmt=(PreparedStatement) conn.prepareStatement(sqlinsert);pstmt.setString(1, "小熊");pstmt.setString(2, "计算机");//执行sqlint k=pstmt.executeUpdate();System.out.println("k== "+k);//关闭资源dbManager.closeResource(conn,pstmt,null); }public static void PreparedStatementupdate() throws Exception{DBManager dbManager=null;Connection conn=null;PreparedStatement pstmt=null;//注册驱动dbManager=new DBManager();//获取连接conn= dbManager.getConnection();//组织sql语句:?从1开始一次增加String sqlupdate="update students set 姓名=?,专业=? where id=?";/* * PreparedStatement对象将sql语句发送到数据库,表示预编译的SQL语句的对象。 */pstmt=(PreparedStatement) conn.prepareStatement(sqlupdate);pstmt.setString(1, "小bai");pstmt.setString(2, "计算机");pstmt.setInt(3, 2);//执行sqlint k=pstmt.executeUpdate();System.out.println("k== "+k);//关闭资源dbManager.closeResource(conn,pstmt,null); }}
2. DBManager.java
package day17;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;public class DBManager {private String url="jdbc:mysql://localhost:3306/test";Connection conn=null;public DBManager() throws Exception{Class.forName("com.mysql.jdbc.Driver");}public Connection getConnection() throws SQLException{conn=(Connection) DriverManager.getConnection(url, "root", "");return conn;}public void closeResource(Connection conn, PreparedStatement pstmt,ResultSet rs) throws SQLException {if(rs!=null){rs.close();}if(pstmt!=null){pstmt.close();}if(conn!=null){conn.close();}}}
- PreparedStatement 与 ResultSet
- PreparedStatement ResultSet
- JDBC02-Statement,PreparedStatement,ResultSet
- PreparedStatement 、CallableStatement和ResultSet
- PreparedStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)使用
- [JSP&JDBC]Statement/ResultSet/PreparedStatement
- jdbc preparedStatement和statement,resultset等区分
- jdbc preparedStatement和statement,resultset等区分
- java JDBC(二):Statement、ResultSet、PreparedStatement
- java.sql 接口 PreparedStatement ResultSet DriverManager
- Java jdbc中Statement,ResultSet,PreparedStatement
- JDBC之使用Statement,PreparedStatement,ResultSet
- 引入ResultSet包和PreparedStatement包错误
- JDBC主要接口DirverManager、Connection、Statement、PreparedStatement、ResultSet的简介
- JDBC主要接口DirverManager、Connection、Statement、PreparedStatement、ResultSet的简介
- java中PreparedStatement接口及ResultSet结果集
- JAVA-21-JDBC讲解,Driver、Connection、Statement、PreparedStatement、CallableStatement、ResultSet
- 使用连接池一定要正确关闭PreparedStatement和ResultSet
- 微软的一道经典逻辑推理题:小明和小强都是张老师的学生,张老师的生日是M月N日
- datalist分页设置
- servlet简单上传示例
- explicit构造函数
- [html]HTML frameset+frame 网页多窗口显示语句 [大三TJB_708]
- PreparedStatement 与 ResultSet
- ioctl 函数
- 学习一种新的编程语言所要做的15个练习
- 网页分类技术介绍
- Arcgis for flex 给Google卫星地图加上地点名(道路图层)
- Hadoop安装
- malloc与free(1)
- jQuery textbox select checkbox radio 取值,设置值
- python串口编程整理(更新完)