Guest User

Untitled

a guest
Dec 5th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package plugin.zozidalom.pp;
  2.  
  3. import gyurix.spigotlib.SU;
  4. import org.sqlite.JDBC;
  5.  
  6. import javax.swing.*;
  7. import java.sql.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11.  
  12. public class MySQL {
  13.  
  14.  
  15. static Connection con = null;
  16. static boolean b;
  17. static PreparedStatement state;
  18.  
  19. public static void setCon() {
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver");
  22. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/zozidalom", "root", "Gumicukor462");
  23. System.out.println(con.toString());
  24. } catch (Exception ex) {
  25. ex.printStackTrace();
  26. }
  27. }
  28.  
  29. public static Connection getCon() throws Exception {
  30. if (con == null) {
  31. setCon();
  32. }
  33. return con;
  34. }
  35.  
  36. public static boolean putData(String sql) {
  37. try {
  38. getCon().setAutoCommit(false);
  39. state = getCon().prepareStatement(sql);
  40. state.executeUpdate();
  41. getCon().commit();
  42. b = true;
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. b = false;
  46. }
  47. return b;
  48. }
  49.  
  50. // connection commit
  51. public static void commit() {
  52. try {
  53. con.commit();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57.  
  58. }
  59.  
  60. // rollback data
  61. public static void rollback() {
  62. if (con != null) {
  63. try {
  64. con.rollback();
  65. } catch (SQLException ex) {
  66. Logger.getLogger(JDBC.class.getName()).log(Level.SEVERE, null, ex);
  67. }
  68. }
  69. }
  70.  
  71. // close statement
  72. public static void putClose() {
  73. try {
  74. state.close();
  75. } catch (SQLException ex) {
  76. Logger.getLogger(JDBC.class.getName()).log(Level.SEVERE, null, ex);
  77. }
  78. }
  79. // close connection
  80.  
  81. public static void conClose() {
  82. try {
  83. con.setAutoCommit(true);
  84. con.close();
  85. } catch (SQLException ex) {
  86. Logger.getLogger(JDBC.class.getName()).log(Level.SEVERE, null, ex);
  87. }
  88. }
  89.  
  90. // clear prepared statement
  91. public static void putClear() {
  92. try {
  93. if (state != null && !state.isClosed()) {
  94. state.close();
  95. }
  96. } catch (SQLException ex) {
  97. Logger.getLogger(JDBC.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99. }
  100.  
  101. // clear the connection
  102. public static void conClear() {
  103. try {
  104. if (con != null && !con.isClosed()) {
  105. con.setAutoCommit(true);
  106. con.close();
  107. }
  108. } catch (SQLException ex) {
  109. Logger.getLogger(JDBC.class.getName()).log(Level.SEVERE, null, ex);
  110. }
  111. }
  112.  
  113. public static ResultSet getData(String sql) throws Exception {
  114. Statement state = getCon().createStatement();
  115. ResultSet rs = state.executeQuery(sql);
  116. return rs;
  117. }
  118.  
  119.  
  120. public static int update(String cmd)
  121. {
  122. try
  123. {
  124. PreparedStatement st = getCon().prepareStatement(cmd);
  125. return st.executeUpdate();
  126. }
  127. catch (Throwable e)
  128. {
  129. SU.error(SU.cs, e, "SpigotLib", "gyurix");
  130. }
  131. return -1;
  132. }
  133.  
  134.  
  135.  
  136. }
Add Comment
Please, Sign In to add comment