JAVA operates the MySQL database

JAVA operation MySQL database, involving the creation of connections, create tables, insert data, update data, query data

General steps:

  1. Get driver (can be omitted)

  2. Get connected

The Connection interface, representing a Connection object, is implemented by the vendor of the database

Using static methods of the DriverManager class,getConnection gets the connection to the database

  1. Obtaining the Statement object

The SQL statement execution object is obtained through the createStatement method of Connection

  1. Processing result sets (only at query time)

  2. Release resources

The code is as follows:

package jdbc; import java.sql.*; Public class Test01 {public static void main(String[] args) throws ClassNotFoundException, SQLException {// Use the driver; Class.forname (". Com. Mysql. JDBC Driver "); / / create a connection, involving database IP, port, database name, character set, account number and password String url = "JDBC: mysql: / / 127.0.0.1:3307 / testdb? characterEncoding=UTF-8"; Connection conn = DriverManager.getConnection(url,"root","Root#123456"); //System.out.println(conn); Statement Statement SMT = conn.createstatement (); String sqL1 = "create table if not exists test14(id int primary key auto_increment,name varchar(20),age int);" ; smt.executeUpdate(sql1); / / insert the data String sql_i = "insert into test14 values (1, 'liu bei, 45), (2," guan yu ", 40), (3, "zhang fei", 37), (4,' zhaoyun, 30), (5, 'zhuge liang, 27);" ; smt.executeUpdate(sql_i); // Update data String SQL_u = "Update test14 SET age = 36 WHERE name=' zhangfu ';" ; smt.executeUpdate(sql_u); // Query result String SQL_q = "select * from test14;" ; ResultSet res = smt.executeQuery(sql_q); while(res.next()){ int id = res.getInt(1); String name= res.getString("name"); int age = res.getInt("age"); System.out.println("id:"+ id + " name:" + name +" age:"+age); } // Close the stream (first open then closed) res.close(); smt.close(); conn.close(); }}Copy the code
Copy the code

The results are as follows:

Highlights from the past

MySQL high availability MHA cluster deployment mysql8.0 new users and encryption rule changes those things

Monitoring tools: Prometheus+Grafana monitors MySQL and Redis databases

MySQL sensitive data encryption and decryption

MySQL > restore MySQL

MySQL database backup and restore (2)