Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sqlfiddle;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Main {
- public static void main(String[] args) {
- Connection c = null;
- try {
- Class.forName("org.postgresql.Driver");
- c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres",
- "postgres", "postgres");
- System.out.println("Database open ok");
- System.out.println("Creating table ...");
- createTable(c);
- System.out.println("Inserting a customer ...");
- insert(c, "Théo Morales", "theo.morales@mail.com", 91748274, false);
- System.out.println("Updating customer Théo Morales...");
- update(c, "theo.morales@mail.com", "Théo Martin Morales", null, true);
- System.out.println("Deleting customer Théo Martin Morales...");
- delete(c, "theo.morales@mail.com");
- System.out.println("Dropping table customers...");
- dropTable(c);
- } catch (Exception e) {
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- if(c != null) {
- try {
- c.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- private static void createTable(Connection c) throws SQLException {
- Statement stmt = c.createStatement();
- String qry = "CREATE TABLE \"JAVA\".customers("
- + "id SERIAL PRIMARY KEY,"
- + "name VARCHAR(30) NOT NULL,"
- + "email VARCHAR(40) UNIQUE NOT NULL,"
- + "phone_number NUMERIC NOT NULL,"
- + "verified BOOLEAN DEFAULT(false)"
- + ");";
- stmt.execute(qry);
- stmt.close();
- System.out.println("Create table ok");
- }
- private static void insert(Connection c, String name, String email, long phoneNumber, boolean verified) throws SQLException {
- Statement stmt = c.createStatement();
- String qry = "INSERT INTO \"JAVA\".customers(name, email, phone_number, verified) "
- + "VALUES('" + name + "', '" + email + "', " + phoneNumber + ", " + verified + ");";
- stmt.execute(qry);
- stmt.close();
- System.out.println("Insert row ok");
- }
- private static void update(Connection c, String customerEmail, String newName, Long newPhoneNumber, Boolean verified) throws SQLException {
- Statement stmt = c.createStatement();
- String qry = "UPDATE \"JAVA\".customers SET"
- + (newName != null ? " name = '" + newName + "'" : "")
- + (newPhoneNumber != null ? ", phone_number = " + newPhoneNumber : "")
- + (verified != null ? ", verified = " + verified : "")
- + " WHERE email = '" + customerEmail + "';";
- stmt.executeUpdate(qry);
- stmt.close();
- System.out.println("Update row ok");
- }
- private static void delete(Connection c, String customerEmail) throws SQLException {
- Statement stmt = c.createStatement();
- String qry = "DELETE FROM \"JAVA\".customers WHERE email = '" + customerEmail + "';";
- stmt.execute(qry);
- stmt.close();
- System.out.println("Delete row ok");
- }
- private static void dropTable(Connection c) throws SQLException {
- Statement stmt = c.createStatement();
- stmt.execute("DROP TABLE \"JAVA\".customers;");
- stmt.close();
- System.out.println("Drop table ok");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement