1、 PreparedStatement是Statement的子接口,可以传入传入带有占位符的SQL语句,并且提供了相应的方法来替换占位符(setXxx(int index, Object value)index从1开始),

然后通过executeUpdate或executeQuery()方法来执行SQL语句。

2、更新操作

public int update(String sql, Object ... args){
int rowNum = 0;
Connection conn = null;
PreparedStatement ps = null; try{
conn = getConnection();
ps = conn.prepareStatement(sql);
for(int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
} rowNum = ps.executeUpdate();
return rowNum;
}catch(Exception e){
e.printStackTrace();
}finally{
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return rowNum;
}

3、查询操作

public static void query(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; StringBuffer sb = new StringBuffer("SELECT name, email, salary from emp1 ");
sb.append("where id = ?");
try{
conn = getConnection();
ps = conn.prepareStatement(sb.toString()); ps.setInt(1, 1234); rs = ps.executeQuery();     if(rs.next()){
String name = rs.getString(1);
String email = rs.getString(2);
double salary = rs.getDouble(3);
System.out.println(name + ":" + email + ":" + salary);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

4、SQL注入攻击:是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入中注入非法的SQL语句段或命令,从而利用系统的SQL引擎完成恶意行为的做法。

5、使用PreparedStatement可以有效地避免SQL注入。而Statement无法禁止SQL注入;

6、使用PreparedStatement可提高代码的可读性和可维护性,同时它能最大可能提高性能。

JDBC--PreparedStatement使用的更多相关文章

  1. Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement

    Connection.prepareStatement()函数出错,提示: Type mismatch: cannot convert from java.sql.PreparedStatement ...

  2. 关于Mysql数据库longblob格式数据的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V问题分析

    当数据库字段为blob类型时 ,我们如果使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法需要注意 在向blob字段类型中插入数据时 ...

  3. mysql数据库插入数据获取自增主键的三种方式(jdbc PreparedStatement方式、mybatis useGeneratedKeys方式、mybatis selectKey方式)

    通常来说对于mysql数据库插入数据获取主键的方法是采用selectKey的方式,特别是当你持久层使用mybatis框架的时候. 本文除此之外介绍其它两种获取主键的方式. 为了方便描述我们先建一张my ...

  4. Method com/mysql/jdbc/PreparedStatement.isClosed()Z is abstract 报错解决

    java.lang.AbstractMethodError: Method com/mysql/jdbc/PreparedStatement.isClosed()Z is abstract ----- ...

  5. JDBC PreparedStatement Statement

    参考:预编译语句(Prepared Statements)介绍,以MySQL为例 1. 背景 本文重点讲述MySQL中的预编译语句并从MySQL的Connector/J源码出发讲述其在Java语言中相 ...

  6. JDBC/PreparedStatement

      JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力     JDBC  AP 是Sun公司提供的I 内容:供程序员调用的接口,集成在Java.sql和javax.sql包中, 如 ...

  7. 20160408javaweb之JDBC ---PreparedStatement

    PreparedStatement 1.Sql注入:由于jdbc程序在执行的过程中sql语句在拼装时使用了由页面传入参数,如果用户恶意传入一些sql中的特殊关键字,会导致sql语句意义发生变化,这种攻 ...

  8. 关于JDBC PreparedStatement

    PreparedStatement的执行步骤: 1. 向数据库服务器发送SQL语句,数据库对SQL进行解析和优化(conn.preparedStatement(sql)) 2. 向数据库发送绑定的参数 ...

  9. JavaEE JDBC PreparedStatement

    PreparedStatement @author ixenos PreparedStatement工作原理 注意:虽然mysql不支持PreparedStatement优化,但依然有预编译的实现! ...

  10. JDBC——PreparedStatement执行SQL的对象

    Statement的子接口,预编译SQL,动态SQL 功能比爹强大 用来解决SQL注入的 预编译SQL:参数使用?作为占位符,执行SQL的时候给?赋上值就可以了 使用步骤: 1.导入驱动jar包 复制 ...

随机推荐

  1. PyQt5操作SQLite数据库

    1.操作SQLite数据库import sysfrom PyQt5.QtSql import QSqlDatabase,QSqlQueryfrom PyQt5.QtCore import * def ...

  2. ubuntu-查看所有用户

    cat /etc/shadow :后面的全是用户

  3. splash-简介及入门

    splash 1.      splash简介 Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我 ...

  4. twisted reactor calllater实现

    twisted reactor calllater实现 1.      calllater实现代码 测试源码: from twisted.internet import reactor from tw ...

  5. 吴裕雄 PYTHON 神经网络——TENSORFLOW MNIST读取数据

    from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("E ...

  6. MAC系统 - 系统目录结构

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/smstong/article/detai ...

  7. 字符串操作函数:JSON.parse()与JSON.stringify()的区别,字符串转数组 str.split(','),数组转字符串String(),以及对象拼接合并Object.assign(),数组拼接合并concat()

    1.JSON.parse()  把字符串转化为 json 对象 例如 arr={ , "site":"www.runoob.com" } var obj = J ...

  8. shell脚本部署apache并能通过浏览器访问!

    第一步:导入httpd-2.2.17.tar包 第二步:创建一个test.sh文件(可在/root下) 第三步编写shell脚本 > 会重写文件,如果文件里面有内容会覆盖 >>这个是 ...

  9. [].slice.call(k).filter(function(l) { return l != 0 });

    [].slice.call(k).filter(function(l) { return l != 0 }); 将类数组调用数组方法.

  10. ES建立索引步骤, 1,index 2.mapping 3,别名

    1.建立索引PUT /index_trans_detail 2.建立mappingPOST /index_trans_detail/type_trans_detail/_mapping{ " ...