JDBC(Java DataBase Connectivity,java数据库连接)是⼀种⽤于执⾏SQL语句的Java API。JDBC是Java访问数据库的标准规范,可以为不同的关系型数据库提供统⼀访问,它由⼀组⽤Java语⾔编写的接⼝和类组成。
JDBC需要连接驱动,驱动是两个设备要进⾏通信,满⾜⼀定通信数据格式,数据格式由设备提供商规定,设备提供商为设备提供驱动软件,通过软件可以与该设备进⾏通信。mysql的驱动mysql-connector-java-5.1.37-bin.jar
JDBC规范(掌握四个核⼼对象):
-
DriverManager:用于注册驱动
-
Connection: 表示与数据库创建的连接
-
Statement: 操作数据库sql语句的对象
-
ResultSet: 结果集或⼀张虚拟表
JDBC原理
Java提供访问数据库规范称为JDBC,⽽⽣产⼚商提供规范的实现类称为驱动。
JDBC是接⼝,驱动是接⼝的实现,没有驱动将⽆法完成数据库连接,从⽽不能操作数据库!每个数据库⼚商都需要提供⾃⼰的驱动,⽤来连接⾃⼰公司的数据库,也就是说驱动⼀般都由数据库⽣成⼚商提供。
JDBC入门案例
准备数据:
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/*==============================================================*/
drop table if exists tb_course;
drop table if exists tb_score;
drop table if exists tb_student;
drop table if exists tb_teacher;
/*==============================================================*/
/* Table: tb_course */
/*==============================================================*/
create table tb_course
(
id int not null auto_increment,
name varchar(20),
description varchar(200),
primary key (id)
);
/*==============================================================*/
/* Table: tb_score */
/*==============================================================*/
create table tb_score
(
id int not null auto_increment,
stu_id int,
course_id int,
teacher_id int,
score int,
primary key (id)
);
/*==============================================================*/
/* Table: tb_student */
/*==============================================================*/
create table tb_student
(
id int not null auto_increment,
name varchar(20),
sex char(6),
stu_num varchar(20),
age int,
primary key (id)
);
/*==============================================================*/
/* Table: tb_teacher */
/*==============================================================*/
create table tb_teacher
(
id int not null auto_increment,
name varchar(20),
sex varchar(6),
primary key (id)
);
alter table tb_score add constraint FK_COURSE_SCORE foreign key (course_id)
references tb_course (id) on delete restrict on update restrict;
alter table tb_score add constraint FK_STU_SCORE foreign key (stu_id)
references tb_student (id) on delete restrict on update restrict;
alter table tb_score add constraint FK_Teacher_SCORE foreign key (teacher_id)
references tb_teacher (id) on delete restrict on update restrict;
下面sql语句可以添加数据
insert into tb_course
values
(null, "数学","数学"),
(null, "英语","英语"),
(null, "语文","语文"),
(null, "物理","物理"),
(null, "化学","化学"),
(null, "生物","生物");
insert into tb_teacher VALUES
(null, "石老师", "男"),
(null, "田老师", "女"),
(null, "杨老师", "男"),
(null, "张老师", "女"),
(null, "李老师", "男"),
(null, "马老师", "男");
insert into tb_student VALUES
(null, "刘备","男",1, 50),
(null, "关羽","男",2, 48),
(null, "张飞","男",3, 46),
(null, "貂蝉","女",4, 20),
(null, "杨玉环","女",5,24),
(null, "西施","女",6, 22),
(null, "如花","女",7, 18);
insert into tb_score VALUE
(null, 1, 1, 1 ,80),
(null, 1, 2, 3 ,60),
(null, 2, 1, 3 ,40),
(null, 3, 2, 2 ,70),
(null, 4, 5, 4 ,90),
(null, 3, 1, 1 ,100),
(null, 4, 1, 1 ,89),
(null, 1, 3, 3 ,99),
(null, 1, 4, 3 ,47),
(null, 5, 5, 5 ,90),
(null, 5, 2, 2 ,38),
(null, 6, 1, 4 ,100),
(null, 6, 4, 3 ,89),
(null, 6, 2, 3 ,88),
(null, 6, 4, 1 ,77);
导⼊驱动jar包
创建lib⽬录,存放mysql的驱动mysql-connector-java-5.1.37-bin.jar
选中mysql的jar包,右键选择“ Add as Library...” 完成jar导⼊
链接:https://pan.baidu.com/s/1QQyVFB5bzGnfSlpuC5bB4Q 提取码:r70v
开发步骤
1. 注册驱动.
2. 获得连接.
3. 获得执⾏sql语句的对象
4. 执⾏sql语句,并返回结果
5. 处理结果
6. 释放资源.
案例实现
package JDBCTest;
import org.junit.Test;
import java.sql.*;
public class JDBCOldTest {
@Test
public void JDBCTest() throws ClassNotFoundException, SQLException {
//注册驱动 就是把Driver.class 文件加载到内存
Class.forName("com.mysql.jdbc.Driver");
/**
* 获取连接
* 参数url:需要连接数据库的地址 jdbc:mysql://IP地址:端口号/要连接的数据库名称
* 参数user:连接数据库使用的 用户名
* 参数password:连接数据库使用的 密码
*/
String url = "jdbc:mysql://localhost:3306/test_wensong";
Connection conn = DriverManager.getConnection(url,"root","root");
System.out.println("conn="+conn);
//获取执行sql语句的对象
Statement stat = conn.createStatement();
//执行查询sql语句,并返回结果
String sql = "select * from tb_student";
ResultSet rs = stat.executeQuery(sql);
System.out.println("rs = " + rs);
//处理结果
while(rs.next()){
int age = rs.getInt(5);//通过列的位置获取值,从1开始
String name= rs.getString(2);//通过列的位置获取值,从1开始
System.out.println("name = " + name +" age = " + age);
}
rs.close();
//执行增删改sql语句
//增加一条数据
String sql1 = "INSERT INTO tb_student(name,age,sex,stu_num) values('wensong',27,'男',8)";
stat.executeUpdate(sql1);
//删除数据
String sql2 = "DELETE FROM tb_student WHERE name='如花'";
stat.executeUpdate(sql2);
//修改数据
String sql3 = "UPDATE tb_student SET age=18 where name='杨玉环' ";
stat.executeUpdate(sql3);
//再次查询数据对比操作效果
String sql4 = "select * from tb_student";
ResultSet rs1 = stat.executeQuery(sql4);
System.out.println("rs1 = " + rs1);
//处理结果
while(rs1.next()){
int age = rs1.getInt("age");//通过字段名查询
String name= rs1.getString("name");//通过字段名查询
System.out.println("name = " + name +" age = " + age);
}
//释放资源,不然其他线程无法获取
rs1.close();
stat.close();
conn.close();
}
}