前言
本文适合有数据库基础的读者阅读
一、JDBC是什么?
Java Database Connectivity,简称JDBC,就是Java数据库连接,是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。
1.jdbc的本质
jdbc的本质就是一套接口,也就是说使用jdbc是面向接口编程
2.为什么要面向接口编程?
目的就是解耦合
二、jdbc的六个步骤
1.注册数据库驱动(告诉java要使用哪个数据库(mysl,oracle…))
2.连接数据库驱动
3.创建数据库的操作对象
4.执行sql语句
5.处理查询的结果
6.释放资源
三、六步骤详解
1.注册数据库
调用Class.forName(),此时JVM会执行该类的静态代码段
public class Test1 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2.获取连接(根据自己数据库填写)
//根据自己的数据库进行配置
try {
connection = DriverManager.getConnection("jdbc:mysql" + "://localhost:3306" + "/teamblog", "root","123456");
} catch (SQLException e) {
e.printStackTrace();
}
3.获取操作对象
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
4.写sql语句
String sql = "select * from book";
5.处理结果集
try {
resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
while (true){
try {
if (!resultSet.next()) break;
} catch (SQLException e) {
e.printStackTrace();
}
try {
System.out.println(resultSet.getString(1));
} catch (SQLException e) {
e.printStackTrace();
}
}
6.释放资源
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
7.编译得到结果
1
3
5
通过Datagrip可以对数据库进行处理,但不建议新手直接使用,这里强推一波Datagrip,比Navicat好用的多(jetyyds)
通过这六步就完成了一个基本的jdbc连接数据库的操作,但是有一个问题值得我们去思考,假如有一个登录系统,假如输入如下的代码,就会出现一个致命漏洞
select * from users where username='admin'#' and password=md5('')
相信聪明的你已经发现了问题所在,这就是曾经有名的sql注入,据说以前一旦有人利用sql注入,就会被锁ip,不过现在已经基本不存在这个问题了,处理方式如下
1.此时就不再是Statement statement,而是PreparedStatement statement
同时我们需要先写部分sql语句
String sql = "select * from book where id = ?";
2.对sql语句进行预加载
try {
statement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
3.对占位符进行赋值
try {
statement.setInt(1,1);
} catch (SQLException e) {
e.printStackTrace();
}
4.处理结果集,注意不能在括号里面加sql不然sql语句会重新编译然后抛出异常
try {
resultSet = statement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
这样就完美的解决了sql注入的问题
结果:
1
四、封装成函数
由于jdbc的代码又臭又长,可以将重复的代码进行方法的封装,要用直接调用就行了
package Test;
import java.sql.*;
/*
*@Author:snow_1020
*@Data:
*@Aim:
*@Return:
*@Github:snow_1020
*/
public class JdbcUtils {
private JdbcUtils(){
}
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection GetConnection() throws Exception {
return DriverManager.getConnection("jdbc:mysql" + "://localhost:3306" + "/teamblog",
"root","123456");
}
public static void Close(Connection conn, ResultSet rs, Statement ps){
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
五、mybatis框架
在大学中,老师不教框架,老师只教jdbc,sevlet,像mybatis,spring,springMvc,springboot等等框架一律不教,其实运用框架可以省去大量的重复代码,同时会非常的方便,mybatis框架就是jdbc的升级版,后期有空会出mybatis框架的运用
六、总结
如果你也心怀梦想,如果你也想变强,如果你热爱开发,无论是游戏开发,web开发,app开发,just do it,你也可以加入我们,我作为一个非常普通的大一学生,虚心接受你们的指导,谢谢观看。