PreparedStatement接口详解
来源:互联网 发布:网络电影制作流程 编辑:程序博客网 时间:2024/06/05 12:47
PerparedStatement接口详解:
一、jdbc工具类扩展:
//close()方法重载 public static void close(Connection conn,Statement stmt,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } }
二、PreparedStatement执行增删改的sql语句:
package sram.jdbc.connection;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import org.junit.Test;import sram.util.jdbc.JdbcUtil;/** * PreparedStatement执行sql语句 */public class Demo4 { /** * 插入、修改、删除 */ @Test public void test(){ Connection conn = null; PreparedStatement stmt = null; try { //1.获取连接 conn = JdbcUtil.getConnection(); //2.准备预编译的sql //插入:String sql = "INSERT INTO dept(NAME,BossName) VALUES(?,?)";//?表示一个参数的占位符 //修改:String sql = "UPDATE dept SET BossName=? WHERE id=?"; //删除: String sql = "DELETE FROM dept WHERE id=?"; //3.执行预编译sql语句(检查语法) stmt = conn.prepareStatement(sql); //4.设置参数值 /** * 参数一: 参数位置 从1开始 */ /*插入: stmt.setString(1, "财务部"); stmt.setString(2, "孙五"); */ /*修改 stmt.setString(1, "钱八"); stmt.setInt(2, 7); */ //删除: stmt.setInt(1, 7); //5.发送参数,执行sql。不同于Statement接口的该方法,这里()不带sql语句。 int count = stmt.executeUpdate(); System.out.println("影响了"+count+"行"); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); }finally{ JdbcUtil.close(conn, stmt); } }}
三、PreparedStatement执行查询的sql语句:
package sram.jdbc.connection;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;import sram.util.jdbc.JdbcUtil;/** * 查询 */public class Demo5 { @Test public void test(){ Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { //1.获取连接 conn = JdbcUtil.getConnection(); //2.准备预编译的sql String sql = "SELECT * FROM dept"; //3.预编译 stmt = conn.prepareStatement(sql); //4.执行sql rs = stmt.executeQuery(); //5.遍历rs while(rs.next()){ int id =rs.getInt("id"); String name =rs.getString("NAME"); String bossName = rs.getString("BossName"); System.out.println(id+","+name+","+bossName); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); }finally{ //关闭资源 JdbcUtil.close(conn, stmt, rs); } }}
1 0
- PreparedStatement接口详解
- preparedStatement 接口
- PreparedStatement接口
- PreparedStatement接口
- PreparedStatement接口
- PreparedStatement接口
- PreparedStatement详解
- PreparedStatement详解
- preparedStatement详解
- JDBC 之 PreparedStatement 接口
- PreparedStatement接口,prepareStatement方法
- PreparedStatement接口,prepareStatement方法 .
- PreparedStatement接口.txt
- jdbc中PreparedStatement接口
- PreparedStatement 接口引入
- Java中的PreparedStatement接口
- JDBC之PreparedStatement接口
- PreparedStatement的用法详解
- 基于公众号服务的预付款会员卡服务系统
- 时间相关
- View 事件传递
- 60.[PHP]PHP简介及LNMP环境搭建(CentOS 7环境)
- 如何解决request.getParameter()取值乱码问题
- PreparedStatement接口详解
- maven私服搭建
- iOS面试常见问题最全梳理
- 二叉树的三种遍历方式的递归实现和迭代实现
- 学习笔记之JavaSE(51)--网络编程3
- HBase安装配置之伪分布式模式
- Qt 多线程之可重入(Reentrancy)与线程安全(Thread-Safety)
- Linux基础
- Ubuntu安装jdk tar gz的方法