Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import java.io.File;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class SQL {
  9.  
  10. private String HOST = "HOSTNAME";
  11. private String DATABASE = "DATABASE";
  12. private String USER = "USER";
  13. private String PASSWORD = "PW";
  14. private Boolean mysql = true;
  15. private static File sqlfile = new File("plugins/Cloud/Database.sqlite");
  16. private Connection con;
  17.  
  18.  
  19. public boolean isConnected(){
  20. if(con == null){
  21. return false;
  22. }else{
  23. return true;
  24. }
  25. }
  26.  
  27.  
  28.  
  29. public void setHOST(String hOST) {
  30. if(this.mysql == true){
  31. HOST = hOST;
  32. }
  33. }
  34.  
  35. public void setDATABASE(String dATABASE) {
  36. if(this.mysql == true){
  37. DATABASE = dATABASE;
  38. }
  39. }
  40.  
  41. public void setUSER(String uSER) {
  42. if(this.mysql == true){
  43. USER = uSER;
  44. }
  45. }
  46.  
  47. public void setPASSWORD(String pASSWORD) {
  48. if(this.mysql == true){
  49. PASSWORD = pASSWORD;
  50. }
  51. }
  52.  
  53. public SQL(boolean MySQL){
  54. this.mysql = MySQL;
  55. }
  56.  
  57. public void connect() {
  58. if(this.mysql == true){
  59. try {
  60.  
  61. con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  62. System.out.println("[SQL] Die Verbindung zur SQL wurde hergestellt!");
  63.  
  64. } catch (SQLException e) {
  65. System.out.println("[SQL] Die Verbindung zur SQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  66. }
  67. }else{
  68. try {
  69. try{
  70. Class.forName("org.sqlite.JDBC");
  71. }catch (ClassNotFoundException localClassNotFoundException){
  72.  
  73. }
  74. con = DriverManager.getConnection("jdbc:sqlite:" + sqlfile.getAbsolutePath());
  75. System.out.println("[SQL] Die Verbindung zur SQL wurde hergestellt!");
  76. } catch (SQLException e) {
  77. System.out.println("[SQL] Die Verbindung zur SQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  78. }
  79. }
  80. }
  81.  
  82. public void close() {
  83. try {
  84. if(con != null) {
  85. con.close();
  86. System.out.println("[SQL] Die Verbindung zur SQL wurde Erfolgreich beendet!");
  87. }
  88.  
  89. } catch (SQLException e) {
  90. System.out.println("[SQL] Fehler beim beenden der Verbindung zur SQL! Fehler: " + e.getMessage());
  91. }
  92.  
  93. }
  94.  
  95. public void update(String qry) {
  96. try {
  97. Statement st = con.createStatement();
  98. st.executeUpdate(qry);
  99. st.close();
  100. } catch (SQLException e) {
  101. connect();
  102. System.err.println(e);
  103. }
  104. }
  105.  
  106. public ResultSet query(String qry) {
  107. ResultSet rs = null;
  108. try {
  109. Statement st = con.createStatement();
  110. rs = st.executeQuery(qry);
  111. } catch (SQLException e) {
  112. connect();
  113. System.err.println(e);
  114. }
  115. return rs;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement