【JDBC初步】掌握JDBC连接Mysql数据库技术。

注意添加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();
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值