Advertisement
transpalette

SQL Fiddling in Java

Nov 20th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package sqlfiddle;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         Connection c = null;
  12.  
  13.         try {
  14.             Class.forName("org.postgresql.Driver");
  15.             c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres",
  16.                     "postgres", "postgres");
  17.             System.out.println("Database open ok");
  18.             System.out.println("Creating table ...");
  19.             createTable(c);
  20.             System.out.println("Inserting a customer ...");
  21.             insert(c, "Théo Morales", "theo.morales@mail.com", 91748274, false);
  22.             System.out.println("Updating customer Théo Morales...");
  23.             update(c, "theo.morales@mail.com", "Théo Martin Morales", null, true);
  24.             System.out.println("Deleting customer Théo Martin Morales...");
  25.             delete(c, "theo.morales@mail.com");
  26.             System.out.println("Dropping table customers...");
  27.             dropTable(c);
  28.         } catch (Exception e) {
  29.             System.err.println(e.getClass().getName() + ": " + e.getMessage());
  30.             System.exit(0);
  31.         }
  32.  
  33.         if(c != null) {
  34.             try {
  35.                 c.close();
  36.             } catch (SQLException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41.  
  42.     private static void createTable(Connection c) throws SQLException {
  43.         Statement stmt = c.createStatement();
  44.         String qry = "CREATE TABLE \"JAVA\".customers("
  45.                     + "id SERIAL PRIMARY KEY,"
  46.                     + "name VARCHAR(30) NOT NULL,"
  47.                     + "email VARCHAR(40) UNIQUE NOT NULL,"
  48.                     + "phone_number NUMERIC NOT NULL,"
  49.                     + "verified BOOLEAN DEFAULT(false)"
  50.                     + ");";
  51.         stmt.execute(qry);
  52.         stmt.close();
  53.         System.out.println("Create table ok");
  54.     }
  55.  
  56.     private static void insert(Connection c, String name, String email, long phoneNumber, boolean verified) throws SQLException {
  57.         Statement stmt = c.createStatement();
  58.         String qry = "INSERT INTO \"JAVA\".customers(name, email, phone_number, verified) "
  59.                     + "VALUES('" + name + "', '" + email + "', " + phoneNumber + ", " + verified + ");";
  60.         stmt.execute(qry);
  61.         stmt.close();
  62.         System.out.println("Insert row ok");
  63.     }
  64.  
  65.     private static void update(Connection c, String customerEmail, String newName, Long newPhoneNumber, Boolean verified) throws SQLException {
  66.         Statement stmt = c.createStatement();
  67.         String qry = "UPDATE \"JAVA\".customers SET"
  68.                     + (newName != null ? " name = '" + newName + "'" : "")
  69.                     + (newPhoneNumber != null ? ", phone_number = " + newPhoneNumber : "")
  70.                     + (verified != null ? ", verified = " + verified : "")
  71.                     + " WHERE email = '" + customerEmail + "';";
  72.         stmt.executeUpdate(qry);
  73.         stmt.close();
  74.         System.out.println("Update row ok");
  75.     }
  76.  
  77.     private static void delete(Connection c, String customerEmail) throws SQLException {
  78.         Statement stmt = c.createStatement();
  79.         String qry = "DELETE FROM \"JAVA\".customers WHERE email = '" + customerEmail + "';";
  80.         stmt.execute(qry);
  81.         stmt.close();
  82.         System.out.println("Delete row ok");
  83.     }
  84.  
  85.     private static void dropTable(Connection c) throws SQLException {
  86.         Statement stmt = c.createStatement();
  87.         stmt.execute("DROP TABLE \"JAVA\".customers;");
  88.         stmt.close();
  89.         System.out.println("Drop table ok");
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement