包结构:

第一步:编写获取连接工具类

package com.atguigu.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; /**
* JDBC 操作的工具类
*/
public class JdbcUtils { public static void close(ResultSet resultSet){
try {
if(resultSet != null){
resultSet.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} //关闭数据库资源
public static void close(Connection connection){
try {
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} public static void close(Statement statement){
try {
if(statement != null){
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} //根据配置文件获取数据库连接
public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException{
Connection connection = null; //0. 读取 Properties 文件
Properties properties = new Properties();
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in); //1. 准备连接数据库的四个基本信息:
String driverClassName = properties.getProperty("jdbc.driverClass");
String url = properties.getProperty("jdbc.jdbcUrl");
String user = properties.getProperty("jdbc.user");
String password = properties.getProperty("jdbc.password"); //2. 加载驱动
Class.forName(driverClassName); //3. 调用 DriverManager.getConnection(url, user, password) 获取连接
connection = DriverManager.getConnection(url, user, password); return connection;
} }

第二步:执行插入操作

public class JdbcTest1 {
//向数据表插入一条记录
@Test
public void testInsert() throws ClassNotFoundException, SQLException, IOException{
//1. 编写一条 SQL 语句:
String sql = "INSERT INTO users2 (username, password) VALUES('tom','2345')"; //2. 获取连接
Connection connection = JdbcUtils.getConnection(); //3. 执行 SQL 需要借助于 Statement 接口
//3.1 调用 Connection#createStatement() 创建 Statement 对象
Statement statement = connection.createStatement(); //3.2 执行 SQL
statement.execute(sql); //4. 关闭数据库资源
statement.close();
connection.close();
}
}
=========================================
/**
* 无论是否出现异常, 都必须保证关闭数据库资源!
* 使用 try catch finallay. 在 finally 中关闭数据库资源
*/
@Test
public void testInsert2(){
Connection connection = null;
Statement statement = null; try {
//1. 编写一条 SQL 语句:
String sql = "INSERT INTO users2 (username, password) VALUES('tom','2345')"; //2. 获取连接
connection = JdbcUtils.getConnection(); //3. 执行 SQL 需要借助于 Statement 接口
//3.1 调用 Connection#createStatement() 创建 Statement 对象
statement = connection.createStatement(); //3.2 执行 SQL
statement.execute(sql); } catch (Exception e) {
e.printStackTrace();
} finally{
//4. 关闭数据库资源
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
=========================================== /*
* 若仅仅是执行一条已经准备好的 SQL, 使用 Statement OK!
* 开发时, 不太可能使用完全实现准备好的 SQL! SQL 中的部分开能需要动态传入.
*/
//调用 JdbcUtils 来关闭数据库资源
@Test
public void testStatement(){
Connection connection = null;
Statement statement = null; try {
//1. 编写一条 SQL 语句:
String sql = "INSERT INTO users2 (username, password) VALUES('tom','2345')"; //2. 获取连接
connection = JdbcUtils.getConnection(); //3. 执行 SQL 需要借助于 Statement 接口
//3.1 调用 Connection#createStatement() 创建 Statement 对象
statement = connection.createStatement(); //3.2 执行 SQL
statement.execute(sql); } catch (Exception e) {
e.printStackTrace();
} finally{
//4. 关闭数据库资源
JdbcUtils.close(statement);
JdbcUtils.close(connection);
}
}
==========================================================
//关于 Statement 做了解即可.
//用户名和密码从控制台进行动态输入
//拼接 SQL 字符串不靠谱!
//1. 麻烦. 2. 还会有 SQL 注入的问题.
@Test
public void testStatement2(){
Connection connection = null;
Statement statement = null; Scanner scanner = new Scanner(System.in); System.out.print("username:");
String username = scanner.nextLine(); System.out.println("password:");
String password = scanner.nextLine(); try { //1. 编写一条 SQL 语句:
String sql = "INSERT INTO users (username, password) VALUES('"
+ username + "','" + password + "')"; //2. 获取连接
connection = JdbcUtils.getConnection(); //3. 执行 SQL 需要借助于 Statement 接口
//3.1 调用 Connection#createStatement() 创建 Statement 对象
statement = connection.createStatement(); //3.2 执行 SQL
statement.execute(sql); } catch (Exception e) {
e.printStackTrace();
} finally{
//4. 关闭数据库资源
JdbcUtils.close(statement);
JdbcUtils.close(connection);
}
}
=========================================================
/**
* PreparedStatement 可以解决 Statement 的问题
* 1. 不再需要拼接 SQL 字符串
* 2. 可以解决 SQL 注入的问题.
*/
@Test
public void testPreparedStatement(){
Connection connection = null;
PreparedStatement preparedStatement = null; Scanner scanner = new Scanner(System.in); System.out.print("username:");
String username = scanner.nextLine(); System.out.println("password:");
String password = scanner.nextLine(); try { //1. 编写一条 SQL 语句, 使用 ? 作为占位符. 所以就可以不拼 SQL 串了.
String sql = "INSERT INTO users (username, password) VALUES(?,?)"; //2. 获取连接
connection = JdbcUtils.getConnection(); //3. 调用 Connection#prepareStatement(sql) 创建 PreparedStatement 对象
preparedStatement = connection.prepareStatement(sql); //4. 调用 PreparedStatement 的 setXxx 方法来填充占位符
preparedStatement.setString(1, username);
preparedStatement.setString(2, password); //3.2 执行 SQL. 调用 execute() 方法. 而不能再调用 Statement 的 execute(sql) 方法
preparedStatement.execute(); } catch (Exception e) {
e.printStackTrace();
} finally{
//4. 关闭数据库资源
JdbcUtils.close(preparedStatement);
JdbcUtils.close(connection);
}
}
==========================================================
jdbc.properties路径:/jdbc-1/src/jdbc.properties
内容:
#连接MySQL
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/jdbc1 #连接Oracle
#jdbc.user=scott
#jdbc.password=tiger
#jdbc.driverClass=oracle.jdbc.driver.OracleDriver
#jdbc.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:ORCL001

使用PrepareStatement的更多相关文章

  1. preparestatement可以避免注入

    之所以PreparedStatement能防止注入,是因为它把单引号转义了,变成了\',这样一来,就无法截断SQL语句,进而无法拼接SQL语句,基本上没有办法注入了. 不使用这个,我们一般做查询或更新 ...

  2. 可输出sql的PrepareStatement封装

    import java.io.InputStream; import java.io.Reader; import java.net.URL; import java.sql.Connection; ...

  3. 预处理prepareStatement是怎么防止sql注入漏洞的?

    序,目前在对数据库进行操作之前,使用prepareStatement预编译,然后再根据通配符进行数据填值,是比较常见的做法,好处是提高执行效率,而且保证排除SQL注入漏洞. 一.prepareStat ...

  4. prepareStatement与Statement的区别

    prepareStatement与Statement的区别 1.区别: 转 http://blog.csdn.net/zsm653983/article/details/7296609 stmt=co ...

  5. JDBC中prepareStatement 和Statement 的区别

    package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedSta ...

  6. 加载执行预编译的Sql :prepareStatement

    1.获得连接:Connection con = null; con = DBUtil.getConnection(); 2.写sql语句:String sql=""; 3.用连接加 ...

  7. prepareStatement和Statement的区别

    1:创建时的区别:    Statement stm=con.createStatement();    PreparedStatement pstm=con.prepareStatement(sql ...

  8. prepareStatement的用法和解释

    1. PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程2. 使用 Statement 对象.在对数据库只执行一次性存取的时侯,用 Statement ...

  9. MySQL PrepareStatement基本的两种模式&客户端空间占用的源码分析

    关于预编译(PrepareStatement),对于所有的JDBC驱动程序来讲,有一个共同的功能,就是“防止SQL注入”,类似Oracle还有一种“软解析”的概念,它非常适合应用于OLTP类型的系统中 ...

  10. JDBC连接数据库 prepareStatement

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

随机推荐

  1. UWP实现吸顶的Pivot

    话不多说,先上效果 这里使用了一个ScrollProgressProvider.cs,我们这篇文章先解析一下整体的动画思路,以后再详细解释这个Provider的实现方式. 结构 整个页面大致结构是 & ...

  2. python 编码报错问题 'ascii' codec can't encode characters 解决方法

    python在安装时,默认的编码是ascii, 当程序中出现非ascii编码时,python的处理常常会报这样的错 'ascii' codec can't encode characters pyth ...

  3. Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇)

    目录 Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇) 1 Optimizing Data Size 2 Optimizing MySQL Data Types 3 Optimizing ...

  4. net必问的面试题系列之基本概念和语法

    上个月离职了,这几天整理了一些常见的面试题,整理成一个系列给大家分享一下,机会是给有准备的人,面试造火箭,工作拧螺丝,不慌,共勉. 1.net必问的面试题系列之基本概念和语法 2.net必问的面试题系 ...

  5. 解读BloomFilter算法(转载)

    1.介绍 BloomFilter(布隆过滤器)是一种可以高效地判断元素是否在某个集合中的算法. 在很多日常场景中,都大量存在着布隆过滤器的应用.例如:检查单词是否拼写正确.网络爬虫的URL去重.黑名单 ...

  6. 渗透之路基础 -- SQL注入

    目录 mysql注入(上) limit 有两个参数 limit 2,3 表示从2开始查3条 通过MySql内置数据库获取表名 通过MySql内置数据库获取表名对应的列名 mysql注入(中) SQL常 ...

  7. .NETCore Docker实现容器化与私有镜像仓库管理

    一.Docker介绍 Docker是用Go语言编写基于Linux操作系统的一些特性开发的,其提供了操作系统级别的抽象,是一种容器管理技术,它隔离了应用程序对基础架构(操作系统等)的依赖.相较于虚拟机而 ...

  8. wcf项目跨域问题处理

    最近做了一个wcf项目,请求发起的项目是一个webform项目,所以这是分开的两个项目端口必然不一样,理所当然存在跨域问题. 有的人当下就反应过来jsonp,jsonp只能用于get请求,对于参数比较 ...

  9. 车联网服务non-RESTful架构改造实践

    导读 在构建面向企业项目.多端的内容聚合类在线服务API设计的过程中,由于其定制特点,采用常规的restful开发模式,通常会导致大量雷同API重复开发的窘境,本文介绍一种GraphQL查询语言+网关 ...

  10. Unity Editor已停止工作

    在更换系统之后,可能会出现打开刚安装好的Unity,显示Unity Editor已停止工作,这时候我们考虑是系统win7的问题.可以在原系统上升级,也可以重新安装,升级.文中所涉及到的软件,可在右侧加 ...