Guest User

Untitled

a guest
Nov 1st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 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.SQLException;
  6. import java.sql.Statement;
  7.  
  8.  
  9. public class DBmanager {
  10.  
  11. private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver" ;
  12. private static final String url = "jdbc:derby:opdracht4;create=true" ;
  13. private Connection connection;
  14.  
  15.  
  16. public DBmanager() throws SQLException{
  17. try {
  18. Class.forName(driver).newInstance();
  19. connection = DriverManager.getConnection(url);
  20. createTable();
  21. testTable();
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace();
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. } catch (InstantiationException e) {
  27. e.printStackTrace();
  28. } catch (IllegalAccessException e) {
  29. e.printStackTrace();
  30. } finally {
  31. if (connection != null){
  32. DriverManager.getConnection("jdbc:derby:;shutdown=true");
  33. connection.close();
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41. public void createTable(){
  42.  
  43. String sql = "CREATE TABLE APP.URL ("
  44. + "createdOn DATE,"
  45. + "timesVisited INTEGER,"
  46. + "fullname VARCHAR(50) NOT NULL,"
  47. + "lastUsedOn DATE);";
  48. try {
  49. PreparedStatement stmt = connection.prepareStatement(sql);
  50. stmt.execute();
  51.  
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. public void testTable(){
  58. String sql = "show tables;";
  59.  
  60. try {
  61. Statement stmt = connection.createStatement();
  62. ResultSet rs = stmt.executeQuery(sql);
  63.  
  64. while (rs.next()){
  65. System.out.println(rs);
  66. }
  67.  
  68. } catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71.  
  72.  
  73. }
  74. }
Add Comment
Please, Sign In to add comment