2
0

SQL.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import java.sql.*;
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4. public class SQL
  5. {
  6. Connection conn = null;
  7. Statement stmt = null;
  8. SQL() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException
  9. {
  10. try {
  11. // load the sqlite-JDBC driver using the current class loader
  12. Class.forName("com.mysql.jdbc.Driver");
  13. conn = DriverManager.getConnection("jdbc:mysql://192.168.2.240:3306/sas?user=Implementation&password=Test123456");
  14. stmt = conn.createStatement();
  15. } catch (ClassNotFoundException ex) {
  16. Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
  17. }
  18. }
  19. public Statement Connect() {
  20. return stmt;
  21. }
  22. public void threadUpdate(String threadId, String state, String data) throws SQLException, ClassNotFoundException {
  23. switch (state) {
  24. case "terminate":
  25. stmt.execute("DELETE FROM thread WHERE threadId='"+threadId+"'");
  26. System.out.println("SQL ==> TERMINATE");
  27. break;
  28. case "birth":
  29. stmt.execute("INSERT INTO thread (threadId,state,`data`) VALUES ('"+threadId+"','"+state+"','"+data+"')");
  30. System.out.println("SQL ==> BIRTH");
  31. break;
  32. default:
  33. stmt.execute("UPDATE thread set `state`='"+state+"',`data`='"+data+"' WHERE `threadId`='"+threadId+"'");
  34. System.out.println("SQL ==> UPDATE");
  35. break;
  36. }
  37. }
  38. }