Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import javax.management.Query;
  2. import java.sql.*;
  3. /*
  4. Kevin Grossi
  5. CS435
  6. Lab 4
  7. */
  8.  
  9. public class Main
  10. {
  11.  
  12. public static void main(String[] args)
  13. {
  14.  
  15. String varSQL, varSQL2;
  16. try
  17. {
  18. //1. Get Connection to database
  19. Class.forName("com.mysql.jdbc.Driver").newInstance();
  20. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lab4", "root", "kg34vin");
  21.  
  22. if(myConn != null)
  23. {
  24. System.out.print("Connection Successful.");
  25. }
  26. System.out.println();
  27.  
  28. //2.Create Statement
  29.  
  30. /*
  31. Question 1
  32. varSQL = "Select Top.ScheduledStartTime, Top.ScheduledArrivalTime, Top.DriverName, Top.BusID " +
  33. "From Trip T, TripOffering Top " +
  34. "Where T.TripNumber = Top.TripNumber";
  35.  
  36. Question 2A
  37. varSQL = "delete" +
  38. "From TripOffering " +
  39. "Where TripNumber = 123456 AND Date LIKE '2016-12-01' AND ScheduledStartTime LIKE '8:00:00'";
  40.  
  41. Question 2B
  42. varSQL = "insert into TripOffering (TripNumber, Date, ScheduledStartTime, ScheduledArrivalTime, DriverName, BusID)"
  43. + "values (?,?,?,?,?,?)";
  44. //? can be the values of the inserted item
  45.  
  46. Question 2C
  47. varSQL = "update TripOffering set DriverName = ? where DriverName = ?";
  48. //? can be the value updated
  49.  
  50. Question 2D
  51. varSQL = "update TripOffering set BusID = ? where BusID = ?";
  52. //? can be the value updated
  53.  
  54. Question 3
  55. varSQL = "Select * " +
  56. "From TripStopInfo";
  57.  
  58. Question 5
  59. varSQL = "insert into Driver (DriverName, DriverTelephoneNumber)"
  60. + ""values(?,?)";
  61.  
  62. Question 6
  63. varSQL = "insert into Bus (BusID, Model, Year)"
  64. + "values(?,?,?)"
  65.  
  66. Question 7
  67. varSQL = "delete" +
  68. "From Bus " +
  69. "Where BusID LIKE 'O213' AND Model LIKE 'Aston Martin' AND Year = 1985";
  70.  
  71. */
  72. Statement myState = myConn.createStatement();
  73. varSQL2 = "Select * " +
  74. "From Bus";
  75.  
  76. System.out.println("Statement Created.");
  77.  
  78. //3. Execute Query
  79. ResultSet rs = myState.executeQuery(varSQL2);
  80. ResultSetMetaData rsMeta = rs.getMetaData();
  81. System.out.println("Query Executed");
  82.  
  83. //4. Process the result set
  84. int varColCount = rsMeta.getColumnCount();
  85.  
  86. for (int col = 1; col <= varColCount; col++)
  87. System.out.print(rsMeta.getColumnName(col) + '\t');
  88.  
  89. System.out.println();
  90. while (rs.next())
  91. {
  92. for (int col = 1; col <= varColCount; col++)
  93. System.out.print (rs.getString(col) + '\t');
  94. System.out.println();
  95. }
  96.  
  97. rs.close();
  98. myState.close();
  99. myConn.close();
  100.  
  101. }
  102. catch (Exception exc)
  103. {
  104. exc.printStackTrace();
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement