JDBC之preparedStatement
来源:互联网 发布:2017广电总局 网络盒子 编辑:程序博客网 时间:2024/05/17 01:41
直接贴代码案例,理论知识可以从隔壁看看。
prestmtest.java
package PREPAREDSTAMENT;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;public class prestmtest { String url="jdbc:mysql://localhost:3306/data"; String user="root"; String password="123456"; @Test /* * 增加 */ public void testInsert(){ Connection conn=null; PreparedStatement pstmt=null; //获取链接对象 try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url, user, password); //准备预编译sql语句 String sql="insert into xxb values(?,?,?)"; //执行sql pstmt=conn.prepareStatement(sql); //设置参数 pstmt.setInt(1, 4); pstmt.setString(2, "杨六"); pstmt.setString(3, "女"); int count=pstmt.executeUpdate(); System.out.print("影响了"+count+"行"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } //throw new RuntimeException(e); finally{ if(pstmt!=null) try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } /* * 删除 */ public void testDelete(){ Connection conn=null; PreparedStatement pstmt=null; //获取链接对象 try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url, user, password); //准备预编译sql语句 String sql="delete from xxb where sid=?"; //执行sql pstmt=conn.prepareStatement(sql); //设置参数 pstmt.setInt(1, 4); int count=pstmt.executeUpdate(); System.out.print("影响了"+count+"行"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } //throw new RuntimeException(e); finally{ if(pstmt!=null) try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } /* * 修改 */ public void testUpdate(){ Connection conn=null; PreparedStatement pstmt=null; //获取链接对象 try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url, user, password); //准备预编译sql语句 String sql="update xxb set sname=? where sid=?"; //执行sql pstmt=conn.prepareStatement(sql); //设置参数 pstmt.setString(1, "张三"); pstmt.setInt(2, 1); int count=pstmt.executeUpdate(); System.out.print("影响了"+count+"行"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } //throw new RuntimeException(e); finally{ if(pstmt!=null) try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } /* * 查询 */ public void testQuery(){ Connection conn=null; PreparedStatement pstmt=null; //获取链接对象 try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url, user, password); //准备预编译sql语句 String sql="select *from xxb"; //执行sql pstmt=conn.prepareStatement(sql); //遍历 ResultSet rs=pstmt.executeQuery(); while(rs.next()){ int id=rs.getInt("sid"); String name=rs.getString("sname"); String gender=rs.getString("sgender"); System.out.println(id+" "+name+" "+gender); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } //throw new RuntimeException(e); finally{ if(pstmt!=null) try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } } }
数据库文件
SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for `xxb`-- ----------------------------DROP TABLE IF EXISTS `xxb`;CREATE TABLE `xxb` ( `sid` int(11) default NULL, `sname` varchar(20) default NULL, `sgender` varchar(20) default NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records -- ----------------------------INSERT INTO `xxb` VALUES ('1','zhangsan','man'), ('2','lisi','woman'), ('3','wangwu','man');
阅读全文
1 0
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC 之 PreparedStatement 接口
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC基础教程之PreparedStatement
- JDBC之PreparedStatement
- Java JDBC 之 PreparedStatement
- JDBC之PreparedStatement
- JDBC之PreparedStatement
- JDBC之preparedStatement
- JDBC之PreparedStatement接口
- JDBC基础教程之PreparedStatement (引用)
- JDBC学习之二PreparedStatement
- java异常
- 嵌入式软件工程师经典笔试题
- HTML标签大全
- ES6 扩展操作符的一个误区
- 继承.重写.重载
- JDBC之preparedStatement
- Error LNK2001 无法解析的外部符号 的几种情况及解决办法
- hdu6119 尺取 双指针
- PHP关于(类对象)回调方法的使用
- phpl判断mysq数据库中的某个数据表是否存在的方法总结
- 关于IDEA工具在springboot整合mybatis中出现的Invalid bound statement (not found)问题
- HDU1160DP最长上升子序列微整形
- 尔石山
- java-三角形矩形输入长宽求面积周长等