Advertisement
zeev

updated SQL

Sep 4th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package Main;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. public class Tester {
  9.     private static Connection db;
  10.     private static PreparedStatement stmt;
  11.     final static String URL = "jdbc:mysql://127.0.0.1:3306/";
  12.     final static String DB_NAME = "hackeru";
  13.     final static String USER_NAME = "root";
  14.     final static String USER_PASS = "";
  15.  
  16.     public static void main(String[] args) throws Exception {
  17.         // creating our connection string
  18.  
  19.         // creating a connection to our database
  20.         db = DriverManager.getConnection(URL + DB_NAME, USER_NAME, USER_PASS);
  21.         //create table for the first time, and if the table exists do not create it....
  22.         //createTable();
  23.        
  24.         //insert first data to our mySQL with JDBC
  25.         insertFirstDemo();
  26.     }
  27.  
  28.     private static void insertFirstDemo() {
  29.         // create a SQL command.
  30.         String sql = "INSERT INTO chocos (name) "
  31.                 + "values ('Para'),('Milka'),('Oreo'),('Bueno'),"
  32.                 + "('Ferror Roche');";
  33.  
  34.         try {
  35.             // prepare statement for given SQL
  36.             stmt = db.prepareStatement(sql);
  37.             // execute the statement
  38.             stmt.execute();
  39.  
  40.         } catch (SQLException e) {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.         }
  44.         System.out.println("Data was inserted");
  45.     }
  46.  
  47.     private static void createTable() {
  48.         // create a SQL command.
  49.         String sql = "CREATE TABLE IF NOT EXISTS chocos" + " (id INT PRIMARY KEY AUTO_INCREMENT,"
  50.                 + " name VARCHAR (16) NOT NULL);";
  51.  
  52.         try {
  53.             // prepare statement for given SQL
  54.             stmt = db.prepareStatement(sql);
  55.             // execute the statement
  56.             stmt.execute();
  57.  
  58.         } catch (SQLException e) {
  59.             // TODO Auto-generated catch block
  60.             e.printStackTrace();
  61.         }
  62.  
  63.         System.out.println("Table was created");
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement