2
0

SQL.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Feel free to copy/use it for your own project.
  3. * Keep in mind that it took me several days/weeks, beers and asperines to make this.
  4. * So be nice, and give me some credit, I won't bite and it won't hurt you.
  5. */
  6. import java.sql.*;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. /**
  10. *
  11. * @author Deben Oldert
  12. */
  13. public class SQL
  14. {
  15. Connection conn = null;
  16. Statement stmt = null;
  17. SQL() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException
  18. {
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. conn = DriverManager.getConnection("jdbc:mysql://192.168.2.240:3306/sas?user=Implementation&password=Test123456");
  22. stmt = conn.createStatement();
  23. } catch (ClassNotFoundException ex) {
  24. Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
  25. }
  26. }
  27. public Statement Connect() {
  28. return stmt;
  29. }
  30. public void threadUpdate(String threadId, String state, String data) throws SQLException, ClassNotFoundException {
  31. switch (state) {
  32. case "terminate":
  33. stmt.execute("DELETE FROM thread WHERE threadId='"+threadId+"'");
  34. System.out.println("SQL ==> TERMINATE");
  35. break;
  36. case "birth":
  37. stmt.execute("INSERT INTO thread (threadId,state,`data`) VALUES ('"+threadId+"','"+state+"','"+data+"')");
  38. System.out.println("SQL ==> BIRTH");
  39. break;
  40. default:
  41. stmt.execute("UPDATE thread set `state`='"+state+"',`data`='"+data+"' WHERE `threadId`='"+threadId+"'");
  42. System.out.println("SQL ==> UPDATE");
  43. break;
  44. }
  45. }
  46. public void clean() throws SQLException {
  47. stmt.execute("TRUNCATE TABLE thread");
  48. }
  49. }