Advertisement
Guest User

Untitled

a guest
Oct 18th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public class CreatingConnection {
  2.  
  3. private static Connection getDBConnection() {
  4. Connection dbConnection = null;
  5. try {
  6. Class.forName("org.postgresql.Driver");
  7. } catch (ClassNotFoundException e) {
  8. System.out.println(e.getMessage());
  9. }
  10. try {
  11. dbConnection = DriverManager.getConnection("jdbc:postgresql::port/db","postgres", "user");
  12. return dbConnection;
  13. } catch (SQLException e) {
  14. System.out.println(e.getMessage());
  15. }
  16. return dbConnection;
  17. }
  18. static void createDbUserTable() throws SQLException {
  19. Connection dbConnection = null;
  20. Statement statement = null;
  21.  
  22. String createTableSQL = "CREATE TABLE DBUSER("
  23. + "USER_ID NUMBER(5) NOT NULL, "
  24. + "USERNAME VARCHAR(20) NOT NULL, "
  25. + "MESSAGE VARCHAR(20) NOT NULL, "
  26. + ")";
  27.  
  28. try {
  29. dbConnection = getDBConnection();
  30. System.out.println(dbConnection);
  31. statement = dbConnection.createStatement();
  32. statement.execute(createTableSQL);
  33. System.out.println("Table "dbuser" is created!");
  34. } catch (SQLException e) {
  35. System.out.println(e.getMessage());
  36. } finally {
  37. if (statement != null) {
  38. statement.close();
  39. }
  40. if (dbConnection != null) {
  41. dbConnection.close();
  42. }
  43. }
  44. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement