Java connects to manage database through JDBC

Third party classes are needed to access MySQL database. These third party classes are all compressed in a file called Jar.
In order for the code to use third-party classes, you need to import mysql's special Jar package for the project

Attach the download address link of the download jar package: https://pan.baidu.com/s/16BdVevqO6ezgtVPdwKar0w extraction code: 5e8s

Right click Project > build path > add external achievements

In the dialog box, select the downloaded jar package and click OK

 

            //Driver class com.mysql.jdbc.Driver
            //In mysql-connector-java-5.1.7-bin.jar
            //If you forget the package of the first step, ClassNotFoundException will be thrown
            //Load the driver and add a try catch statement
			Class.forName("com.mysql.jdbc.Driver");

To perform this step successfully, you must build a database in mysql

            //Connect to database
            // Establish a Connection to the database
            // Here we need to provide:
            // IP of the database: 127.0.0.1 (local)
            // Port number of database: 3306 (mysql special port number)
            // Database name how2java
            // Encoding mode UTF-8
            // Account number root
            // Password admin
			
			String url = "jdbc:mysql://127.0.0.1:3306/how2java? 
                          useUnicode=true&characterEncoding=utf8";
			String username = "root";
			String password = "admin";
			java.sql.Connection c = DriverManager.getConnection(url,username,password);

Create statement, which is used to execute SQL statements, such as adding and deleting

            // Note: java.sql.Statement is used
            // Don't use it carelessly: com.mysql.jdbc.Statement;
            Statement s = c.createStatement();

s.execute execute execute sql statement

            // Prepare sql statement
            // Note: use single quotation mark for string '
            String sql = "insert into Table name of the database values('cr',213)";
            s.execute(sql);

The connection of the database is limited resources. After relevant operations are completed, the database should be closed
Close Statement first
Close Connection after

            // The connection of the database is limited in resources. After the relevant operation, form a good habit of closing the database
            // Close Statement first
            if (s != null)
                try {
                    s.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            // Close Connection after
            if (c != null)
                try {
                    c.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

If you find the previous method of closing the connection troublesome, please refer to Closed flow The way of,

Use try with resource to automatically close the connection,

Because Connection and Statement both implement the autocolosable interface

        try (
           String url = "jdbc:mysql://127.0.0.1:3306/how2java? 
                          useUnicode=true&characterEncoding=utf8";
			String username = "root";
			String password = "admin";
			java.sql.Connection con = DriverManager.getConnection(url,username,password);  
        )
        {
            String sql = "insert into hero values(null," + "'Ti Mo'" + "," + 313.0f + "," + 50 + ")";
            s.execute(sql);
              
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Keywords: Programming Database MySQL SQL JDBC

Added by philspliff on Tue, 10 Dec 2019 22:03:32 +0200