Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1.  
  2. public class EmployeeSkillDAO
  3. {
  4. private Connection conn;
  5.  
  6. public Connection getConnection()
  7. {
  8. try
  9. {
  10. Class.forName("com.mysql.jdbc.Driver");
  11. String url = "jdbc:mysql://localhost:3306/pd1";
  12. String userName = "root";
  13. String password = "root";
  14. conn = DriverManager.getConnection(url, userName, password);
  15. System.out.println("Database connection successful!");
  16. }
  17.  
  18. catch (ClassNotFoundException | SQLException e)
  19. {
  20. e.printStackTrace();
  21. }
  22.  
  23. return conn;
  24. }
  25.  
  26. public void insertEmployeeSkill(RetrieveUserInput rui)
  27. {
  28. //Get DB Connection
  29. DBUtil util = new DBUtil();
  30. Connection conn = util.getConnection();
  31.  
  32. //create update statement
  33. String sql = "insert into employeeSkill(skillId, empId, rating)" + "values(?, ?, ?)";
  34.  
  35. //use prepared statement
  36. try {
  37. PreparedStatement ps = conn.prepareStatement(sql);
  38. ps.setInt(1, rui.getSkillId());
  39. ps.setString(2, rui.getEmpName());
  40. ps.setDouble(3, rui.getSkillRating());
  41. conn.setAutoCommit(false);
  42. ps.executeUpdate();
  43. conn.commit();
  44. } catch (SQLException e)
  45.  
  46. {
  47. try
  48. {
  49. conn.rollback();
  50. e.printStackTrace();
  51. }
  52.  
  53. catch (SQLException e1)
  54. {
  55. System.err.println(e1.getMessage());
  56. }
  57. }
  58.  
  59. finally
  60. {
  61. try
  62. {
  63. conn.close();
  64. }
  65.  
  66. catch (SQLException e)
  67. {
  68. System.err.println(e.getMessage());
  69. }
  70. }
  71. }
  72.  
  73. public static EmployeeSkill getEmployeeBySkill(String skill)
  74. {
  75. EmployeeSkill eSkill = null;
  76.  
  77. DBUtil util = new DBUtil();
  78. Connection conn = util.getConnection();
  79. String sql = "select id, Employee, rating from EmployeeSkill" +
  80. "where employeeId = ?";
  81.  
  82. try
  83. {
  84. PreparedStatement ps = conn.prepareStatement(sql);
  85. ps.setString(1, skill);
  86. ResultSet rs = ps.executeQuery();
  87.  
  88. while(rs.next())
  89. {
  90. int eId = rs.getInt("employeeId");
  91. String eName = rs.getString("employeeName");
  92. double rating = rs.getDouble("rating");
  93.  
  94. eSkill.setId(eId);
  95. eSkill.getEmployee().setEmployeeName(eName);;
  96. eSkill.setRating(rating);
  97. }
  98.  
  99. if(eSkill == null) throw new InvalidIdException("Sorry EmpId: " + skill + "not found.");
  100. }
  101.  
  102. catch (SQLException | InvalidIdException e)
  103. {
  104. System.err.println(e.getMessage());
  105. }
  106.  
  107. finally
  108. {
  109. try
  110. {
  111. conn.close();
  112. }
  113.  
  114. catch (SQLException e)
  115. {
  116. System.err.println(e.getMessage());
  117. }
  118. }
  119. return eSkill;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement