Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. package greenDB;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. public class DatabaseManager
  12. {
  13. private Connection conn;
  14. private Statement stmt;
  15. private ResultSet rset;
  16.  
  17. public DatabaseManager(String username, String password)
  18. { // the constructor for the database manager
  19. // Connect to database and execute the SQL commands for creating and
  20. // initializing the Listings table.
  21. try
  22. {
  23. Class.forName("org.postgresql.Driver"); // Load the MySQL JDBC driver
  24. } catch (ClassNotFoundException e)
  25. {
  26. System.out.println("Failed to load JDBC/ODBC driver.");
  27. e.printStackTrace();
  28. return;
  29. }
  30.  
  31. try
  32. {
  33. // Connect to the database.
  34. // Give the whole URL as a parameter rather than using a variable
  35. conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres",
  36. "1234");
  37. stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); // Create
  38. // a
  39. // Statement
  40. // Execute the creation and initialization of table query
  41. DatabaseMetaData aboutDB = conn.getMetaData();
  42. String[] tableType =
  43. { "TABLE" };
  44. ResultSet rs = aboutDB.getTables(null, null, "listings", tableType);
  45. if (!inspectForTable(rs, "listings"))
  46. { // Find out if the table is already there
  47. // there is no table--make it from the initialization listing
  48. String[] SQL = initListingsTable(); // code for this method is below
  49. for (int i = 0; i < SQL.length; i++)
  50. {
  51. stmt.execute(SQL[i]);
  52. }
  53. }
  54. } catch (SQLException e)
  55. {
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. private String[] initListingsTable()
  61. {
  62. // Executable SQL commands for creating Listings table
  63. // inserting initial names and phone numbers.
  64. String[] SQL =
  65. { "create table \"PhoneBook\".Listings" + "("
  66. + "LAST_NAME varchar (16),"
  67. + "FIRST_NAME varchar (16),"
  68. + "AREA_CODE varchar(3),"
  69. + "PREFIX varchar(3),"
  70. + "SUFFIX varchar(4))",
  71. "INSERT INTO \"PhoneBook\".Listings "
  72. + "VALUES ('ANDERSON', 'JOHN', '314', '825', '1695'),"
  73. + " ('CABLES', 'WALLY', '212', '434', '9685'),"
  74. + " ('FRY', 'EDGAR', '415', '542', '5885'),"
  75. + " ('MARTIN', 'EDGAR', '665', '662', '9001'),"
  76. + " ('TUCKER', 'JOHN', '707', '696', '8541')"
  77.  
  78. };
  79.  
  80. // "create table \"PhoneBook\".Listings (" + "LAST_NAME varchar (16)," + "FIRST_NAME varchar (16),"
  81. // + "AREA_CODE varchar(3)," + "PREFIX varchar(3)," + "SUFFIX varchar(4))",
  82. // "insert into Listings values ('ANDERSON', 'JOHN', '314', '825', '1695')",
  83. // "insert into Listings values ('CABLES', 'WALLY', '212', '434', '9685')",
  84. // "insert into Listings values ('FRY', 'EDGAR', '415', '542', '5885')",
  85. // "insert into Listings values ('MARTIN', 'EDGAR', '665', '662', '9001')",
  86. // "insert into Listings values ('TUCKER', 'JOHN', '707', '696', '8541')", };
  87. return SQL;
  88. }
  89.  
  90. private boolean inspectForTable(ResultSet rs, String tableName) throws SQLException
  91. { // exception will be caught when method is used
  92. int i;
  93. ResultSetMetaData rsmd = rs.getMetaData(); // Get the ResultSetMetaData to use for the column headings
  94. int numCols = rsmd.getColumnCount(); // Get the number of columns in the result set
  95.  
  96. boolean more = rs.next();
  97. while (more)
  98. { // Get each row, fetching until end of the result set
  99. for (i = 1; i <= numCols; i++)
  100. {
  101. if (rsmd.getColumnLabel(i) == "table_name") // Loop through each row, getting the column
  102. // data looking for Tables
  103. if (rs.getString(i).equals(tableName)) // If the column is the TABLE_NAME, is it the
  104. // one we are looking for?
  105. {
  106. System.out.println("Found one that equals " + rs.getString(i));
  107. return true;
  108. }
  109. }
  110. System.out.println("");
  111. more = rs.next(); // Fetch the next result set row
  112. }
  113. return false;
  114. }
  115.  
  116. public void doGetQuery(String query)
  117. { // rather than the "getEntries" of the previous example
  118. try
  119. {
  120. rset = stmt.executeQuery(query);
  121. } catch (SQLException e)
  122. {
  123. e.printStackTrace();
  124. }
  125. }
  126.  
  127. public void doInsertQuery(String query)
  128. { // rather than the hard-coded "addEntry" of the previous example
  129. try
  130. {
  131. stmt.executeUpdate(query);
  132. } catch (SQLException e)
  133. {
  134. e.printStackTrace();
  135. }
  136. }
  137.  
  138. public ResultSet getResultSet()
  139. { // a new method that will let the GUI get the resultSet to manipulate it
  140. return rset;
  141. }
  142.  
  143. public void close(boolean remove)
  144. { // closes all open connections
  145. try
  146. {
  147. if (remove)
  148. stmt.execute("drop table Listings;");
  149. stmt.close();
  150. conn.close();
  151. } catch (SQLException e)
  152. {
  153. System.out.println("\n*** SQLException caught ***\n");
  154. e.printStackTrace();
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement