Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1.  
  2. import java.sql.DriverManager; // DriverManager for establishing linkage to DB
  3. import java.sql.SQLException; // SQLException handles exception for DB classes
  4. import java.sql.Connection; // Connection establishes a logon to the DB
  5. import java.sql.Statement; // Holds and executes the sql statements
  6. import java.sql.ResultSet; // Holds the results of a query
  7. import java.util.Date;
  8.  
  9. public class Hw6Pr2
  10. {
  11.  
  12. /**
  13. * Method: main<br>
  14. * Responsibilities: .
  15. *
  16. * @param args
  17. */
  18. public static void main(String[] args)
  19. {
  20. System.out.println("Starting Connection Process");
  21. String myConnStr; // holds the connection string
  22. String myUser; // holds the user name
  23. String myPasswd; // holds the user password
  24. String tableCreateStatement; // holds sql statement to be run
  25. String populateTableStatement;
  26. String populateTableStatementTwo;
  27. String populateTableStatementSelect;
  28.  
  29.  
  30.  
  31. try
  32. {
  33. //Link the class to the jdbc driver
  34. System.out.println("Link this class to the jdbc driver");
  35. DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  36.  
  37. //connection to the database
  38. myConnStr = "jdbc:oracle:thin:@pythia.etsu.edu:1521:csdb";
  39. myUser = "";
  40. myPasswd = "";
  41. Connection myCon = null;
  42. myCon = DriverManager.getConnection(myConnStr, myUser, myPasswd);
  43.  
  44.  
  45. Statement myStmt = null;
  46. myStmt = myCon.createStatement();
  47. Statement myTableStatement = null;
  48. myTableStatement = myCon.createStatement();
  49. Statement myTableStatementTwo = null;
  50. myTableStatementTwo = myCon.createStatement();
  51. Statement myTableStatementSelect = null;
  52. myTableStatementSelect = myCon.createStatement();
  53.  
  54. ResultSet tablePopulatedResults = null;
  55. ResultSet tableCreateResults = null;
  56. tableCreateStatement = "create table ATTEND2( " +
  57. "STUDENT, " +
  58. "COURSE, " +
  59. "SECTION, " +
  60. "TERM, " +
  61. "ABSENCES number (2,0), " +
  62. "STATUS varchar2(12) not null, " +
  63. "primary key (STUDENT, COURSE, SECTION, TERM, ABSENCES), " +
  64. "foreign key (STUDENT, COURSE, SECTION, TERM) references TAKES on delete cascade)";
  65. // the sql statement to run
  66. populateTableStatement = "INSERT INTO ATTEND2 VALUES "+
  67. "(12345678, 'CSCI2200', '001', '083', 5, 'STUDENT')";
  68. populateTableStatementTwo = "INSERT INTO ATTEND2 VALUES "+
  69. "(87654321, 'MATH2100', '201', '073', 11, 'STUDENT')";
  70.  
  71. populateTableStatementSelect = "SELECT * FROM ATTEND5";
  72.  
  73. tableCreateResults = myStmt.executeQuery(tableCreateStatement);
  74.  
  75. myTableStatement.executeQuery(populateTableStatement);
  76. myTableStatementTwo.executeQuery(populateTableStatementTwo);
  77.  
  78. tablePopulatedResults= myTableStatementSelect.executeQuery(populateTableStatementSelect);
  79.  
  80.  
  81. while (tablePopulatedResults.next())
  82. {
  83.  
  84.  
  85. int student = tablePopulatedResults.getInt("STUDENT");
  86. String course = tablePopulatedResults.getString("COURSE");
  87. String section = tablePopulatedResults.getString("COURSE");
  88. String term = tablePopulatedResults.getString("COURSE");
  89. int absences = tablePopulatedResults.getInt("ABSENCES");
  90. String status = tablePopulatedResults.getString("COURSE");
  91.  
  92. System.out.println(student + " " + course + " " + section + " " + term + " " + absences + " " + status);
  93.  
  94.  
  95. } //end while
  96.  
  97. tableCreateResults.close();
  98. tablePopulatedResults.close();
  99. myTableStatement.close();
  100. myTableStatementTwo.close();
  101. myTableStatementSelect.close();
  102. myStmt.close();
  103. myCon.close();
  104.  
  105. } // end try
  106. catch (SQLException mySqlError)
  107. {
  108. //error handling
  109. System.out.println("An Error has occured, dumping error stack");
  110. mySqlError.printStackTrace();
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement