注意添加MySQL的JDBC依赖以及Junit单元测试依赖时,注意版本:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
```
具体实战步骤:
https://developer.aliyun.com/article/770798#slide-9
相关知识:JDBC连接数据库的七个步骤https://blog.csdn.net/weixin_43520450/article/details/107230205
---------------------
通过idea对mysql进行增删改查:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class sqlTest {
private Connection con=null;//数据库连接,从DriverManager的方法获得,用以产生执行sql的PreparedStatement
public sqlTest() throws SQLException, ClassNotFoundException {
String URL = "jdbc:mysql://localhost:3306/web?serverTimezone=UTC&&useSSL=false";
String DRIVER = "com.mysql.cj.jdbc.Driver";
//step1 加载数据库驱动
Class.forName(DRIVER);
System.out.println("数据库驱动加载成功");
//step2 连接数据库
this.con = DriverManager.getConnection(URL,"root","346270hhh");
System.out.println("数据库连接成功");
}
public void close() throws SQLException {
this.con.close();;
}
public void insertStudent(Student student) throws SQLException {
//创建sql语句s
String sql="insert into Students(stuName,stuNo,sex,birth_date)value(?,?,?,?)";
//PreparedStatement能够对SQL语句进行预编译,这样防止了 SQL注入 提高了安全性。
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,student.getName());
ps.setString(2,student.getNo());
ps.setBoolean(3,student.isMale());
ps.setString(4,student.getBirthdate());
//执行 sql 因为这里插入操作就不对结处理
ps.executeUpdate();
ps.close();
}
public List<Student> get_student() throws SQLException {
List<Student>list=new ArrayList<Student>();//返回的结果
String sql = "select * from Students where stuId = 2";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet resultSet=ps.executeQuery();//结果集
while(resultSet.next())//遍历
{
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
String no=resultSet.getString(3);
boolean male=resultSet.getBoolean(4);
String birthday = resultSet.getString(5);
list.add(new Student(id,name,no,male,birthday));
}
ps.close();
return list;
}
public List<Student> findByName() throws SQLException {
List<Student>list=new ArrayList<Student>();//返回的结果
String sql = "select * from Students where stuName like '%一%'";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet resultSet=ps.executeQuery();//结果集
while(resultSet.next())//遍历
{
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
String no=resultSet.getString(3);
boolean male=resultSet.getBoolean(4);
String birthday = resultSet.getString(5);
list.add(new Student(id,name,no,male,birthday));
}
ps.close();
return list;
}
public void update() throws SQLException {
String sql = "update Students set sex=false and stuNo = 22222 where stuId=12";
PreparedStatement ps = con.prepareStatement(sql);
ps.executeUpdate();
ps.close();
}
public void deletebyid(int id) throws SQLException {
String sql = "delete from Students where stuId = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
ps.close();
}
}