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. AVAudioPlayer init 报错: Error Domain=NSOSStatusErrorDomain Code=1937337955

    Error Domain=NSOSStatusErrorDomain Code=1937337955 原因: 音频文件的格式不规范导致 对于iOS7以上的系统(含iOS7),在确定文件格式的情况下可以 ...

  2. transient关键字的含义

    transient java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当 ...

  3. eclipse Project facet Java version 1.8 is not supported.

    在移植eclipse项目时,如果遇到 “Project facet Java version 1.7 is not supported.” 项目中的jdk1.7不支持.说明项目是其他版本jdk编译的, ...

  4. Java基础(53):内部类(转)

    java中的内部类总结 内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行 ...

  5. .NET: 防止多个应用程序同时开

    用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...

  6. ofbiz进击 第二节。 control 理解与创建

    首先要说的是,学习ofbiz,要去http://ofbiz.apache.org/官网里面,去看右边菜单里   Management Apps  的例子,然后找到类似的页面,去看调用的源码方法. co ...

  7. android studio adb

    bogon:platform-tools alamps$ echo $HOME /Users/alamps bogon:platform-tools alamps$ echo $PATH /usr/l ...

  8. 0422 数学口袋精灵app

    首先要部署这个app项目就是第一步: 一.前提下载并安装JDK 在线图解:手把手教你安装JDK      http://www.lvtao.net/server/windows-setup-jdk.h ...

  9. Android 发送短信与接收短信

    package com.example.testsms; import android.app.Activity; import android.app.PendingIntent; import a ...

  10. java mvc控制器基本传值方式

    控制器----- @RequestMapping(value = "MatchDetail", method = RequestMethod.GET) public ModelAn ...