To write JAVA to Xiaobai to link MySQL database (JDBC):

Keywords: Java SQL Database JDBC

As a review and summary note, I have listed several jdbc steps, followed by a simple example, in which the try block is handled by the reader

/*
*1. Download the driver package: com.mysql.jdbc.Driver. There are a lot of download resources on the Internet. You can find duniang yourself, which will not be provided here;
*
*2. Import the driver package into the project, add to build path, and ask Du Niang for specific steps
*
*3. Load driver: use the forName(String driver) method of Class class to get the Class or interface related object of given string name;
*
*4. Configure database information: including database url/user/pass, etc;
*
*5. Get Connection object: use getConnection() method of DriverManager class to get Connection link object conn;
*
*6. Preprocessing sql: use the preparedstatement() method of Connection to preprocess the assembled sql statement,
*And get the Preparedstatement object pst;
*
*7. Execute operation: use the executeQuery() method of pst to get the result set of query or use the executeUpdate() method of pst to get the number of affected databases;
*
*8. Release resources: immediately after the operation, use the close() method of PreparedStatemment and the close() method of Connection to release the corresponding objects,
*Instead of waiting for the object to close automatically before releasing

  1 package com.cnblogs.chuanyueinlife;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 
  9 /**
 10  * JAVA Example steps for linking MySQL database:
 11  * 
 12  * @author Zhang Jian
 13  * 
 14  */
 15 public class JdbcTest {
 16 
 17     static Connection conn = null;
 18     static PreparedStatement pst = null;
 19 
 20     public static void main(String[] args) {
 21         // Example 1.Query operation:
 22         String sql = "SELECT * FROM `user` WHERE user_name =? AND pass_word = ?";// Spell sql Sentence(Placeholder with anti injection)
 23         selectDemo(sql);
 24         // Example 2.delete
 25         String sql1 = "DELETE FROM `user` WHERE user_name = 'zj'";
 26         deleteDemo(sql1);
 27     }
 28 
 29     public static void deleteDemo(String sql) {
 30         getConn();
 31         getPst(sql);
 32         try {
 33             int num = pst.executeUpdate();
 34             System.out.println("Successfully deleted" + num + "Records!");
 35 
 36         } catch (SQLException e) {
 37             // TODO Auto-generated catch block
 38             e.printStackTrace();
 39         }
 40         closeAll();// Release resources
 41     }
 42 
 43     public static void selectDemo(String sql) {
 44         getConn();
 45         getPst(sql);
 46         try {
 47             pst.setString(1, "admin");
 48             pst.setString(2, "admin");
 49             ResultSet rs = pst.executeQuery();
 50             System.out.println("user ID\t user name\t Password");
 51 
 52             while (rs.next()) {
 53                 System.out
 54                         .println(rs.getInt("id") + "\t" + rs.getString("user_name") + "\t" + rs.getString("pass_word"));
 55             }
 56         } catch (SQLException e) {
 57             // TODO Auto-generated catch block
 58             e.printStackTrace();
 59         }
 60         closeAll();// Release resources
 61     }
 62 
 63     public static void getConn() {
 64 
 65         /*
 66          * Configuration database information:
 67          */
 68         String driver = "com.mysql.jdbc.Driver";// drive
 69         String url = "jdbc:mysql://localhost:3306/zj";// data base url
 70         String user = "root";// Database user name
 71         String pass = "123456";// Database password
 72         try {
 73             Class.forName(driver);// Load driver
 74             conn = DriverManager.getConnection(url, user, pass);// Get connection object
 75 
 76         } catch (ClassNotFoundException | SQLException e) {
 77             // TODO Auto-generated catch block
 78             e.printStackTrace();
 79         }
 80     }
 81 
 82     public static void getPst(String sql) {
 83         try {
 84             pst = conn.prepareStatement(sql);// Pretreatment sql
 85         } catch (SQLException e) {
 86             // TODO Auto-generated catch block
 87             e.printStackTrace();
 88         }
 89     }
 90 
 91     public static void closeAll() {
 92         try {
 93             pst.close();
 94             conn.close();
 95         } catch (SQLException e) {
 96             // TODO Auto-generated catch block
 97             e.printStackTrace();
 98         }
 99 
100     }
101 }

 


*/

Posted by tomas.srna on Thu, 30 Apr 2020 14:14:05 -0700