Java learning daily (using JDBC)

Keywords: JDBC MySQL SQL Database

Learning content

  • Use of JDBC
  • Development steps
    1. Select database: load database driver;
    2. Connect to the database
    3. Create database query
    4. Get query results
    5. Get query results
  • Complete JDBC operation code
package com.dodoke.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Demo1 {

    public static String DB_DRIVER = "com.mysql.jdbc.Driver";
    public static String URL = "jdbc:mysql://localhost:3306/dodoke";
    public static String USER_NAME = "root";
    public static String PASSWORD = "123456";

    public static void main(String[] args) throws Exception{
//      insert();
//      update();
//      delete();
        select();
    }

    public static void insert() throws Exception{
        // Loading drive
        Class.forName(DB_DRIVER); // Load driver by class full name
        // Establish connection
        Connection conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

        for (int i = 0; i < 100; i++) {

            // Create query
            PreparedStatement pst = conn.prepareStatement("INSERT INTO student(name) VALUES('Jack" + i + "')");
            // Execution query
            int rs = pst.executeUpdate();
            // Return result
            System.out.println(rs);
            // Close channel: close from the inside to the outside, first close the query, then close the connection
            pst.close();

        }
        conn.close();
    }

    public static void update()  throws Exception{
        // Loading drive
        Class.forName("com.mysql.jdbc.Driver"); // Load driver by class full name
        // Establish connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dodoke", "root", "123456");

        // Create query
        PreparedStatement pst = conn.prepareStatement("UPDATE student SET name=? WHERE ID<?");
        pst.setString(1, "Mary");
        pst.setInt(2, 4); // Setting the value of in sql statement represents the parameter
        // Execution query
        int rs = pst.executeUpdate();
        // Return result
        System.out.println(rs);
        // Close channel: close from the inside to the outside, first close the query, then close the connection
        pst.close();

        conn.close();
    }

    public static void delete() throws Exception {
        // Loading drive
        Class.forName("com.mysql.jdbc.Driver"); // Load driver by class full name
        // Establish connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dodoke", "root", "123456");

        // Create query
        PreparedStatement pst = conn.prepareStatement("DELETE FROM student WHERE ID<?");
        pst.setInt(1, 4); // Setting the value of in sql statement represents the parameter
        // Execution query
        int rs = pst.executeUpdate();
        // Return result
        System.out.println(rs);
        // Close channel: close from the inside to the outside, first close the query, then close the connection
        pst.close();

        conn.close();
    }

    public static void select() throws Exception {
        // Loading drive
        Class.forName("com.mysql.jdbc.Driver"); // Load driver by class full name
        // Establish connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dodoke", "root", "123456");

        // Create query
        PreparedStatement pst = conn.prepareStatement("SELECT id as idd,name as nm FROM student");
        // Execution query
        ResultSet rs = pst.executeQuery();
        // Return result
        // Traverse the contents of rs
        while (rs.next()) {
            int id = rs.getInt("idd");
            String name = rs.getString("nm");
            System.out.println(id + ":" + name);
        }
        rs.close();
        // Close channel: close from the inside to the outside, first close the query, then close the connection
        pst.close();

        conn.close();
    }
}

Learning summary

Today's study is more interesting, not as boring as the other days. But I don't have a thorough grasp of the key points of knowledge. I need my own efforts to practice more.

Posted by cdjsjoe on Tue, 31 Dec 2019 05:34:00 -0800