Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. import org.json.simple.JSONObject;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6.  
  7. public class DB {
  8. private static Connection conn1, conn2;
  9. private static Statement statmt;
  10. private static ResultSet resSet;
  11.  
  12. public static void DB() throws ClassNotFoundException, SQLException
  13. {
  14. Class.forName("org.sqlite.JDBC");
  15. conn1 = DriverManager.getConnection("jdbc:sqlite:conv.s3db");
  16. conn2 = DriverManager.getConnection("jdbc:sqlite:msgs.s3db");
  17.  
  18. }
  19.  
  20. public static void createDB() throws ClassNotFoundException, SQLException
  21. {
  22. statmt = conn1.createStatement();
  23. statmt.execute("CREATE TABLE if not exists 'conversations' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, " +
  24. "'surname name' text, 'status' text, 'date' text);");
  25. statmt.execute("CREATE TABLE if not exists 'messages' ('id of message' INTEGER PRIMARY KEY AUTOINCREMENT, " +
  26. "'id of conversation' INT, 'surname name' text, 'message' text);");
  27. }
  28.  
  29. public static void writeConv(String name, String status, String date) throws SQLException
  30. {
  31. statmt.execute("INSERT INTO 'conversations' ('surname name', 'status', 'date') VALUES ('" + name
  32. + "', '" + status + "', '" + date + "'); ");
  33.  
  34. }
  35.  
  36. public static void writeMsg(String name, String date, String msg, int conversationId) throws SQLException {
  37. statmt.execute("INSERT INTO 'messages' ('id of conversation', 'surname name', 'message') VALUES ('" + conversationId
  38. + "', '" + name + "', '" + msg + "'); ");
  39.  
  40. }
  41.  
  42. public static String readConvInfo(int conversationId) throws SQLException {
  43. resSet.beforeFirst();
  44. resSet = statmt.executeQuery("SELECT COUNT (*) FROM conversations").beforeFirst();
  45. System.out.println(resSet.getInt(1));
  46.  
  47. resSet.first();
  48. JSONObject obj = new JSONObject();
  49. while (resSet.next()) {
  50. obj.put("surname name", resSet.getString("surname name"));
  51. obj.put("status", resSet.getString("status"));
  52. obj.put("date", resSet.getString("date"));
  53. obj.put("id", resSet.getInt("id"));
  54. }
  55. return obj.toJSONString();
  56. }
  57.  
  58. public static String readMsg(int msgId) throws SQLException {
  59. resSet = statmt.executeQuery("SELECT * FROM messages WHERE 'id of message' LIKE '" + msgId + "'");
  60. JSONObject obj = new JSONObject();
  61. obj.put("message", resSet.getString("message"));
  62. return obj.toJSONString();
  63. }
  64.  
  65. public static String readCnvr(int conversationId) throws SQLException {
  66. resSet = statmt.executeQuery("SELECT * FROM messages WHERE 'id of conversation' LIKE '" + conversationId + "'");
  67. ArrayList<String> msgs = new ArrayList<>();
  68. while (resSet.next()) {
  69. msgs.add(resSet.getString("message"));
  70. }
  71. JSONObject obj = new JSONObject();
  72. obj.put("message", msgs);
  73. return obj.toJSONString();
  74. }
  75. }
  76. /*
  77. import java.sql.Connection;
  78. import java.sql.DriverManager;
  79. import java.sql.ResultSet;
  80. import java.sql.SQLException;
  81. import java.sql.Statement;
  82.  
  83.  
  84. public class conn {
  85. public static Connection conn;
  86. public static Statement statmt;
  87. public static ResultSet resSet;
  88.  
  89. // --------ПОДКЛЮЧЕНИЕ К БАЗЕ ДАННЫХ--------
  90. public static void Conn() throws ClassNotFoundException, SQLException
  91. {
  92. conn = null;
  93. Class.forName("org.sqlite.JDBC");
  94. conn = DriverManager.getConnection("jdbc:sqlite:TEST1.s3db");
  95.  
  96. System.out.println("База Подключена!");
  97. }
  98.  
  99. // --------Создание таблицы--------
  100. public static void CreateDB() throws ClassNotFoundException, SQLException
  101. {
  102. statmt = conn.createStatement();
  103. statmt.execute("CREATE TABLE if not exists 'users' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' text, 'phone' INT);");
  104.  
  105. System.out.println("Таблица создана или уже существует.");
  106. }
  107.  
  108. // --------Заполнение таблицы--------
  109. public static void WriteDB() throws SQLException
  110. {
  111. statmt.execute("INSERT INTO 'users' ('name', 'phone') VALUES ('Petya', 125453); ");
  112. statmt.execute("INSERT INTO 'users' ('name', 'phone') VALUES ('Vasya', 321789); ");
  113. statmt.execute("INSERT INTO 'users' ('name', 'phone') VALUES ('Masha', 456123); ");
  114.  
  115. System.out.println("Таблица заполнена");
  116. }
  117.  
  118. // -------- Вывод таблицы--------
  119. public static void ReadDB() throws ClassNotFoundException, SQLException
  120. {
  121. resSet = statmt.executeQuery("SELECT * FROM users");
  122.  
  123. while(resSet.next())
  124. {
  125. int id = resSet.getInt("id");
  126. String name = resSet.getString("name");
  127. String phone = resSet.getString("phone");
  128. System.out.println( "ID = " + id );
  129. System.out.println( "name = " + name );
  130. System.out.println( "phone = " + phone );
  131. System.out.println();
  132. }
  133.  
  134. System.out.println("Таблица выведена");
  135. }
  136.  
  137. // --------Закрытие--------
  138. public static void CloseDB() throws ClassNotFoundException, SQLException
  139. {
  140. conn.close();
  141. statmt.close();
  142. resSet.close();
  143.  
  144. System.out.println("Соединения закрыты");
  145. }
  146.  
  147. }
  148. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement