Guest User

Untitled

a guest
Jul 11th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. /**
  7. *
  8. * @author ec09002
  9. */
  10. import java.sql.Statement;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.ResultSetMetaData;
  15. import java.sql.SQLException;
  16.  
  17. public class QuestionTable {
  18.  
  19. public static void main(String[] args) throws SQLException {
  20. Connection db = null;
  21. String user = "vf300";
  22. String pass = "qmulmarina27";
  23. String URL = "jdbc:oracle:thin:@dbprojects.eecs.qmul.ac.uk:1521:CALVIN";
  24. try {
  25. Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
  26. } catch (ClassNotFoundException ce) {
  27. System.out.println("Error class not found: " + ce.getMessage());
  28. System.exit(0);
  29. } catch (IllegalAccessException ae) {
  30. System.out.println("Error illegal access: " + ae.getMessage());
  31. System.exit(0);
  32. } catch (InstantiationException ie) {
  33. System.out.println("Error creating: " + ie.getMessage());
  34. System.out.println("The Driver was not found. Please check driver location, classpath, username/password and server url settings");
  35. System.exit(0);
  36. }
  37.  
  38. try {
  39. db = DriverManager.getConnection(URL, user, pass);
  40. } catch (SQLException e) {
  41. }
  42.  
  43. Statement statement = null;
  44.  
  45. ResultSet resultSet = null;
  46.  
  47. ResultSetMetaData resultMetaData = null;
  48.  
  49. try {
  50. System.out.println("Creating QUESTION table");
  51. String create = "CREATE TABLE question (question_id NUMERIC(8) PRIMARY KEY, exam_id VARCHAR2(8), question_Number VARCHAR2(2), marks_Available NUMERIC(15))";
  52. System.out.println(create);
  53. statement = db.createStatement();
  54. statement.executeUpdate(create);
  55. System.out.println("Insert person 1");
  56. statement.executeUpdate("insert into question (question_id, exam_id, question_Number, marks_Available) values ('1', 'ex123', 1, 15)");
  57.  
  58. resultSet = statement.executeQuery("select * from question");
  59. resultMetaData = resultSet.getMetaData();
  60. int numCols = resultMetaData.getColumnCount();
  61. while (resultSet.next()) {
  62. for (int i = 1; i <= numCols; i++) {
  63. String colName = resultMetaData.getColumnName(i);
  64. String colVal = resultSet.getString(i);
  65. if (resultSet.wasNull()) {
  66. colVal = "and up";
  67. }
  68. System.out.println(colName + "=" + colVal);
  69. }
  70. System.out.println("\n");
  71. }
  72. } catch (SQLException e) {
  73. System.out.println("SQL Error: " + e.getMessage());
  74. } finally {
  75. System.out.println("Closing Connections...");
  76. try {
  77. db.close();
  78. } catch (SQLException e) {
  79. System.out.println("Can't close connection.");
  80. }
  81. }
  82.  
  83.  
  84. }
  85. }
Add Comment
Please, Sign In to add comment