一,什么是JDBC
JDBC操作数据库的步骤
//1.首先在项目根目录创建lib文件夹,放入jdbc驱动程序,然后Add As Library//2.加载数据库驱动
//3.使用驱动管理器来获得连接---获得一个数据库连接对象Connection
//4.使用Connection创建PreparedStatement语句对象---PreparedStatement对象可以执行sql语句
//5.使用PreparedStatement对象执行SQL语句
//6.操作判断--增删改返回的是影响的行数(返回值是int),只有查询获得结果集(返回值ResultSet)
//7.回收资源
二,创建数据库
A.认识数据库
1、数据库:存储数据的仓库。
2、安装数据库:
解压mysql安装包
修改配置文件my.ini(basedir=设置为自己mysql安装的路径;datadir=设置为自己mysql安装的路径\data)
初始化Mysql (mysqld --initialize)
获取初始化之后的Mysql登录密码(mysql安装目录下的DESKTOP-JA5S8TS.err文件中)
安装Mysql服务(mysqld --install)
启动Mysql服务(net start mysql)
使用初始化密码进行登录Mysql (mysql -uroot -p初始化密码)
修改数据加密规则 (alter user 'root'@'localhost' identified by 'password' password expire never)
修改初始化密码 (alter user 'root'@'localhost' identified with mysql_native_password by '新设置的密码')
ctrl+z 关闭再重新用新密码进行登录
查询显示所有数据库信息 (show databases)
3、安装Navicat
注意点:把破解文件复制到Navicat安装目录下
B.数据库的使用
三,JDBC的增删改查
IDEA自动构造代码快捷键:alt+insert
1.方法要定义为无参无返回值的。且测试类的名字不能是Test
2.在方法上使用@Test这个注解
3.光标放在后面,然后使用alt+回车 自动进行导包,选择--Add'JUnit4’to classpath
4.这个方法就不需要依赖main方法就可以直接执行
查,
public void testSelectAll() throws ClassNotFoundException, SQLException {
//JDBC操作数据库的步骤
//1.首先在项目根目录创建lib文件夹,放入jdbc驱动程序,然后Add As Library
//2.加载数据库驱动
//加载数据库驱动
Class.forName(driver);
//使用驱动管理器获得连接,获得一个数据库连接对象Connection
Connection con= DriverManager.getConnection(url,username,password);
//生成方法调用返回值的快捷键:ctrl + alt + v
// 4.使用Connection创建PreparedStatement预处理对象---PreparedStatement对象可以执行带 ? 的sql语句
String sql="select * from student";
PreparedStatement pstm= con.prepareStatement(sql);
//5.使用PreparedStatement对象执行SQL语句,获得ResultSet结果集对象
ResultSet rs= pstm.executeQuery();
//6.操作判断--增删改返回的是影响的行数(返回值是int),只有查询获得结果集(返回值ResultSet)
//定义集合(大的容器),用来装Student对象(小容器)
List<Student> studentList=new ArrayList<>();
//让结果集的游标不断的往下移动,直到没有数据的时候结束循环
while(rs.next()){
//根据字段名称获取表中的数据
int stuId= rs.getInt("stuId");
String stuName= rs.getString("stuName");
String stuSex=rs.getString("stuSex");
int stuAge=rs.getInt("stuAge");
String stuaddr=rs.getString("stuAddr");
//把以上数据封装到Student对象中
Student student=new Student();//一行数据就封装成了一个Student对象
student.setStuId(stuId);
student.setStuName(stuName);
student.setStuSex(stuSex);
student.setStuAge(stuAge);
student.setStuAddr(stuaddr);
//把当前行封装后的Student对象装载到 List集合中
studentList.add(student);
}
System.out.println(studentList);
//7.回收资源,先关闭rs结果集对象 再pstm预处理对象 最后con连接对象
if(rs!=null){
rs.close();
}
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
添加,
public void testAdd() throws ClassNotFoundException, SQLException {
//1.导入驱动包
//2.通过反射加载驱动程序
Class.forName(driver);
//3.通过驱动管理器获得数据库的连接对象
Connection con= DriverManager.getConnection(url,username,password);
//4.通过连接对象,获取SQ预处理对象
String sql="insert into student(stuName,stuSex,stuAge,stuAddr) values(?,?,?,?)";
PreparedStatement pstm= con.prepareStatement(sql);
//实际开发是前端页面传递过来的数据,此处我们直接模拟一组数据
Student student=new Student();
student.setStuName("史艳红");
student.setStuSex("女");
student.setStuAge(18);
student.setStuAddr("开封");
//5.1预处理对象的sql语句有 ? 所以需要进行传参
pstm.setObject(1,student.getStuName());
pstm.setObject(2,student.getStuSex());
pstm.setObject(3,student.getStuAge());
pstm.setObject(4,student.getStuAddr());
//5.2执行更新(增删改都叫做数据库的更新,更新返回的是影响的行数)
int n=pstm.executeUpdate();
//6判断影响的行数 n > 0 表示插入成功,否则插入失败
if(n>0){
System.out.println("插入数据成功");
}else {
System.out.println("插入数据失败");
}
//7释放资源
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}
删除,
public void testDel() throws ClassNotFoundException, SQLException {
//1.导入驱动程序包
//2.通过反射加载驱动程序
Class.forName(driver);
Connection con= DriverManager.getConnection(url,username,password);
String sql="delete from student where stuId=?";
PreparedStatement pstm= con.prepareStatement(sql);
//5.1预处理对象的sql语句有 ? ,所以需要传参
int stuId=3; //实际开发是从前端页面获取要删除的id
pstm.setObject(1,stuId);
//5.2执行更新操作(增删改都是更新操作,返回的结果是影响的行数)
int n=pstm.executeUpdate();
//6判断影响的行数 n>0 表示删除成功,否则删除失败
if(n>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
//7释放资源
if(pstm!=null){
pstm.close();
}
if (con!=null){
con.close();
}
}
修改
@Test
public void testUpdate() throws SQLException, ClassNotFoundException {
//第一步:根据id先查询原始数据
Student student=selectById(4);
System.out.println("修改前:"+student);
//第二步:根据需要修改原始数据的字段值(后期学习是用前端html页面修改数据,然后传输过来新的数据)
Scanner sc=new Scanner(System.in);
System.out.println("请修改名字:");
String newName=sc.next();
student.setStuName(newName);
System.out.println("请修改性别:");
String newSex=sc.next();
student.setStuSex(newSex);
System.out.println("请修改年龄:");
int newAge=sc.nextInt();
student.setStuAge(newAge);
System.out.println("请修改地址:");
String newAddr=sc.next();
student.setStuAddr(newAddr);
System.out.println("修改后:"+student);
//第三步:修改字段值后把最新的数据执行数据库JDBC的修改操作
//1.导入jar包
//2.使用反射加载驱动程序
Class.forName(driver);
//3.使用驱动管理器获得数据库的连接
Connection con = DriverManager.getConnection(url, username, password);
//4.使用连接对象获取SQL的预处理对象
String sql="update student set stuName=?,stuSex=?,stuAge=?,stuAddr=? where stuId=?";
PreparedStatement pstm = con.prepareStatement(sql);
//5.传参并执行SQL
pstm.setObject(1,student.getStuName());
pstm.setObject(2,student.getStuSex());
pstm.setObject(3,student.getStuAge());
pstm.setObject(4,student.getStuAddr());
pstm.setObject(5,student.getStuId());
int n = pstm.executeUpdate();
//6.判断执行结果
if (n>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
//7.释放资源
if (pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}
模糊查询1
@Test
public void testSeach() throws ClassNotFoundException, SQLException {
//1.导入jar包
//2.加载驱动
Class.forName(driver);
//3.获取数据库的连接
Connection con = DriverManager.getConnection(url, username, password);
//编写sql语句
String sql="select * from student where stuName like ?";
//4.获取预处理对象
PreparedStatement pstm = con.prepareStatement(sql);
//5.传参并执行sql
String keyword="r";
pstm.setObject(1,"%"+keyword+"%");
ResultSet rs=pstm.executeQuery();
//6.解析结果集,模糊查询有可能查到很多条数据,所以定义集合储存
List<Student> studentList=new ArrayList<>();
while (rs.next()){
Student student=new Student();
//通过rs根据字段名获取数据库表中的数据,然后把数据封装到Student对象中
student.setStuId(rs.getInt("stuId"));
student.setStuName(rs.getString("stuName"));
student.setStuSex(rs.getString("stuSex"));
student.setStuAge(rs.getInt("stuAge"));
student.setStuAddr(rs.getString("stuAddr"));
//把当前的student对象储存到list集合中
studentList.add(student);
System.out.println(studentList);
}
//7.资源的释放
if(rs!=null){
rs.close();
}
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}
模糊查询2
@Test
public void testSeach1() throws ClassNotFoundException, SQLException {
//1.导入jar包
//2.加载驱动
Class.forName(driver);
//3.获取数据库连接
Connection con= DriverManager.getConnection(url,username,password);
//编写sql语句
String sql="select * from student where stuName like concat('%',?,'%')";
PreparedStatement pstm = con.prepareStatement(sql);
//5.传参并执行sql
String keyword="红"; //后期是通过页面获取用户输入的关键词
pstm.setObject(1,keyword);
ResultSet rs= pstm.executeQuery();
//解析结果集,模糊查询有可能查到很多条数据,所以定义集合存储
List<Student> studentList=new ArrayList<>();
while (rs.next()){
Student student=new Student();
student.setStuId(rs.getInt("stuId"));
student.setStuName(rs.getString("stuName"));
student.setStuSex(rs.getString("stuSex"));
student.setStuAge(rs.getInt("stuAge"));
student.setStuAddr(rs.getString("stuAddr"));
//把当前的student对象存储到list集合中
studentList.add(student);
}
//打印输出
System.out.println(studentList);
//7.资源的释放
if(rs!=null){
rs.close();
}
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}