Advertisement
Guest User

Untitled

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