1. JDBC
- java database connectivity java数据库的连接。
- java中针对操作数据库,提供的⼀套规范, ⽐如⼀些接⼝。
2、.jar包 ( 8.x的, 5.x的)
3、操作
- DriverManager 注册驱动创建连接。
- Connection 数据库的连接对象。
- Statement 执⾏sql语句的对象。
- Result 查询之后的结果。
4、jdbc操作步骤
- 加载数据库的驱动:①:把数据库的jar包导⼊到项⽬中。②:通过反射加载驱动对象。
- 创建数据库的连接。
- 编写sql语句,执⾏sql语句。
- 如果查询,会得到结果集,遍历得到的内容。
- 关闭资源
5、案例
-
@Test public void testSelect() throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName("com.mysql.cj.jdbc.Driver"); //mysql 8.x // 2. 创建数据库的连接 // 参数1: url:数据库的地址 jdbc:mysql://数据库的IP地址:端口号/数据库名称 // 参数2: userName 用户名 root // 参数3: passWord 密码 123456 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root","123456"); // 3. 编写sql语句,执行sql语句 //编写SQL语句 String sql="select StudentNo,StudentName from student"; // 创建statement对象来执行sql语句 Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery(sql); // 4. 如果查询,会得到结果集,遍历得到的内容 while(resultSet.next()){ int studentNo =resultSet.getInt("StudentNo"); String studentName =resultSet.getString("StudentName"); System.out.println("学号:"+studentNo+",姓名:"+studentName); } // 5. 关闭资源 resultSet.close(); statement.close(); conn.close(); }
-
@Test public void testInsert() throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName("com.mysql.cj.jdbc.Driver"); //mysql 8.x // 2. 创建数据库的连接 // 参数1: url:数据库的地址 jdbc:mysql://数据库的IP地址:端口号/数据库名称 // 参数2: userName 用户名 root // 参数3: passWord 密码 123456 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root","123456"); // 3. 编写sql语句,执行sql语句 //编写SQL语句 String sql="insert into student(StudentNo,StudentName,Email) values(1018,'小帅','[email protected]')"; // 创建statement对象来执行sql语句 Statement statement = conn.createStatement(); int i = statement.executeUpdate(sql); // 4. 如果查询,会得到结果集,遍历得到的内容 System.out.println(i); // 5. 关闭资源; statement.close(); conn.close(); }
-
//更新数据 @Test public void testUpdate() throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName("com.mysql.cj.jdbc.Driver"); //mysql 8.x // 2. 创建数据库的连接 // 参数1: url:数据库的地址 jdbc:mysql://数据库的IP地址:端口号/数据库名称 // 参数2: userName 用户名 root // 参数3: passWord 密码 123456 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root","123456"); // 3. 编写sql语句,执行sql语句 //编写SQL语句 String sql="update student set StudentName='小帅帅' where StudentNo=1018"; // 创建statement对象来执行sql语句 Statement statement = conn.createStatement(); int i = statement.executeUpdate(sql); // 4. 如果查询,会得到结果集,遍历得到的内容 System.out.println(i); // 5. 关闭资源; statement.close(); conn.close(); }
-
@Test public void testDelete() throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName("com.mysql.cj.jdbc.Driver"); //mysql 8.x // 2. 创建数据库的连接 // 参数1: url:数据库的地址 jdbc:mysql://数据库的IP地址:端口号/数据库名称 // 参数2: userName 用户名 root // 参数3: passWord 密码 123456 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool", "root","123456"); // 3. 编写sql语句,执行sql语句 //编写SQL语句 String sql="delete from student where StudenNo=1018"; // 创建statement对象来执行sql语句 Statement statement = conn.createStatement(); int i = statement.executeUpdate(sql); // 4. 如果查询,会得到结果集,遍历得到的内容 System.out.println(i); // 5. 关闭资源; statement.close(); conn.close(); }
-
public class Student { private int StudenNo; private String LoginPwd; private String StudentName; private String sex; private int GradeId; private String Phone; private String Address; private String BornDate; private String Email; private String IdentityCard; public Student() { } public Student(int studenNo, String loginPwd, String studentName, String sex, int gradeId, String phone, String address, String bornDate, String email, String identityCard) { StudenNo = studenNo; LoginPwd = loginPwd; StudentName = studentName; this.sex = sex; GradeId = gradeId; Phone = phone; Address = address; BornDate = bornDate; Email = email; IdentityCard = identityCard; } public int getStudenNo() { return StudenNo; } public void setStudenNo(int studenNo) { StudenNo = studenNo; } public String getLoginPwd() { return LoginPwd; } public void setLoginPwd(String loginPwd) { LoginPwd = loginPwd; } public String getStudentName() { return StudentName; } public void setStudentName(String studentName) { StudentName = studentName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getGradeId() { return GradeId; } public void setGradeId(int gradeId) { GradeId = gradeId; } public String getPhone() { return Phone; } public void setPhone(String phone) { Phone = phone; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public String getBornDate() { return BornDate; } public void setBornDate(String bornDate) { BornDate = bornDate; } public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } public String getIdentityCard() { return IdentityCard; } public void setIdentityCard(String identityCard) { IdentityCard = identityCard; } @Override public String toString() { return "Student{" + "StudenNo=" + StudenNo + ", LoginPwd='" + LoginPwd + '\'' + ", StudentName='" + StudentName + '\'' + ", sex='" + sex + '\'' + ", GradeId=" + GradeId + ", Phone='" + Phone + '\'' + ", Address='" + Address + '\'' + ", BornDate='" + BornDate + '\'' + ", Email='" + Email + '\'' + ", IdentityCard='" + IdentityCard + '\'' + '}'; } }
-
import java.sql.*; import java.util.ArrayList; import java.util.List; public class JDBCTest2 { private static final String DRIVER ="com.mysql.cj.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost:3306/myschool"; private static final String USER = "root"; private static final String PASSWORD = "123456"; public static void main(String[] args) throws SQLException, ClassNotFoundException { // testSelect(); testSelect2("张三"); } //查所有 public static void testSelect() throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName(DRIVER); //创建数据库的连接 Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //编写sql语句,执行sql语句 String sql = "select * from student"; Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery(sql); // 4. 如果查询,会得到结果集,遍历得到的内容 //封装业务数据,把一行封装到一个Java对象中,把所有行封装到一个Java集合中 Student student=null; List<Student> studentList = new ArrayList<Student>(); while (resultSet.next()){ student = new Student(); student.setStudenNo(resultSet.getInt("StudentNo")); student.setLoginPwd(resultSet.getString("LoginPwd")); student.setStudentName(resultSet.getString("StudentName")); student.setSex(resultSet.getString("Sex")); student.setGradeId(resultSet.getInt("GradeId")); student.setPhone(resultSet.getString("Phone")); student.setAddress(resultSet.getString("Address")); student.setBornDate(resultSet.getString("BornDate")); student.setEmail(resultSet.getString("Email")); student.setIdentityCard(resultSet.getString("BornDate")); studentList.add(student); System.out.println(student); } // 5. 关闭资源 resultSet.close(); statement.close(); conn.close(); } //根据学生姓名查学生信息 public static void testSelect2(String name) throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName(DRIVER); //创建数据库的连接 Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //编写sql语句,执行sql语句 String sql = "select * from student where StudentName=?"; PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1,name); ResultSet resultSet = preparedStatement.executeQuery(); // 4. 如果查询,会得到结果集,遍历得到的内容 //封装业务数据,把一行封装到一个Java对象中,把所有行封装到一个Java集合中 Student student=null; List<Student> studentList = new ArrayList<Student>(); while (resultSet.next()){ student = new Student(); student.setStudenNo(resultSet.getInt("StudentNo")); student.setLoginPwd(resultSet.getString("LoginPwd")); student.setStudentName(resultSet.getString("StudentName")); student.setSex(resultSet.getString("Sex")); student.setGradeId(resultSet.getInt("GradeId")); student.setPhone(resultSet.getString("Phone")); student.setAddress(resultSet.getString("Address")); student.setBornDate(resultSet.getString("BornDate")); student.setEmail(resultSet.getString("Email")); student.setIdentityCard(resultSet.getString("BornDate")); studentList.add(student); System.out.println(student); } // 5. 关闭资源 resultSet.close(); preparedStatement.close(); conn.close(); } //插入一条数据 public static void testInsert(Student student) throws ClassNotFoundException, SQLException { // 1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName(DRIVER); //创建数据库的连接 Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //编写sql语句,执行sql语句 String sql = "insert into student value (?,?,?,?,?,?,?,?,?,?)"; PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setInt(1,student.getStudenNo()); preparedStatement.setString(2,student.getLoginPwd()); preparedStatement.setString(3,student.getStudentName()); preparedStatement.setString(4,student.getSex()); preparedStatement.setInt(5,student.getGradeId()); preparedStatement.setString(6,student.getPhone()); preparedStatement.setString(7,student.getAddress()); preparedStatement.setString(8,student.getBornDate()); preparedStatement.setString(9,student.getEmail()); preparedStatement.setString(10,student.getIdentityCard()); int i = preparedStatement.executeUpdate(); System.out.println(i); // 5. 关闭资源 preparedStatement.close(); conn.close(); } //事务 使用account表 A用户给B用户转钱,如果A用户钱变负数则回滚,如果为正数则提交 public static void testTransaction(String name1,String name2,int m) throws ClassNotFoundException, SQLException { //1. 加载数据库的驱动 // 1. 把数据库的jar包导入到项目中 // 2. 通过反射加载驱动对象 Class.forName(DRIVER); //2. 创建数据库的连接 Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //1. 关闭自动提交 conn.setAutoCommit(false); //3. 编写sql语句,执行sql语句 String sql1="update account set money=money-? where name=?"; String sql2="update account set money=money+? where name=?"; PreparedStatement preparedStatement = conn.prepareStatement(sql1); preparedStatement.setInt(1,m); preparedStatement.setString(2,name1); int i = preparedStatement.executeUpdate(); preparedStatement = conn.prepareStatement(sql2); preparedStatement.setInt(1,m); preparedStatement.setString(2,name2); i = preparedStatement.executeUpdate(); //业务逻辑 查A的钱是多少 如果比m大 commit 如果比m少 rollback String sql3="select money from account where name=?"; preparedStatement = conn.prepareStatement(sql3); preparedStatement.setString(1,name1); ResultSet resultSet = preparedStatement.executeQuery(); int name1Money=0; while(resultSet.next()){ name1Money = resultSet.getInt("money"); } if(name1Money>=m){ System.out.println(name1+"给"+name2+"转账成功"); conn.commit();//提交事务 }else{ conn.rollback();//回滚 System.out.println(name1+"给"+name2+"转账失败,余额不足"); } //恢复自动提交 conn.setAutoCommit(true); //5. 关闭资源 resultSet.close(); preparedStatement.close(); conn.close(); } }