Advertisement
Guest User

Untitled

a guest
May 1st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1.  
  2. package domino.server;
  3.  
  4. import java.rmi.RemoteException;
  5. import java.rmi.server.UnicastRemoteObject;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. import domino.DominoStorageIface;
  16.  
  17. public class DominoStorageImpl extends UnicastRemoteObject implements DominoStorageIface {
  18.  
  19. Connection conn;
  20.  
  21. protected DominoStorageImpl(String s) throws Exception {
  22. String user = "sa";
  23. String password = "";
  24. String url = "jdbc:hsqldb:file:" + s;
  25.  
  26. loadDbDriver("org.hsqldb.jdbc.JDBCDriver");
  27.  
  28. conn = DriverManager.getConnection(url, user, password);
  29. createTables(conn);
  30. }
  31.  
  32. // TODO Auto-generated constructor stub
  33.  
  34. private static void loadDbDriver(String driverClassName) throws Exception {
  35. try {
  36. Class.forName(driverClassName);
  37. } catch (Exception e) {
  38. System.err.println("ERROR: failed to load JDBC driver.");
  39. throw e;
  40. }
  41. }
  42.  
  43. private static void createTables(Connection conn) throws SQLException {
  44. try (Statement stat = conn.createStatement();) {
  45. stat.executeUpdate("drop table if exists Dominos;");
  46. stat.executeUpdate("create table Dominos (name varchar(80), Domino varchar(20), idx int);");
  47. }
  48. }
  49.  
  50. @Override
  51. public void save(String userName, List<String> dominos) throws RemoteException {
  52. try (PreparedStatement st = conn.prepareStatement("DELETE FROM Dominos WHERE name = ?;");) {
  53. st.setString(1, userName);
  54. st.executeUpdate();
  55. } catch (SQLException exc) {
  56. System.out.println(exc.getMessage());
  57. }
  58. for (int i = 0; i < dominos.size(); i++) {
  59. try (PreparedStatement st = conn.prepareStatement("INSERT INTO Dominos (name, Domino, idx) VALUES (?, ?, ?);");) {
  60. addDomino(st, userName, dominos.get(i), i + 1);
  61. st.executeUpdate();
  62. } catch (SQLException exc) {
  63. System.out.println(exc.getMessage());
  64. }
  65. }
  66.  
  67. }
  68.  
  69. private static void addDomino(PreparedStatement prep, String name, String domino, int idx) {
  70. try {
  71. prep.setString(1, name);
  72. prep.setString(2, domino);
  73. prep.setInt(3, idx);
  74. prep.addBatch();
  75. } catch (SQLException exc) {
  76. System.out.println(exc.getMessage());
  77. }
  78. }
  79.  
  80. @Override
  81. public List<String> load(String userName) throws RemoteException {
  82. List<String> dominos = new ArrayList<>();
  83. String domino = "";
  84. try (Statement stat = conn.createStatement();
  85. ResultSet rs = stat.executeQuery("select * from Domino where name = userName;");) {
  86. while (rs.next()) {
  87. String name = rs.getString("userName");
  88.  
  89. System.out.printf("domino = %s", domino);
  90.  
  91. dominos.add(domino);
  92. }
  93. } catch (SQLException exc) {
  94. System.out.println(exc.getMessage());
  95. }
  96.  
  97. return dominos;
  98.  
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement