PreparedStatement接口是Statement接口的子接口,使用它的好处有三个

一:简化代码,便于sql语句的书写

二:有效的禁止sql语句的注入,例如:用户名和密码,使用PreparedStatement接口的方法,可防止不正确的输入登陆成功,提高

数据库系统的安全性

三:最大可能的提高了效率

代码如下:

package com.lanqiao.javatest;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

import com.mysql.jdbc.Statement;

/*
* preparedStatement是Statement的子接口,好处一:可实现sql语句的便捷写法
* */
public class Test1 {

public void testPreparedStatement() throws Exception{
Connection connection=null;
PreparedStatement preparedstatement=null;
try {
connection=getConnection();
String sql="insert into table12 (id,name,email,birth) values(?,?,?,?)";
preparedstatement=connection.prepareStatement(sql);
preparedstatement.setInt(1, 8);
preparedstatement.setString(2, "liquan");
preparedstatement.setString(3, "fsdf");
preparedstatement.setDate(4, new Date(new java.util.Date().getTime()));

//获取实时时间的方法Date date=new Date(new java.util.Date().getTame);

preparedstatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(preparedstatement!=null){
preparedstatement.close();
}
if(connection!=null){
connection.close();
}
}
}
public Connection getConnection() throws Exception{
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;

InputStream in=Test1.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);

driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");

Driver driver=(Driver)Class.forName(driverClass).newInstance();
Properties info=new Properties();
info.put("user", "root");
info.put("password", "lxn123");
Connection connection=driver.connect(jdbcUrl, info);
return connection;
}

public void testConnection() throws Exception{
System.out.println(getConnection());
}
@Test
//好处二:作用:有效的禁止sql注入,输入正确的用户名和密码才能登陆;
//就是防止错误的用户名和密码,实现数据库的安全性
public void testSQL() throws Exception{
// String userName="a' or password=";
// String password="or '1'='1";

String userName="lxn";
String password="lxn123";

String sql="SELECT * FROM table1 WHERE userName=? AND PASSWORD=?";
System.out.println(sql);

Connection connection=null;
PreparedStatement preparedstatement=null;
ResultSet resultset=null;
try {
connection=getConnection();
preparedstatement=connection.prepareStatement(sql);

preparedstatement.setString(1, userName);
preparedstatement.setString(2, password);;

resultset=preparedstatement.executeQuery();
if(resultset.next()){
System.out.println("登陆成功!!!");
}
else{
System.out.println("登陆失败!!!");
}
} catch (Exception e) {

}finally{
if (resultset!=null) {
resultset.close();
}
if (preparedstatement!=null) {
preparedstatement.close();
}
if (connection!=null) {
connection.close();
}
}
}

//好处三:最大可能的提高效率

}

PreparedStatement接口及其方法的使用的更多相关文章

  1. jdbc java数据库连接 4)PreParedStatement接口 之 区别和例子

    Statement 和 PreparedStatement 的区别: 1)语句不同 PreparedStatement需要预编译以及需要参数 2)由于PreparedStatement有缓存区,所以效 ...

  2. JDBC的使用(二):PreparedStatement接口;ResultSet接口(获取结果集);例题:SQL注入

    ResultSet接口:类似于一个临时表,用来暂时存放数据库查询操作所获得的结果集. getInt(), getFloat(), getDate(), getBoolean(), getString( ...

  3. Java数据库——PreparedStatement接口

    PreparedStatement接口是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,是先在数据表中准备好了一条SQL语 ...

  4. MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. JDBC数据库编程:PreparedStatement接口

    使用PreparedStatement进行数据库的更新及查询操作. PreparedStatement PreparedStatement是statement子接口.属于预处理. 使用statemen ...

  6. 为什么类和接口不能使用private和protected?接口的方法不能使用private、protected、default

    对于java程序员来说,java的访问权限修饰词public.protected.default.private的区别和使用肯定都不是问题,这里也不再啰嗦了,反正度娘一搜就一大把.最近在整理java ...

  7. java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)

    interface接口参数 jdk1.7及以前使用spring功能实现的: 注意: 1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名. public static void test() ...

  8. java 28 - 7 JDK8的新特性 之 接口可以使用方法

    JDK8的新特性: http://bbs.itcast.cn/thread-24398-1-1.html 其中之一:接口可以使用方法 interface Inter { //抽象方法 public a ...

  9. Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例

    利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...

随机推荐

  1. Java基础之读文件——使用通道随机读写文件(RandomReadWrite)

    控制台程序,使用通道随机读写primes_backup.bin文件. import static java.nio.file.StandardOpenOption.*; import java.nio ...

  2. php4.3.4.4、apache2.0.4.8、mysql 4.0.26、window7 配置过程

    apache的安装不需要过程,直接默认安装,下一步 下一步就Ok了. php4的安装: 1 将php-4.0.4-Win32.zip(最新版本4.0.4)解压缩到硬盘的一个目录中,例如解压到E:php ...

  3. Java实现数组按数值大小排序

    package shb.java.test; /** * 比较数组中元素的大小,按从大到小顺序排列. * @Package:shb.java.test * @Description: * @autho ...

  4. 供应商 银行 SQL (转自ITPUB)

    在此记录一下自己学习过程.新手,请多多指教,谢谢. 最近客户有需求,找出供应商对应的银行信息,查看了下网上帖子,发现都是从供应商及供应商地点层发起,去查找对应的银行信息,但是,供应商维护银行界面共有四 ...

  5. SLC、MLC和TLC三者的区别

    SLC=Single-LevelCell,即1bit/cell,速度快寿命长,价格超贵(约MLC3倍以上的价格),约10万次擦写寿命 MLC=Multi-LevelCell,即2bit/cell,速度 ...

  6. 如何设置DB2I(SPUFI)来正常工作

    首先确定你现在所使用的登录proc,确保有权限可以在对应的PDS内新建member,可以在s.st里面找userid对应的job,然后去serach using,基本可以找到对应的dataset 用t ...

  7. 八、Java基础---------基本语法

    一.学习Java注意的细节:     1.1 Java语言拼写上严格区分大小写: 1.2 一个Java源文件里可以定义多个Java类,但其中最多只能有一个类被定义成public类: 1.3 若源文件中 ...

  8. 精通 JS正则表达式

    一.正则表达式可以: 测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 替换文本.可以在文档中使用一个正则表达式 ...

  9. SuperSocket架构设计示意图【转】

    转自:http://docs.supersocket.net/v1-6/zh-CN/Architecture-Diagrams 中文(中国)Toggle Dropdown v1.6Toggle Dro ...

  10. printf,sprintf,vsprintf 区别【转】

    转自:http://blog.csdn.net/anye3000/article/details/6593551 有C语言写作历史的程序员往往特别喜欢printf 函数.即使可以使用更简单的命令(例如 ...