PreparedStatement接口及其方法的使用
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接口及其方法的使用的更多相关文章
- jdbc java数据库连接 4)PreParedStatement接口 之 区别和例子
Statement 和 PreparedStatement 的区别: 1)语句不同 PreparedStatement需要预编译以及需要参数 2)由于PreparedStatement有缓存区,所以效 ...
- JDBC的使用(二):PreparedStatement接口;ResultSet接口(获取结果集);例题:SQL注入
ResultSet接口:类似于一个临时表,用来暂时存放数据库查询操作所获得的结果集. getInt(), getFloat(), getDate(), getBoolean(), getString( ...
- Java数据库——PreparedStatement接口
PreparedStatement接口是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,是先在数据表中准备好了一条SQL语 ...
- MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- JDBC数据库编程:PreparedStatement接口
使用PreparedStatement进行数据库的更新及查询操作. PreparedStatement PreparedStatement是statement子接口.属于预处理. 使用statemen ...
- 为什么类和接口不能使用private和protected?接口的方法不能使用private、protected、default
对于java程序员来说,java的访问权限修饰词public.protected.default.private的区别和使用肯定都不是问题,这里也不再啰嗦了,反正度娘一搜就一大把.最近在整理java ...
- java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)
interface接口参数 jdk1.7及以前使用spring功能实现的: 注意: 1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名. public static void test() ...
- java 28 - 7 JDK8的新特性 之 接口可以使用方法
JDK8的新特性: http://bbs.itcast.cn/thread-24398-1-1.html 其中之一:接口可以使用方法 interface Inter { //抽象方法 public a ...
- Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例
利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...
随机推荐
- C++ note
主要是为了学习c++的类和对象 内容摘自 c++概述 http://see.xidian.edu.cn/cpp/biancheng/cpp/rumen_1/ 1,变量 ,C++中,我们可以在 ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- mysql 查看是否存在某一张表
判断表是否存在 SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname'; 或者 SHOW TABLE ...
- int和long long有符号整形 负数比正数多一个
int的负数比正数多一个,则有一个负数在int范围内没有对应的正数 最大正整数用十六进制,很容易表示:0x7f ff ff ff int num = 0x7fffffff; num = -num; p ...
- [转]数据库高可用架构(MySQL、Oracle、MongoDB、Redis)
一.MySQL MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换 服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失 ...
- Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法
Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7 :Apatana Studio3+ pydev2.2+python2.7 run时设置 run--&g ...
- POJ 3356 AGTC(DP-最小编辑距离)
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- Openssl生成根证书、服务器证书并签核证书
1.修改Openssl配置文件CA目录: cat /etc/pki/tls/openssl.cnf dir = /etc/pki/CA 2.生成根证书及私钥: #http://www.haiyun.m ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON DispCross
zw版[转发·台湾nvp系列Delphi例程]HALCON DispCross procedure TForm1.Button1Click(Sender: TObject);var r, c : Ol ...
- Debian下配置网络的方法
1.网络配置 配置网卡修改 /etc/network/interfaces 添加如下 # #号后面是备注,不要添加哦! auto eth0 #开机自动激活 iface eth0 inte static ...