更多精彩文章欢迎关注公众号“Java之康庄大道”

此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的

具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写。

上一篇地址http://www.cnblogs.com/yunqing/p/6136896.html

1、增加

/**
* 新建一个使用PreparedStatement的方法
* PreparedStatement与Statement的区别
* 1.不需要sql语句拼接,防止sql注入,更加安全
* 2.用占位符的方式写sql,便于后期维护,提高代码可读性,可以自动对类型进行转换
* 3.有预编译功能,可以大批量处理sql,(mysql不明显,Oracle很明显)
*
* 向数据库中添加一条数据
*
* PreparedStatement:用于执行sql语句的对象
* 用connection的PreparedStatement(sql)方法获取
* 用executeUpdate(sql)执行sql语句
* 注意:只能执行 insert,update,delect,不能执行select
*
*/
public void testPreparedStatement(){
Connection conn=null;
PreparedStatement preStatement=null;//创建PreparedStatement对象
try {
//1、准备Connection连接数据库
conn=JDBCTools.getConnection2();
//2、准备sql语句
//sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
String sql="insert into t_student(name,age,email) values(?,?,?)";
//3、准备prepareStatement
//对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
preStatement=conn.prepareStatement(sql);
//4、占位符设置值
preStatement.setString(1, "klkl");
preStatement.setInt(2, 12);
preStatement.setString(3, "kkk@jjj");
//5、执行sql
preStatement.executeUpdate(); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
//6、关闭数据库等
JDBCTools.guanbi(null, preStatement, conn);
}
}

2.修改

    public void testPreparedStatement(){
Connection conn=null;
PreparedStatement preStatement=null;//创建PreparedStatement对象
try {
conn=JDBCTools.getConnection2(); //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
String sql="update t_student set name=?,age=?,email=? where id=?"; //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
preStatement=conn.prepareStatement(sql);
preStatement.setString(1, "asas");
preStatement.setInt(2, 12);
preStatement.setString(3, "asa@jjj");
preStatement.setInt(4, 11);
//执行sql
preStatement.executeUpdate(); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
JDBCTools.guanbi(null, preStatement, conn);
}
}

3、删除

    public void testPreparedStatement(){
Connection conn=null;
PreparedStatement preStatement=null;//创建PreparedStatement对象
try {
conn=JDBCTools.getConnection2(); //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
String sql="delete from t_student where id=?"; //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
preStatement=conn.prepareStatement(sql);
preStatement.setInt(1, 12);
//执行sql
preStatement.executeUpdate(); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
JDBCTools.guanbi(null, preStatement, conn);
}
}

4.查询

    public void testPreparedStatement(){
Connection conn=null;
PreparedStatement preStatement=null;//创建PreparedStatement对象
ResultSet rs=null;
try {
//1.准备Connection连接数据库
conn=JDBCTools.getConnection2();
//2.准备sql字符串
String sql="select * from t_student";
//3.准备prepareStatement
preStatement=conn.prepareStatement(sql);
//4.执行sql得到ResultSet
rs=preStatement.executeQuery();
//5.处理ResultSet显示查询到的结果
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
int age=rs.getInt(3);
String email=rs.getString(4); System.out.println(id);
System.out.println(name);
System.out.println(age);
System.out.println(email);
} } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
//6.关闭
JDBCTools.guanbi(rs, preStatement, conn);
}
}

JDBC增删改查,PreparedStatement和Statement的区别的更多相关文章

  1. JDBC增删改查和查唯一的完整代码

    第一部分代码(实体类) package com.wf.entity; public class Hehe{ private int hehe_id; private String hehe_name; ...

  2. JDBC增删改查

    /* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...

  3. jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决

    最近开始复习以前学过的JDBC今天肝了一晚上 来睡睡回笼觉,长话短说 我们现在开始. 我们先写一个获取数据库连接的jdbc封装类 以后可以用 如果不是maven环境的话在src文件下新建一个db.pr ...

  4. jdbc增删改查进行封装

    jdbc封装 1 dao (代码分层) com.aaa.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 com.aaa.servlet 存放servle ...

  5. JAVA JDBC 增删改查简单例子

    1.数据库配置文件jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username= ...

  6. JDBC 增删改查代码 过滤查询语句

    package test; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; i ...

  7. JDBC增删改查简单测试

    首先编写一个entity以便与数据库表文件相对应 lyTable.java public class LyTable implements java.io.Serializable { private ...

  8. 商城项目整理(三)JDBC增删改查

    商品表的增加,修改,删除,订单表的增加,确认,用户表的查看,日志表的增加,查看 商品表建表语句: create table TEST.GOODS_TABLE ( gid NUMBER not null ...

  9. 增删改查——PreparedStatement接口

    1.添加 package pers.Pre.add; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...

随机推荐

  1. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

  2. 【noiOJ】p8210

    10:河中跳房子 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心 ...

  3. Android --自定义简单Toast

    1. 效果图

  4. bug:clang: error: no input files

    1.clang: error: no input files这个问题一般是因为你删除或者移动了某一个文件,但是在你的编译资源里面( project > target > Build Pha ...

  5. 'Could not load NIB in bundle: 'NSBundle xxx/storeFlix.app> ' with name 'UIViewController-w6Q-ra-j06' and directory 'StoreFlixIpad.storyboardc

    1.此代码是从 git clone xxx 下载的. 2.使用 sourcetree 下载即可解决此问题.

  6. UICollectionView集合视图的概念

    如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...

  7. Android---表格布局

    最简单的表格布局

  8. Java_解密ThreadLocal

    概述 相信读者在网上也看了很多关于ThreadLocal的资料,很多博客都这样说:ThreadLocal为解决多线程程序的并发问题提供了一种新的思路:ThreadLocal的目的是为了解决多线程访问资 ...

  9. [LintCode] Wiggle Sort 扭动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  10. ubuntu 安装 GCC

    网上查了好多方式,试了一下,最简单可行的是: sudo apt-get install  build-essential 等待执行完,输入 gcc -v 输出: Using built-in spec ...