Guest User

dankSQL

a guest
Mar 12th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Date;
  9.  
  10. public class DBAccess {
  11. private Connection connect = null;
  12. private Statement statement = null;
  13. private PreparedStatement preparedStatement = null;
  14. private ResultSet resultSet = null;
  15.  
  16. public void readDataBase() throws Exception {
  17. try {
  18. // This will load the MySQL driver, each DB has its own driver
  19. Class.forName("com.mysql.jdbc.Driver");
  20. // Setup the connection with the DB
  21. connect = DriverManager
  22. .getConnection("jdbc:mysql://localhost/cs435_lab4?"
  23. + "user=final&password=project");
  24.  
  25. // Statements allow to issue SQL queries to the database
  26. statement = connect.createStatement();
  27.  
  28. question1(statement);
  29.  
  30.  
  31.  
  32. /*
  33. // Result set get the result of the SQL query
  34. resultSet = statement
  35. .executeQuery("select * from bus");
  36. writeResultSet(resultSet);
  37.  
  38.  
  39. // PreparedStatements can use variables and are more efficient
  40. preparedStatement = connect
  41. .prepareStatement("insert into feedback.comments values (default, ?, ?, ?, ? , ?, ?)");
  42. // "myuser, webpage, datum, summery, COMMENTS from feedback.comments");
  43. // Parameters start with 1
  44. preparedStatement.setString(1, "Test");
  45. preparedStatement.setString(2, "TestEmail");
  46. preparedStatement.setString(3, "TestWebpage");
  47. preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
  48. preparedStatement.setString(5, "TestSummary");
  49. preparedStatement.setString(6, "TestComment");
  50. preparedStatement.executeUpdate();
  51.  
  52. preparedStatement = connect
  53. .prepareStatement("SELECT myuser, webpage, datum, summery, COMMENTS from feedback.comments");
  54. resultSet = preparedStatement.executeQuery();
  55. writeResultSet(resultSet);
  56.  
  57. // Remove again the insert comment
  58. preparedStatement = connect
  59. .prepareStatement("delete from feedback.comments where myuser= ? ; ");
  60. preparedStatement.setString(1, "Test");
  61. preparedStatement.executeUpdate();
  62. resultSet = statement
  63. .executeQuery("select * from trip");
  64. writeMetaData(resultSet);
  65. */
  66.  
  67.  
  68. } catch (Exception e) {
  69. throw e;
  70. } finally {
  71. close();
  72. }
  73.  
  74. }
  75.  
  76. private ResultSet question1(Statement statement) throws SQLException{
  77. String startName = "Pittsburgh";
  78. String destName = "Canada";
  79. String date = "2/19/12";
  80.  
  81. ResultSet result = statement.executeQuery("SELECT t.StartLocationName, t.DestinationName, tof.TripDate, tof.ScheduledStartTime, tof.ScheduledArrivalTime, tof.DriverName, tof.BusID "+
  82. "FROM trip t, tripoffering tof "+
  83. "WHERE t.TripNumber = tof.TripNumber "+
  84. "AND t.StartLocationName = '" + startName +"' "+
  85. "AND t.DestinationName = '" + destName + "' " +
  86. "AND tof.TripDate = '" + date + "'");
  87. writeResultSet(result);
  88. return result;
  89. }
  90.  
  91. private ResultSet question2Add(Statement statement) throws SQLException{
  92.  
  93. String tripNumber = "1";
  94. String date = "2/19/12";
  95. String schedStartTime = "12:00PM";
  96. String schedArrivalTime = "2:00PM";
  97. String driverName = "Bob";
  98. String busId = "1";
  99.  
  100. ResultSet result = statement.executeQuery("SELECT t.StartLocationName, t.DestinationName, tof.TripDate, tof.ScheduledStartTime, tof.ScheduledArrivalTime, tof.DriverName, tof.BusID "+
  101. "FROM trip t, tripoffering tof "+
  102. "WHERE t.TripNumber = tof.TripNumber "+
  103. "AND t.StartLocationName = '" + startName +"' "+
  104. "AND t.DestinationName = '" + destName + "' " +
  105. "AND tof.TripDate = '" + date + "'");
  106. writeResultSet(result);
  107. return result;
  108. }
  109.  
  110. private void writeMetaData(ResultSet resultSet) throws SQLException {
  111. // Now get some metadata from the database
  112. // Result set get the result of the SQL query
  113.  
  114. System.out.println("The columns in the table are: ");
  115.  
  116. System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
  117. for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  118. System.out.println("Column " + i + " "
  119. + resultSet.getMetaData().getColumnName(i));
  120. }
  121. }
  122.  
  123. private void writeResultSet(ResultSet resultSet) throws SQLException {
  124. // ResultSet is initially before the first data set
  125. ResultSetMetaData rsmd = resultSet.getMetaData();
  126.  
  127. int totalColumns = rsmd.getColumnCount();
  128.  
  129. for(int j = 1; j <= totalColumns; j++){
  130. System.out.printf("%30s", rsmd.getColumnName(j));
  131. }
  132.  
  133. while (resultSet.next()) {
  134. System.out.println("");
  135. for(int i = 1; i <= totalColumns; i ++){
  136. System.out.printf("%30s", resultSet.getObject(i).toString());
  137. }
  138. }
  139. }
  140.  
  141. // You need to close the resultSet
  142. private void close() {
  143. try {
  144. if (resultSet != null) {
  145. resultSet.close();
  146. }
  147.  
  148. if (statement != null) {
  149. statement.close();
  150. }
  151.  
  152. if (connect != null) {
  153. connect.close();
  154. }
  155. } catch (Exception e) {
  156.  
  157. }
  158. }
  159. }
Add Comment
Please, Sign In to add comment