Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. //MetaSQL.class
  2.  
  3. package org.biohazard.msql;
  4.  
  5. import java.io.File;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11.  
  12. import org.hsqldb.Server;
  13.  
  14. public class MetaSQL {
  15. Server hsqlServer;
  16. public Connection connection;
  17. private String a;
  18. private String b;
  19. private String c;
  20.  
  21. public MetaSQL(String name, String username, String password) {
  22. a = name;
  23. b = username;
  24. c = password;
  25. }
  26.  
  27. private Connection getConnection() {
  28. try {
  29. if (connection == null || connection.isClosed()) {
  30. try {
  31. Class.forName("org.hsqldb.jdbcDriver");
  32. return DriverManager.getConnection(
  33. "jdbc:hsqldb:hsql://localhost/" + a, "sa", "");
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. } catch (ClassNotFoundException e1) {
  37. e1.printStackTrace();
  38. }
  39. } else {
  40. return connection;
  41. }
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. return null;
  46. }
  47.  
  48. public void startServer(String path) {
  49. try {
  50. hsqlServer = new Server();
  51. hsqlServer.setLogWriter(null);
  52. hsqlServer.setSilent(true);
  53. System.out.println("DatabaseServerPath: " + path);
  54. hsqlServer.setDatabaseName(0, a);
  55. hsqlServer.setDatabasePath(0, "file:" + path);
  56. hsqlServer.start();
  57. try {
  58. Class.forName("org.hsqldb.jdbcDriver");
  59. connection = DriverManager.getConnection(
  60. "jdbc:hsqldb:hsql://localhost/" + a, b, c);
  61. connection.prepareStatement(
  62. "create table testtable ( id INTEGER, "
  63. + "name VARCHAR(255));").execute();
  64. connection.prepareStatement(
  65. "insert into testtable(id, name) "
  66. + "values (1, 'testvalue');").execute();
  67. ResultSet rs = connection.prepareStatement(
  68. "select * from testtable;").executeQuery();
  69. rs.next();
  70. System.out.println("Id: " + rs.getInt(1) + " Name: "
  71. + rs.getString(2));
  72.  
  73. connection.prepareStatement("drop table testtable;").execute();
  74. createDatabase(path);
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. } catch (ClassNotFoundException e) {
  78. e.printStackTrace();
  79. } finally {
  80. // Closing the connection
  81. // if (connection != null)
  82. // try
  83. // {
  84. // connection.close();
  85. // }
  86. // catch (SQLException e)
  87. // {
  88. // // TODO Auto-generated catch block
  89. // e.printStackTrace();
  90. // }
  91.  
  92. }
  93. } finally {
  94. // Closing the server
  95. // if (hsqlServer != null)
  96. // hsqlServer.stop();
  97. }
  98. }
  99.  
  100. public void createDatabase(String savePath) {
  101. }
  102.  
  103. // TODO: 4 Commands
  104. public void createTable(String tablename) {
  105. executeCommand("CREATE TABLE "
  106. + tablename
  107. + " (DATA VARCHAR(100),X VARCHAR(100),Y VARCHAR(100),Z VARCHAR(100))");
  108. }
  109.  
  110. public void insertTable(String tablename, int[] blockPos, String data) {
  111. executeCommand("INSERT INTO " + tablename + " VALUES (" + blockPos[0]
  112. + ", " + blockPos[1] + "," + blockPos[2] + "," + data + ")");
  113. }
  114.  
  115. public void updateTable(String tablename, String[] pos, String oldvar,
  116. String newvar) {
  117. executeCommand("UPDATE Persons SET X='" + pos[0] + "', Y='" + pos[1]
  118. + "', Z='" + pos[2] + "' WHERE X='" + pos[0]
  119. + "' AND FirstName='" + pos[1] + "'AND FirstName='" + pos[2]
  120. + "'");
  121. }
  122.  
  123. public void deleteItem(String tablename, int[] blockPos) {
  124. executeCommand("DELETE FROM " + tablename + " WHERE X='" + blockPos[0]
  125. + "' AND Y='" + blockPos[1] + "' AND Z='" + blockPos[3] + "'");
  126. }
  127.  
  128. public ResultSet executeQuery(String command) {
  129. try {
  130. Connection c = getConnection();
  131. return c.prepareStatement(command).executeQuery();
  132. } catch (SQLException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. }
  136. return null;
  137. }
  138.  
  139. public void executeCommand(String command) {
  140. try {
  141. Connection c = getConnection();
  142. c.prepareStatement(command).execute();
  143. } catch (SQLException e) {
  144. // TODO Auto-generated catch block
  145. e.printStackTrace();
  146. }
  147. }
  148.  
  149. public PreparedStatement prepareStatement(String command) {
  150. try {
  151. Connection c = getConnection();
  152. return c.prepareStatement(command);
  153. } catch (SQLException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. }
  157. return null;
  158. }
  159.  
  160. public void createDatabase(File savePath) {
  161. createDatabase(savePath.getAbsolutePath());
  162. }
  163.  
  164. public void shutDown() {
  165. if (hsqlServer != null)
  166. hsqlServer.stop();
  167. }
  168.  
  169. }
  170.  
  171. //Metaloader.class (Which the mods shall use)
  172.  
  173. package org.biohazard.msql;
  174.  
  175. import java.io.File;
  176.  
  177. public class Metaloader {
  178. public Metaloader(String name, String username, String password) {
  179. msql = new MetaSQL(name, username, password);
  180. }
  181.  
  182. public void createDB(String path, String username, String password) {
  183. msql.createDatabase(new File(path));
  184. }
  185.  
  186. public void startDB(String path) {
  187. msql.startServer(path);
  188. }
  189.  
  190. public void createTable(String tablename) {
  191. msql.createTable(tablename);
  192. }
  193.  
  194. public void deleteItem(String tablename, int[] BlockPos) {
  195. msql.deleteItem(tablename, BlockPos);
  196. }
  197.  
  198. public void insertItem(String tablename, int[] BlockPos, String Data) {
  199. msql.insertTable(tablename, BlockPos, Data);
  200. }
  201.  
  202. public void stopDB() {
  203. msql.shutDown();
  204. }
  205.  
  206. private MetaSQL msql;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement