Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class DBGuy {
  4. private Connection c = null;
  5.  
  6. public DBGuy() {
  7. try {
  8. Class.forName("org.postgresql.Driver");
  9. c = DriverManager
  10. .getConnection("jdbc:postgresql://localhost:5432/testdb",
  11. "user", "pass");
  12. } catch(Exception e) {
  13. e.printStackTrace();
  14. System.exit(0);
  15. }
  16. System.out.println("Opened database sucessfully.");
  17. }
  18.  
  19. public void createCompanyTable() {
  20. Statement s = null;
  21. try {
  22. s = c.createStatement();
  23. String sql = "CREATE TABLE COMPANY (" +
  24. "ID INT PRIMARY KEY NOT NULL," +
  25. "NAME TEXT NOT NULL," +
  26. "AGE INT NOT NULL," +
  27. "ADDRESS CHAR(50)," +
  28. "SALARY REAL)";
  29. s.executeUpdate(sql);
  30. s.close();
  31. } catch(Exception e) {
  32. e.printStackTrace();
  33. System.exit(0);
  34. }
  35. System.out.println("Company table created successfully.");
  36. }
  37.  
  38. public void deleteCompanyTable() {
  39. Statement s = null;
  40. try {
  41. s = c.createStatement();
  42. String sql = "DROP TABLE IF EXISTS COMPANY";
  43. s.execute(sql);
  44. } catch(Exception e) {
  45. e.printStackTrace();
  46. System.exit(0);
  47. }
  48. }
  49.  
  50. public void closeConnection() {
  51. try {
  52. c.close();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. System.exit(0);
  56. }
  57. System.out.println("Connection closed.");
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement