java simple use for MySQL

MySQL java to operate it means three steps: load the driver -> Get connected -> execute SQL statements -> Show the results
here I want to say about that execute SQL statements, we must pay attention to the original SQL statement when writing SQL statements exactly the same, that is to say in the SQL string is a pair of single quotation marks, but also add a pair of single quotation marks to write your java while, of course, there are other ways to perform, just say here that one of the following is Some simple examples:

Loading the corresponding drive

. 1 String = Driver "com.mysql.jdbc.Driver" ;
 2 the Class.forName (Driver); // classLoader, loading the corresponding drive

Get connected

Url = String "? Jdbc: MySQL: //127.0.0.1: 3306 / webstore useSSL = false"; // server IP: 127.0.0.1 Port: 3306 Database name: webstore 
String username = "root"; // username 
String = password "123456"; // exemplary password 
connection Conn = (connection) the DriverManager.getConnection (URL, username, password);   // Get connected

Execute SQL statements

// inserted into a products table data 
public  void the Add (String ID, pname String, String Brand, a float . Price, int Stock) { 
// design variable SQL statement String SQL
= "INSERT INTO Product values ( '" + + ID " ',' "+ + pname" ',' "+ + Brand" ', ". price + +", "+ Stock +") " ; the try { State of Statement = (of Statement) conn.createStatement (); // get the operating statement IF (state.executeUpdate (SQL) = 0! {) // send SQL statements to run MySQL System.out.println ( "Inserting data success! " ); } else { System.out.println ( "insert data failed!" ); } state.close(); } catch (SQLException e) { e.printStackTrace (); }
}
1      / * 
2       * product displays all the data in the table
 . 3       * / 
. 4      public  void a selectAll () {
 . 5          String = SQL "SELECT * from product" ;
 . 6          the try {
 . 7              the Statement = Statement (the Statement) conn.prepareStatement (SQL) ;
 . 8              the ResultSet RES = Statement.executeQuery (SQL);
 . 9              System.out.println ( "product ID" + "\ t" + " product name" + "\ t" + "brand" + "\ t" + "Unit "+" \ t "+" stock " );
 10              the while (res.next ()) {
 . 11                 System.out.println(res.getString(1) + "\t" + res.getString(2) + "\t" + res.getString(3) + "\t" + res.getFloat(4) +"\t"  + res.getInt(5));
12             }
13             res.close();
14             statement.close();
15         } catch (SQLException e) {
16             e.printStackTrace();
17         }
18     }
19 
20     /**
21      * 通过id显示product表中的数据 
22      * @param id
23      */
24     public void selectById(String id) {
25          String = SQL "SELECT * WHERE ID = Product from" + ID;
 26 is          the try {
 27              the Statement = Statement (the Statement) conn.prepareStatement (SQL);   // get operation statement
 28              the ResultSet RES = Statement.executeQuery (SQL);   / / MySQL implementation of the statement and returns the result set
 29              System.out.println ( "product ID" + "\ t" + " product name" + "\ t" + "brand" + "\ t" + "unit price" + "\ t "+" stock " );
 30              the while (res.next ()) {   // content output result set
31 is System.out.println (res.getString (. 1) +" \ T "+ res.getString (2) + "\ t" + res.getString(3) + "\t" + res.getFloat(4) +"\t" + res.getInt(5)); 32 } 33 res.close(); 34 statement.close(); 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 } 38 }

 

Guess you like

Origin www.cnblogs.com/li1234567980/p/11104830.html