概念
Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法,可以使用 Java 语言连接数据库完成增删改查的操作。
JDBC API
JDBC是由多个接口和类进行功能实现
类
1.java.sql.DriverManager
管理多个数据库驱动类,提供了获取数据库连接的方法
2.java.sql.SQLException
处理数据库应用程序时所发生的异常
接口
1.java.sql.Connection
代表一个数据库连接(当connection不是null时,表示已连接数据库)
2.java.sql.Statement
发送SQL语句到数据库工具
3.java.sql.ResultSet
保存SQL查询语句的结果数据(结果集)
JDBC开发步骤
1.加载驱动
Class.forName("com.mysql.jdbc.Driver");//加载驱动
2.创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc?useUnicode=true&characterEncoding=utf8", "root","123");
3.创建语句对象
Statement statement = conn.createStatement();
4.执行sql语句对象
int i = statement.executeUpdate("insert into book (isbn,name,price,stock) values('00100','三国演义',20.0,1)");
5.处理结果
if(i>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
6.释放资源
if(statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
案例
public static void main(String[] args) throws Exception{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获得连接对象
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc","root","123");
//3.获得执行SQL的对象
Statement statement = connection.createStatement();
//4.执行SQL语句,并接收结果
int result = statement.executeUpdate("delete from book where id = '5';");
//5.处理结果
if(result==1){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
//6.释放资源
statement.close();
connection.close();
}
ResultSet(结果集)
接收结果集
ResultSet rs= statement.executeQuery("select * from book;");
遍历 ResultSet 中的数据
-
数据行指针:初始位置在第一行数据前,每调用一次 boolean next()方法ResultSet 的指针向下移动一行,结果为 true,表示当前行有数据。
-
rs.getXxx(整数):代表根据列的编号顺序获得,从 1 开始
-
rs.getXxx(“列名”):代表根据列名获得
案例
根据列的名称获取
public static void main(String[] args) {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "123");
//3.获取发送 sql 语句对象
Statement statement = connection.createStatement();
//4.执行 SQL 语句并接收结果集
ResultSet resultSet = statement.executeQuery("select * from book");
//5 处理结果集
while(resultSet.next()){
//5.1有数据,依据列名获取数据
String id = resultSet.getString("id");
String name = resultSet.getString("name");
int min_Price = resultSet.getInt("min_Price");
int max_Price = resultSet.getInt("max_Price");
System.out.println(id+"\t"+name+"\t"+min_Price+"\t"+max_Price);
}
//6.释放资源
rs.close();
statement.close();
connection.close();
}
根据列的编号获取数据
public static void main(String[] args) {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "123");
//3.获取发送 sql 语句对象
Statement statement = connection.createStatement();
//4.执行 SQL 语句并接收结果集
ResultSet resultSet = statement.executeQuery("select * from book");
//5 处理结果集
while(resultSet.next()){
//5.2有数据,依据列的编号获取数据
String id = resultSet.getString(1);
String name = resultSet.getString(2);
int min_Price = resultSet.getInt(3);
int max_Price = resultSet.getInt(4);
System.out.println(id+"\t"+name+"\t"+min_Price+"\t"+max_Price);
}
//6.释放资源
rs.close();
statement.close();
connection.close();
}
SQL注入
SQL注入概念
用户输入的数据中有 SQL 关键字或SQL语法导致 SQL 语句编译后的条件为 true,一直得到正确的结果,信息容易被泄露,这种现象称为 SQL 注入
避免 SQL 注入
为了避免 SQL 注入的问题,我们要使 SQL 语句在用户输入数据前就已进行编译成完整的 SQL 语句,再进行填充数据(预编译)。
PreparedStatement的应用
- 预编译SQL 语句,效率高。
- 安全,避免SQL注入 。
- 可以动态的填充数据,执行多个同构的 SQL 语句
//1.预编译 SQL 语句
PreparedStatement pstmt = conn.prepareStatement("select * from user where username=? and password=?");
//2.为参数下标赋值
pstmt.setString(1,username);
pstmt.setString(2,password);