JDBC accesses Mysql database to realize addition, deletion, modification and query

1. Download the driver

Download the MySQL driver package, and then import the package into the integrated development environment

2. Load the driver

After importing the package, load the driver with the statement

Class.forName("com.mysql.jdbc.Driver");

3. Establish a connection

The DriverManger class provides two class methods (static methods) for establishing connections

Connection getConnection(java.lang.String,java.lang.String,java.lang.String);
Connection getConnection(java.lang.String);

Both of these methods may throw SQL Exception exceptions, and both return a Connection object

as follows:

String url="jdbc:mysql://localhost:3306/student?useSSL=false&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
String user="root";
String password="12345";
Connection conn=DriverManager.getConnection(url,user,password);

4. Actual projects

1.JDBC.java

import java.sql.*;
public class JDBC {
    private Statement stmt = null;
    private Connection conn= null;
    ResultSet rs = null;
    public JDBC() {
    }
//    数据库连接函数
    public void openConnection() {
        try {
//            连接数据库
//            加载驱动
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url="jdbc:mysql://localhost:3306/books?useSSL=false&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
            String user="root";
            String password="12345";
//            连接数据库
            conn=DriverManager.getConnection(url,user,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    数据库查询函数
    public ResultSet executeQuery(String sql) {
        rs = null;
        try {
//            执行数据库查询语句,通过外界传入sql语句
            stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs=stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        查询成功返回一个集合
        return rs;
    }
//    数据库更新函数
    public int executeUpdate(String sql) {
        int n =0;
        try {
            stmt=conn.createStatement();
            n=stmt.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return n;
    }
//    数据库关闭函数
//    运行完数据库要关闭所有连接,减少内存压力
    public void closeConnection() {
        try {
            if (rs!=null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (stmt!=null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn!=null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

2.mian.java

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        JDBC jdbc = new JDBC();
        int i=0;
        System.out.println("欢迎来到图书系统!");
        while (i!=5) {
            System.out.println("目录:");
            System.out.println("1.查询所有图书;");
            System.out.println("2.增加图书;");
            System.out.println("3.删除图书;");
            System.out.println("4.查询图书;");
            System.out.println("5.退出;");
            System.out.println("请选择:");
            Scanner ready = new Scanner(System.in);
            i = ready.nextInt();
            if (i==1){
//              定义查询sql语句
                String sql1="select * from book_message";
                jdbc.openConnection();
                ResultSet rs=jdbc.executeQuery(sql1);
                try {
                    while (rs.next()){
                        String name = rs.getString("name");
                        String zz = rs.getString("zz");
                        String cbs = rs.getString("cbs");
                        String date = rs.getString("date");
                        String jg = rs.getString("jg");
                        System.out.printf("%s\t",name);
                        System.out.printf("%s\t",zz);
                        System.out.printf("%s\t",cbs);
                        System.out.printf("%s\t",date);
                        System.out.printf("%s元\n",jg);
                    }
                jdbc.closeConnection();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else if (i==2){
//                定义增加图书语句
                String sql2="insert into book_message(id,name,zz,cbs,date,jg)";
                String id,name,zz,cbs,date,jg;
                System.out.println("请输入id:");
                id = ready.next();
                System.out.println("请输入书名:");
                name = ready.next();
                System.out.println("请输入作者:");
                zz = ready.next();
                System.out.println("请输入出版社:");
                cbs = ready.next();
                System.out.println("请输入时间:");
                date = ready.next();
                System.out.println("请输入价格:");
                jg = ready.next();
                sql2=sql2+"values('"+id+"','"+name+"','"+zz+"','"+cbs+"','"+date+"','"+jg+"')";
                jdbc.openConnection();
                int n = jdbc.executeUpdate(sql2);
                if (n>0) System.out.println("添加成功!");
                else System.out.println("添加失败!");
                jdbc.closeConnection();
            }
            else if (i==3){
//                定义删除sql语句
                String sql3 = "delete from book_message where";
                System.out.println("请输入删除图书名:");
                String name = ready.next();
                sql3 = sql3+" name='"+name+"'";
                jdbc.openConnection();
                int n = jdbc.executeUpdate(sql3);
                if (n>0) System.out.println("删除成功!");
                else System.out.println("删除失败!");
                jdbc.closeConnection();
            }else if (i==4){
//                定义查询sql语句
                String sql4 = "select * from book_message where name='";
                System.out.println("请输入查询图书名:");
                String name = ready.next();
                sql4 = sql4+name+"'";
                jdbc.openConnection();
                try {
                    ResultSet rs =jdbc.executeQuery(sql4);
                    rs.next();
                    String zz = rs.getString("zz");
                    String cbs = rs.getString("cbs");
                    String date = rs.getString("date");
                    String jg = rs.getString("jg");
                    System.out.printf("%s\t",name);
                    System.out.printf("%s\t",zz);
                    System.out.printf("%s\t",cbs);
                    System.out.printf("%s\t",date);
                    System.out.printf("%s元\n",jg);
                    jdbc.closeConnection();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }
        System.out.println("欢迎下次使用!");
    }

}

Five, there may be problems

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

The version of the driver is low, the new version will not appear, download a new driver or modify forName

Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

Guess you like

Origin blog.csdn.net/Jocks5/article/details/121672717