MySQL small practice (using JDBC to operate the database)

topic:

1. Create a database (student number + name abbreviation, such as: 2020001zs) and create a table (more than five fields) in the database;

2. Use JDBC (using the PreparedStatement interface) to operate the database to add, delete, modify and query the data in the table

Table of contents

1. Database

1. Create a database

2. Create a table

3. Add data

 2. JDBC

1. Prepare the environment

 2. Query data

3. Add data

 4. Delete data

 5. Modify data


1. Database

1. Create a database

 

2. Create a table

 The title says: Create a table (more than five fields) in the database, let’s get 5

drop table if exists student_course;
create table student_course
(
    course_id     varchar(10)  comment '课程号',
    course_name   varchar(15) comment '课程名',
    course_number double unsigned comment '学分数',
    student_time  int unsigned comment '学时数',
    teacher       varchar(10) comment '任课教师'
)
    comment '课程表';

select *
from student_course;

 

 

 

3. Add data


INSERT INTO student_course
values ('K001', '计算机图形学', 2.5, 40, '胡晶晶'),
       ('K002', '计算机应用基础', 3, 48, '任泉'),
       ('K006', '数据结构', 4, 64, '马跃先'),
       ('M001', '政治经济学', 4, 64, '孔繁新'),
       ('S001', '高等数学', 3, 48, '赵晓尘');

			 select *
from student_course;

 

 2. JDBC

1. Prepare the environment

Click to enter the mysql jar package download official website

 If MySQL is version 5:

 

 

 My mysql is version 8, so download this:

 

 ​​​​​​

 

 Unzip:

 

 

 

 

 2. Query data

The driver file jar package of version 5.x corresponds to:
Class.forName("com.mysql.jdbc.Driver");
statement to load the database driver

And I am using the 8.0x version of the database driver file. For this, the statement of loading the database driver needs to be changed to:
Class.forName("com.mysql.cj.jdbc.Driver");

 

import java.sql.*;

public class Main {

    public static void main(String[] args) throws Exception {
        PreparedStatement ps = null;
        Connection con = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/20202122084tsj?serverTimezone=GMT%2B8", "root", "root");
            String sql = "select * from student_course;";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println("编号:" + rs.getString(1) + "\t" + "课程名:" + rs.getString(2) + "\t" + "学分数:" + rs.getString(3) + "\t" + "学时数:" + rs.getString(4) + "\t" + "任课教师:" + rs.getString(5));
            }

            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

 

 

3. Add data

import java.sql.*;
public class InsertData {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/20202122084tsj?serverTimezone=GMT%2B8", "root", "root");
            String sql = "INSERT INTO student_course values(?,?,?,?,?);";
            ps = con.prepareStatement(sql);
            ps.setString(1, "K111");
            ps.setString(2, "javaWeb");
            ps.setDouble(3, 3);
            ps.setInt(4, 48);
            ps.setString(5, "丁老师");
            ps.execute();
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 4. Delete data

import java.sql.*;

public class DeleteData {
    public static void main(String[] args){
        Connection con = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/20202122084tsj?serverTimezone=GMT%2B8", "root", "root");
            String sql = "delete from student_course where course_id  = ?;";
            ps = con.prepareStatement(sql);
            ps.setString(1,"K111");
            ps.execute();
            ps.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

Previously added data is removed 

 5. Modify data

import java.sql.*;

public class UpdateData {
    public static void main(String[] ags){
        Connection con = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/20202122084tsj?serverTimezone=GMT%2B8", "root", "root");
            String sql = "update student_course set course_name = ?,teacher = ? where course_id  = ?;";
            ps = con.prepareStatement(sql);
            ps.setString(1,"SpringCloud");
            ps.setString(2,"王老师");
            ps.setString(3,"S001");
            ps.execute();
            ps.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 The code redundancy is serious, and there will be tool class simplification in the future

Guess you like

Origin blog.csdn.net/Javascript_tsj/article/details/130874372