Guest User

Untitled

a guest
Jan 25th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.lang.System.*;
  2. import java.nio.file.* ;
  3. import java.sql.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class TestDB {
  8. public static void main(String[] args) throws IOException {
  9. try {
  10. runTest();
  11. } catch (SQLException ex) {
  12. for (Throwable t : ex)
  13. t.printStackTrace();
  14. }
  15. }
  16.  
  17.  
  18. public static void runTest() throws SQLException, IOException {
  19. try (Connection conn = getConnection(); Statement stat = conn.createStatement()) {
  20. stat.executeUpdate("CREATE TABLE G (id BIGINT,name CHAR(20))");
  21. stat.executeUpdate("ALTER TABLE G ALTER COLUMN id SET DEFAULT NEXTVAL('seq')");
  22. for( int i=0;i<5;i++)
  23. stat.executeUpdate("INSERT INTO public.g (id,name) VALUES(DEFAULT,'Hello, world2')");
  24. try (ResultSet result=stat.executeQuery("SELECT NEXTVAL('seq')"))
  25. {
  26. while(result.next())
  27. System.out.println(result.getString(1));
  28. }
  29. ////stat.executeUpdate("DROP TABLE G");
  30. // stat.executeUpdate("DROP TABLE Greetings");
  31. }
  32. }
  33. public static Connection getConnection()throws SQLException,IOException
  34. {
  35. Properties props=new Properties();
  36. try (InputStream in =
  37. Files.newInputStream(Paths.get("Database.properties")))
  38. {
  39. props.load(in);
  40. }
  41. String drivers = props.getProperty("jdbc.drivers");
  42. if(drivers!=null)
  43. {
  44. System.setProperty("jdbc.drivers",drivers);
  45. }
  46. String url=props.getProperty("jdbc.url");
  47. String username = props.getProperty("jdbc.username");
  48. String password = props.getProperty("jdbc.password");
  49.  
  50. return DriverManager.getConnection(url,username,password);
  51. }
  52. }
Add Comment
Please, Sign In to add comment