JDBC connection database operation in java

First, we need to create a database. After we download MySQL and install it, open it. First, use show databases to view the local database. Generally, there is local data. We don’t need to worry about it. Edit the database you just created, use <database name>, and a database is created. We can also use Navicat for MySQL to create database.
import java.sql.*;

public class test {
    public static void main(String[] args) {
        //declare the Connection object
        Connection con;
        //driver name
        String driver = "com.mysql.jdbc.Driver";
        //URL points to the database name mydata to be accessed
        String url = "jdbc:mysql://localhost:3306/student";
        //Username for MySQL configuration
        String user = "root";
        //Password for MySQL configuration
        String password = "*********";
        // Traverse the query result set
        try {
            // load driver
            Class.forName(driver);
            //1.getConnection() method, connect to MySQL database! !
            con = DriverManager.getConnection(url,user,password);
            if(!con.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            //2. Create a statement class object to execute SQL statements! !
            Statement statement = con.createStatement();
            // SQL statement to execute
            String sql = "select * from studinfo";
            String sql1 = "select * from grade";
            //3.ResultSet class, used to store the obtained result set! !
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("--------------------------------");
            System.out.println("The execution result is as follows:");  
            System.out.println("--------------------------------");  
            System.out.println("|   姓名        |" + " 性别    |" + "   年龄      |"+"chinese|"+"math|"+"english");  
            System.out.println("--------------------------------");  
            
            
            while(rs.next()){
                //Get the column data of stuname
//             rs.getInt(1);
             System.out.println(rs.getString(1)+"   "+rs.getString(2)+"      "+rs.getString(3));
             rs.getString(1);
//               System.out.println(rs);
                //Get the data of this column of stuid
       
            }
         
            rs.close();
            con.close();
            
            
 /*           while(rs.next()){
                //Get the data of this column of studinfo
             String studinfo = rs.getString("studinfo");
                //Get the data of the grade column
             String grade = rs.getString("grade");
                // output result
                System.out.println(studinfo + "\t" + grade);
            }
*/
        } catch(ClassNotFoundException e) {   
            //Database driver class exception handling
            System.out.println("Sorry,can`t find the Driver!");   
            e.printStackTrace ();   
            } catch(SQLException e) {
            // exception handling for database connection failure
            e.printStackTrace ();  
            }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace ();
        }finally{
            System.out.println("Database data obtained successfully!!");
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326061487&siteId=291194637