Advertisement
Guest User

Untitled

a guest
May 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package com.dmitryporyvkin;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Main {
  6. static final String URL = "jdbc:postgresql://localhost:5434/testdb";
  7. static final String JDBC_DRIVER = "org.postgresql.Driver";
  8. static final String USER = "postgres";
  9. static final String PASSWORD = "1234";
  10.  
  11. public static void main(String[] args) throws SQLException , ClassNotFoundException{
  12. Connection connection = null;
  13. Statement statement = null;
  14. ResultSet resultSet;
  15. try{
  16.  
  17. Class.forName(JDBC_DRIVER);
  18. connection = DriverManager.getConnection(URL , USER , PASSWORD);
  19. statement = connection.createStatement();
  20.  
  21. statement.executeUpdate("DROP TABLE accounts");
  22. statement.executeUpdate("create table accounts("
  23. + "id SERIAL primary key , "
  24. + " username varchar(225) unique not null ," +
  25. " firstName varchar(225) not null , lastName varchar(225) not null) "
  26. );
  27.  
  28. statement.executeUpdate("insert into accounts (username, firstName, lastName) " +
  29. "values ('firstuser','Ivan','Petrov')," +
  30. "('seconduser','Oleg','Tarasov')," +
  31. "('third','Nikita','Chernov');");
  32.  
  33. statement.executeUpdate("update accounts set lastName = 'Sidorov' where username = 'seconduser' ");
  34. } catch (SQLException e) {
  35. System.out.println("Failed connection to database.");
  36. e.printStackTrace();
  37. }
  38. catch (ClassNotFoundException e) {
  39. e.printStackTrace();
  40. System.out.println("Driver not found.");
  41. }
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement