Guest User

Untitled

a guest
Jan 7th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. public static void main(String [] args) {
  2. final String user = "user";
  3. final String password = "password";
  4. final String url = "org.apache.derby.jdbc.EmbeddedDriver";
  5. try { // create
  6. Class.forName(url).newInstance();
  7. Properties properties = new Properties();
  8. properties.put("user", user);
  9. properties.put("password", password);
  10. Connection connection = DriverManager.getConnection("jdbc:derby:hello;create=true", properties);
  11. connection.setAutoCommit(false);
  12. Statement statement = connection.createStatement();
  13. statement.execute("create table t(name varchar(40), age int)");
  14. statement.execute("insert into t values('John', 26)");
  15. statement.execute("insert into t values ('Smith', 32)");
  16. ResultSet resultSet = statement.executeQuery("SELECT name, age FROM t ORDER BY age");
  17. while (resultSet.next()) {
  18. System.out.println("id:" + resultSet.getString(1));// works fine
  19. }
  20. // experiment begins
  21. // experiment ends
  22. statement.execute("drop table t");
  23. resultSet.close();
  24. statement.close();
  25. connection.commit();
  26. connection.close();
  27. try {
  28. DriverManager.getConnection("jdbc:derby:;shutdown=true");
  29. } catch (SQLException se) {
  30. System.out.println("Database shutdown with exception"); // why exception here?
  31. }
  32. } catch (Throwable e) {
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. DriverManager.getConnection("jdbc:derby:;shutdown=true");
  38.  
  39. Database shutdown with exception
  40.  
  41. // experiment begins
  42. try {
  43. WebRowSet wrs = new WebRowSetImpl();
  44. wrs.setUsername(user);
  45. wrs.setPassword(password);
  46. wrs.setUrl("jdbc:derby:hello;");
  47. wrs.setCommand("SELECT * FROM t");
  48. System.out.println("step1");
  49. wrs.execute();
  50. System.out.println("step2");
  51. FileOutputStream fileOutputStream = new FileOutputStream("customers.xml");
  52. wrs.writeXml(fileOutputStream);
  53. fileOutputStream.close();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. //experiment ends
Add Comment
Please, Sign In to add comment