当前位置:网站首页>JDBC operation MySQL example

JDBC operation MySQL example

2022-06-27 07:21:00 Qingyun ing

One 、mysql Driver download :

Address :https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-8.0.29.zip

Two 、 Project reference

1  Create project , modular

2 hold mysql-connector-java-8.0.29.jar Copy to project

3 Introduced mysql Package add project reference

3、 ... and 、 Sample code

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDemo {
    public static void main(String[] args) throws Exception {
        //1  Registration drive 
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2  Get the connection 
        String url = "jdbc:mysql://10.1.1.1:3306/db_test";
        String username = "qingyuning";
        String password = "[email protected]~2022";
        Connection conn = DriverManager.getConnection(url, username, password);
        
        //3 sql  sentence 
        String sql = "update account set age=18 where id=1";

        //4  Access to perform sql The object of 
        Statement stmt = conn.createStatement();

        //5  perform sql
        int count = stmt.executeUpdate(sql);

        //6  Print processing results 
        System.out.println(count);

        //7  Release resources 
        stmt.close();
        conn.close();
    }
}

原网站

版权声明
本文为[Qingyun ing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270655102071.html