JDBC总结
JDBC的全称为:java database connection (java和 数据库的连接 ) 就是使用java代码来操作数据库。
Sun公司为了简化、统一对数据库的操作,定义了一套java操作数据库的接口的规范,称之为JDBC。
Java程序和数据库是两个不同的程序,他们是怎么进行访问的?
java程序想和数据库进行交互,也必须得安装数据库驱动,这样才能交互。但是,我们数据库有多种,这样就会导致不同的数据库具备不同的数据库驱动。
从而会导致在开发中作为程序员由于安装不同的数据库驱动而浪费很多时间,同时和数据库交互的java代码也得重新书写,这样都会导致效率很低。所以说,sun公司就会制定一套规则,这套规则就是用来java程序连接数据库的,然后各大数据库厂商只需要实现这个规则即可。这个规则就是jdbc技术,即接口。
换句话就是说,就是数据库厂商使用sun公司提供的接口,然后作为java程序员实现接口中的方法即可。接口中的方法体由具体的数据库厂商来实现。
JDBC的常用类和接口
有关的类都在java.sql和javax.sql(扩展包)下
DriverManager : 数据库驱动管理类。这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取Connection接口;
Connection: 是一个接口, 建立数据库连接的一个接口。作用:建立数据库和java代码之间的连接。
Statement(接口)、PreparedStatement(接口)(解决安全隐患问题,比如sql注入的问题)、CallbackStatement(操作存储过程,oracle会讲解): 数据库操作,向数据库发送sql语句。
ResultSet(接口): 结果集。 Statement 发送sql语句,得到的结果 封装在 ResultSet 中。
JDBC的CRUD(增删改查)
create(增加) read(查) update(更改) delete (删除)
查询
需求:查询user表中的所有用户数据。
public static void main(String[] args) {
//获取连接
Connection con = null;
//创建statement对象
Statement statement = null;
ResultSet rs = null;
try {
// 1.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/day04_db”, “root”, “123”);
statement = con.createStatement();
//发送并执行sql
String sql = “select * from user”;
rs = statement.executeQuery(sql);
//遍历
while(rs.next()){
String id = rs.getInt(“id”);
String name = rs.getString(2);
int pwd= rs.getString(3);
System.out.println(id+"+++"+name+"++++"+pwd);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// 6 释放资源
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
添加
需求:向数据库中插入一条新的数据。
public static void main(String[] args) {
//获取连接
Connection connection = null;
//创建statement对象
Statement statement = null;
try {
//注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/day04_db”, “root”, “123”);
statement = connection.createStatement();
//发送sql
String sql = “insert into user values(null, ‘zhaoliu’, ‘abc’)”;
int update = statement.executeUpdate(sql);
System.out.println(“插入了”+update+“条数据”);
//释放资源
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// 6 释放资源
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
}
修改
需求:将zhaoliu 修改成 tianqi。
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
- 1
- 2
- 3
- 4
//注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
// 获得连接
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/day04_db”, “root”, “123”);
// 获得发送sql的对象
stmt = conn.createStatement();
// 执行sql 获得结果
String sql = “update user set username=‘tianqi’ where username=‘zhaoliu’”;
int sum = stmt.executeUpdate(sql);
// 处理结果
System.out.println(sum);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6 释放资源
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
}
删除
需求:删除id为4的用户。
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
- 1
- 2
- 3
- 4
//注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
// 获得连接
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/day04_db”, “root”, “123”);
// 获得发送sql的对象
stmt = conn.createStatement();
// 执行sql 获得结果
String sql = “delete from user where id=4”;
int sum = stmt.executeUpdate(sql);
// 处理结果
System.out.println(sum);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6 释放资源
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
}
JDBC工具类抽取
主要是应对数据库信息改变之后,修改代码的麻烦
先将数据库信息提取到配置文件,然后读取配置文件就可以了
SQL注入问题
无非是在参数中设置一些特殊字符,使SQL语句在拼接这些参数的时候因为特殊字符的原因改变了SQL语句原来的规则。
解决方案
使用PreparedStatement 解决SQL注入问题,运行在SQL中参数以 ? 占位符的方式表示。
PreparedStatement解决步骤:
PreparedStatement是Statement的子接口,可以防止sql注入问题。可以通过Connection接口中的prepareStatement(sql)方法获得PreparedStatement的对象。
prepareStatement(String sql) 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
注意:sql提前创建好的。sql语句中需要参数。使用?进行占位。
举例:
select *from user where username=‘zhangsan’ and password = ‘123456’;
使用?进行占位
select *from user where username=? and password = ?;
String sql=”select *from user where username=? and password = ?”;
步骤一:PreparedStatement pstmt = conn.prepareStatement(sql); -----需要你事先传递sql。如果sql需要参数,使用?进行占位。
步骤二:设置参数(执行sql之前):pstmt.setXXX(int index, 要放入的值) -----根据不同类型的数据进行方法的选择。第一个参数index表示的是?出现的位置。从1开始计数,有几个问号,就需要传递几个参数。
方法的参数说明:
第一个参数:int index ;表示的是问号出现的位置。 问号是从1开始计数
第二个参数:给问号的位置传入的值。
步骤三、执行,不需要在传递sql了。
pstmt.executeQuery();—执行select
pstmt.executeUpdate();—执行insert,delete,update