1. 学会搭建jdbc环境并熟悉学习流程
- JDBC的学习流程:JDBC学习流程精讲
2. 在数据库下建表并测试语句
CREATE TABLE student (
stu_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
stu_name varchar(20) NOT NULL,
stu_sex char(2) NOT NULL,
stu_birthday date NOT NULL,
stu_user varchar(50) NOT NULL,
stu_password varchar(50) NOT NULL
)
INSERT INTO student VALUES (1, '李华', ' 男', '2000-03-06', 'hhh', '123456')
INSERT INTO student VALUES (DEFAULT, '李华', ' 男', '2000-03-06', 'hhh', '123456')
结果:
UPDATE student SET stu_password=12345 WHERE stu_id=2
结果:
DELETE FROM student WHERE stu_id=2
结果:
SELECT * FROM student
结果:
3. 在IDEA中操作数据库
-
步骤
- 加载驱动
- 连接数据库DriverManager
- 获得执行sql的对象PreparedStatement
- 获得返回的结果集ResultSet
- 释放连接
-
增删改ps.executeUpdate()返回一个int值,因此不需要ResultSe集接判断是否大于0即可判断是否成功
-
查询需要用ResultSet集接收,rs.next(类似于指针)
四个操作
package com.mystudy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Insert {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
//forName抛出异常 ClassNotFoundException
Class.forName("com.mysql.jdbc.Driver");
//使用DriverManager来判断数据库
//getConnection抛出异常 SQLException
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?useSSL=true","root","root");
//创建SQL语句 插入数据
//这里用的是Nacivat Premium数据库,
//插入语句可以先在数据库中测试,再放入下方
String sql="INSERT INTO student VALUES (DEFAULT, '李华', ' 男', '2000-03-06', 'hhh', '123456')";
//创建预处理对象
PreparedStatement ps= conn.prepareStatement(sql);
//ps.executeUpdate()返回一个int值,用resultint接住
int resultint=ps.executeUpdate();
//判断resultint是否大于0,即可洞察是否插入成功
//这里加个判断语句
if (resultint>0){
System.out.println("数据插入成功");
}
else {
System.out.println("数据插入失败");
}
//资源释放
ps.close();
conn.close();
}
}
结果:
由于第一遍插入忘记资源释放,故插入了两边得到结果:
package com.mystudy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Update {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?useSSL=true","root","root");
String sql="UPDATE student SET stu_password=12345 WHERE stu_id=4";
PreparedStatement ps= conn.prepareStatement(sql);
int resultint=ps.executeUpdate();
if (resultint>0){
System.out.println("数据修改成功");
}
else {
System.out.println("数据修改失败");
}
//资源释放
ps.close();
conn.close();
}
}
结果:
package com.mystudy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Delete {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?useSSL=true","root","root");
String sql="DELETE FROM student WHERE stu_id=4";
PreparedStatement ps= conn.prepareStatement(sql);
int resultint=ps.executeUpdate();
if (resultint>0){
System.out.println("数据删除成功");
}
else {
System.out.println("数据删除失败");
}
//资源释放
ps.close();
conn.close();
}
}
结果:
package com.mystudy.jdbc;
import java.sql.*;
public class Select {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?useSSL=true","root","root");
String sql="SELECT * FROM student ";
PreparedStatement ps= conn.prepareStatement(sql);
//返回结果集
ResultSet rs=ps.executeQuery();
//结果集的遍历
while (rs.next()){
//两种打印方法
// System.out.print(rs.getInt("stu_id"));
// System.out.print(rs.getString("stu_name"));
// System.out.print(rs.getString("stu_sex"));
// System.out.print(rs.getDate("stu_birthday"));
// System.out.print(rs.getString("stu_user"));
// System.out.print(rs.getString("stu_password"));
System.out.print(rs.getInt(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));
System.out.print(rs.getDate(4));
System.out.print(rs.getString(5));
System.out.println(rs.getString(6));
}
//资源释放
rs.close();
ps.close();
conn.close();
}
}
结果: