Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. package sample;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Timestamp;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.Map;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16.  
  17. /**
  18.  *
  19.  * @author zamora
  20.  * Prepared statement
  21.  */
  22. public class RequestSQL {
  23.     public static class EmployeeSQL{
  24.         private static Connection connection = null;
  25.         private static PreparedStatement preparedStatement = null;
  26.         private static ResultSet result = null;
  27.  
  28.         private static String url = "jdbc:mysql://192.168.2.2:3306/mydb";
  29.         private static String user = "testuser";
  30.         private static String password = "test623";
  31.  
  32.         private final String idField = "employee_id";
  33.         private final String firstNameField = "first_name";
  34.         private final String lastNameField = "last_name";
  35.         private final String emailField = "email";
  36.         private final String managerField = "manager";
  37.         private final String availableAnnualLeavesField = "available_annual_leaves";
  38.         private final String availableSickLeavesField = "available_sick_leaves";
  39.  
  40.         private static HashSet<Integer> results;
  41.  
  42.         public static boolean searchEmployee(String name){
  43.             ArrayList<String> words = new ArrayList();
  44.             results = new HashSet<Integer>();
  45.  
  46.             HashMap<Integer, Employee> employeeHashMap = new HashMap<Integer, Employee>();
  47.             boolean found = false;
  48.  
  49.             String word = name.substring(0,name.indexOf(" "));
  50.             int index = name.length()-1;
  51.             while(!word.isEmpty()){
  52.                 words.add(word);
  53.                 name = name.substring(name.indexOf(" ") + 1) + " ";
  54.                 word = name.substring(0,name.indexOf(" "));
  55.             }
  56.  
  57.             for (String s : words){
  58.                 checkName(s);
  59.             }
  60.             return found;
  61.         }
  62.  
  63.         public static int checkName(String name){
  64.             int found = 0;
  65.  
  66.             try{
  67.                 connection = DriverManager.getConnection(url, user, password);
  68.                 preparedStatement = connection.prepareStatement("SELECT "+
  69.                         "* FROM hr.employees WHERE"+
  70.                         " first_name = \""+name+"\" OR " +
  71.                         " last_name = \"\"+name+\"\"");
  72.                 result = preparedStatement.executeQuery();
  73.  
  74.                 while (result.next()) {
  75.                     if (!results.add(result.getInt(idField))) {
  76.                         return result.getInt(idField);
  77.                     }
  78.                 }
  79.  
  80.             }catch(SQLException e){
  81.                 Logger logger = Logger.getLogger(EmployeeSQL.class.getName());
  82.                 logger.log(Level.SEVERE, e.getMessage(), e);
  83.             }finally{
  84.                 try {
  85.                     if (result != null) {
  86.                         result.close();
  87.                     }
  88.                     if (preparedStatement != null) {
  89.                         preparedStatement.close();
  90.                     }
  91.                     if (connection != null) {
  92.                         connection.close();
  93.                     }
  94.  
  95.                 } catch (SQLException ex) {
  96.                     Logger lgr = Logger.getLogger(EmployeeSQL.class.getName());
  97.                     lgr.log(Level.WARNING, ex.getMessage(), ex);
  98.                 }
  99.             }
  100.  
  101.             return -1;
  102.         }
  103.  
  104.         public static boolean Insert(Employee employee){
  105.             insert = false;
  106.             try{
  107.                 connection = DriverManager.getConnection(url, user, password);
  108.                 preparedStatement = connection.prepareStatement("INSERT INTO "+
  109.                         "Entrada_Salida_Carro VALUES (?,?,?,?)");
  110.  
  111.                 preparedStatement.setNull(1, 0);
  112.                 preparedStatement.setTimestamp(2, datetime);
  113.                 preparedStatement.setInt(3, camera);
  114.                 preparedStatement.setString(4, plate);
  115.  
  116.                 preparedStatement.executeUpdate();
  117.                 insert = true;
  118.  
  119.             }catch(SQLException e){
  120.                 Logger logger = Logger.getLogger(CheckPlate.class.getName());
  121.                 logger.log(Level.SEVERE, e.getMessage(), e);
  122.                 insert = false;
  123.             }finally{
  124.                 try {
  125.                     if (preparedStatement != null) {
  126.                         preparedStatement.close();
  127.                     }
  128.                     if (connection != null) {
  129.                         connection.close();
  130.                     }
  131.                 } catch (SQLException ex) {
  132.                     Logger lgr = Logger.getLogger(InsertRecord.class.getName());
  133.                     lgr.log(Level.SEVERE, ex.getMessage(), ex);
  134.                 }
  135.             }
  136.             return insert;
  137.         }
  138.  
  139.         public static boolean Insert(Employee employee){
  140.             insert = false;
  141.             try{
  142.                 connection = DriverManager.getConnection(url, user, password);
  143.                 preparedStatement = connection.prepareStatement("INSERT INTO "+
  144.                         "Entrada_Salida_Carro VALUES (?,?,?,?)");
  145.  
  146.                 preparedStatement.setNull(1, 0);
  147.                 preparedStatement.setTimestamp(2, datetime);
  148.                 preparedStatement.setInt(3, camera);
  149.                 preparedStatement.setString(4, plate);
  150.  
  151.                 preparedStatement.executeUpdate();
  152.                 insert = true;
  153.  
  154.             }catch(SQLException e){
  155.                 Logger logger = Logger.getLogger(CheckPlate.class.getName());
  156.                 logger.log(Level.SEVERE, e.getMessage(), e);
  157.                 insert = false;
  158.             }finally{
  159.                 try {
  160.                     if (preparedStatement != null) {
  161.                         preparedStatement.close();
  162.                     }
  163.                     if (connection != null) {
  164.                         connection.close();
  165.                     }
  166.                 } catch (SQLException ex) {
  167.                     Logger lgr = Logger.getLogger(InsertRecord.class.getName());
  168.                     lgr.log(Level.SEVERE, ex.getMessage(), ex);
  169.                 }
  170.             }
  171.             return insert;
  172.         }
  173.     }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement