Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. package my.arduinoapp;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. public class DATABASE_ {
  12.  
  13. public static boolean connection;
  14. private Connection connect = null;
  15. private PreparedStatement preparedStatement = null;
  16. private ResultSet resultSet = null;
  17.  
  18. public DATABASE_(){
  19. try {
  20.  
  21. Class.forName("com.mysql.jdbc.Driver");
  22. connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/yo","root","");
  23. connection = true;
  24. }catch (ClassNotFoundException | SQLException e) {
  25. System.out.println(e);
  26. connection = false;
  27. }
  28. }
  29.  
  30. public ResultSet readDataBase(String id) throws Exception {
  31.  
  32. try {
  33. Statement statementr = null;
  34. statementr = connect.createStatement();
  35.  
  36. System.out.println(id);
  37. String[] parts = id.split(" ", 2);
  38. resultSet = statementr.executeQuery("SELECT * FROM employee_info, employee_service_info WHERE employee_info.login_id ='" + parts[0] + "' AND employee_service_info.login_id ='" + parts[0] + "'AND employee_service_info.service_date ='" + parts[1] + "'");
  39. //System.out.println("?! 4");
  40.  
  41. } catch (SQLException e) {
  42. throw e;
  43. } finally {
  44. //close();
  45. }
  46. return resultSet;
  47. }
  48.  
  49. public boolean writeDataBase(String id) throws Exception {
  50.  
  51. try {
  52. Statement statementw = null;
  53. statementw = connect.createStatement();
  54.  
  55. System.out.println(id);
  56. String[] parts = id.split(" ", 5);
  57.  
  58.  
  59.  
  60. if (!CheckDataBase(parts[0])) {
  61. return false;
  62. }
  63.  
  64. if(parts[4].equals("in")){
  65. statementw.executeUpdate("INSERT INTO `employee_service_info`(`service_date`, `login_time`, `login_id`) VALUES ('" + parts[1] + "','" + parts[2] + " " + parts[3] + "','" + parts[0] + "')");
  66. }
  67.  
  68. if(parts[4].equals("out")){
  69. statementw.executeUpdate("UPDATE `employee_service_info` SET `logout_time`='" + parts[2] + " " + parts[3] + "' WHERE service_date = '" + parts[1] + "' AND login_id = '" + parts[0] + "'");
  70. }
  71.  
  72.  
  73.  
  74. } catch (SQLException e) {
  75. throw e;
  76. } finally {
  77. //System.out.println("done");
  78. //close();
  79. }
  80. return true;
  81. }
  82.  
  83. private boolean CheckDataBase(String id) throws Exception {
  84.  
  85. try {
  86. Statement statementC = null;
  87. statementC= connect.createStatement();
  88.  
  89. ResultSet count = statementC.executeQuery("SELECT * FROM employee_info WHERE login_id ='" + id + "'");
  90.  
  91. if (count.next()) {
  92. return true;
  93. }
  94.  
  95. } catch (SQLException e) {
  96. throw e;
  97. } finally {
  98. //close();
  99. }
  100. return false;
  101. }
  102.  
  103. // You need to close the resultSet
  104. private void close() {
  105. try {
  106. if (resultSet != null) {
  107. resultSet.close();
  108. }
  109.  
  110. if (connect != null) {
  111. connect.close();
  112. }
  113. } catch (Exception e) {
  114.  
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement