Advertisement
Guest User

sql class

a guest
Dec 11th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package me.davidev.punir.utils;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8.  
  9. public class SQL
  10. {
  11. private Connection conn;
  12. private File file;
  13. private Statement stmt;
  14.  
  15. private SQL(File f)
  16. {
  17. this.file = f;
  18. try
  19. {
  20. Class.forName("org.sqlite.JDBC");
  21. this.conn = DriverManager.getConnection("jdbc:sqlite:" + this.file);
  22. this.stmt = this.conn.createStatement();
  23. }
  24. catch (Exception e)
  25. {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. private SQL(String urlconn)
  31. {
  32. try
  33. {
  34. this.conn = DriverManager.getConnection(urlconn);
  35. this.stmt = this.conn.createStatement();
  36. }
  37. catch (Exception e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }
  42.  
  43. public static SQL load(File f)
  44. {
  45. return new SQL(f);
  46. }
  47.  
  48. public static SQL load(String f)
  49. {
  50. return new SQL(new File(f));
  51. }
  52.  
  53. public static SQL load(String host, String database, String user, String pass)
  54. {
  55. return new SQL("jdbc:mysql://" + host + "/" + database + "?user=" + user + "&password=" + pass);
  56. }
  57.  
  58. public void update(String q)
  59. {
  60. try
  61. {
  62. this.stmt.executeUpdate(q);
  63. }
  64. catch (Exception e)
  65. {
  66. e.printStackTrace();
  67. }
  68. }
  69.  
  70. public ResultSet query(String q)
  71. {
  72. try
  73. {
  74. return this.stmt.executeQuery(q);
  75. }
  76. catch (Exception localException) {}
  77. return null;
  78. }
  79.  
  80. public void close()
  81. {
  82. try
  83. {
  84. this.stmt.close();
  85. this.conn.close();
  86. }
  87. catch (Exception localException) {}
  88. }
  89.  
  90. public boolean isConnected()
  91. {
  92. try
  93. {
  94. return (this.stmt != null) && (this.conn != null) && (!this.stmt.isClosed()) && (!this.conn.isClosed());
  95. }
  96. catch (Exception localException) {}
  97. return false;
  98. }
  99.  
  100. public Connection getConnection()
  101. {
  102. return this.conn;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement