Advertisement
Guest User

Untitled

a guest
May 30th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. // ## INITIALIZATION SECTION ##
  2. // Include the java SQL classes
  3. import java.sql.*;
  4.  
  5. public class Database {
  6. public static void main(String[] args) throws SQLException {
  7. /* Strings fuer die Verbindung */
  8. String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  9. String dbName = "testdb";
  10. String connectionURL = "jdbc:derby:" + dbName + ";create=true";
  11.  
  12. //Attempts to establish a connection to the given database URL. The DriverManager attempts
  13. //to select an appropriate driver from the set of registered JDBC drivers.
  14. Connection conn = DriverManager.getConnection(connectionURL);
  15. Statement s = conn.createStatement();
  16. // ResultSet result;
  17.  
  18. try { // Try-Block um eventuelle Fehler abzufangen
  19.  
  20. // ## LOAD DRIVER SECTION ##
  21. try {
  22. Class.forName(driver); // Treiber laden
  23. System.out.println(driver + " loaded. ");
  24. } catch (java.lang.ClassNotFoundException e) {
  25. System.err.print("ClassNotFoundException: ");
  26. System.err.println(e.getMessage());
  27. }
  28.  
  29. // ## INITIAL SQL SECTION ##
  30. //s.execute("insert into DBUSER(Name) values ('Glato')");
  31.  
  32.  
  33. ResultSet result = s.executeQuery("select * from DBUSER");
  34. while (result.next()) { // ausgabe aller Ergebnisse
  35. System.out.println(result.getString(1) + " "
  36. + result.getString(2));
  37. }
  38.  
  39. s.close();
  40. conn.close();
  41. System.out.println("Closed connection");
  42.  
  43. // ## DATABASE SHUTDOWN SECTION ##
  44. /***
  45. * In embedded mode, an application should shut down Derby. Shutdown
  46. * throws the XJ015 exception to confirm success.
  47. ***/
  48. if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
  49. boolean gotSQLExc = false;
  50. try {
  51. DriverManager.getConnection("jdbc:derby:;shutdown=true");
  52. } catch (SQLException se) {
  53. if (se.getSQLState().equals("XJ015")) {
  54. gotSQLExc = true;
  55. }
  56. }
  57. if (!gotSQLExc) {
  58. System.out.println("Database did not shut down normally");
  59. } else {
  60. System.out.println("Database shut down normally");
  61. }
  62. }
  63.  
  64. // Beginning of the primary catch block: uses errorPrint method
  65. } catch (Throwable e) {
  66. /*
  67. * Catch all exceptions and pass them to the exception reporting
  68. * method
  69. */
  70. System.out.println(" . . . exception thrown:");
  71. errorPrint(e);
  72.  
  73. }
  74. System.out.println("program ending.");
  75. }
  76.  
  77. static void errorPrint(Throwable e) {
  78. if (e instanceof SQLException)
  79. SQLExceptionPrint((SQLException) e);
  80. else {
  81. System.out.println("A non SQL error occured.");
  82. e.printStackTrace();
  83. }
  84. } // END errorPrint
  85.  
  86. static void SQLExceptionPrint(SQLException sqle) {
  87. while (sqle != null) {
  88. System.out.println("\n---SQLException Caught---\n");
  89. System.out.println("SQLState: " + (sqle).getSQLState());
  90. System.out.println("Severity: " + (sqle).getErrorCode());
  91. System.out.println("Message: " + (sqle).getMessage());
  92. sqle.printStackTrace();
  93. sqle = sqle.getNextException();
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement