Advertisement
Guest User

Untitled

a guest
May 30th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package zad3;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5. import java.util.*;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9.  
  10. public class MySqlDAOImpl implements DAO{
  11.  
  12. static long positionInc;
  13. static long employeeInc;
  14.  
  15. private static ObjectOutputStream employeeFileOut = null;
  16.  
  17. private static ObjectOutputStream positionFileOut = null;
  18.  
  19. private static Connection con;
  20.  
  21. MySqlDAOImpl() throws SQLException, ClassNotFoundException{
  22. String db = "db";
  23. String url = "jdbc:mysql://localhost:3306/" + db;
  24. String userName = "root";
  25. String password = "mysql";
  26. Class.forName(com.mysql.jdbc.Driver.class.getName());
  27. con = DriverManager.getConnection(url, userName, password);
  28. }
  29.  
  30. ObjectOutputStream getStreamToPositions() throws FileNotFoundException, IOException{
  31. if(positionFileOut == null)
  32. positionFileOut = new ObjectOutputStream(new FileOutputStream("Positions.dat"));
  33. return positionFileOut;
  34. }
  35.  
  36. static long getNextPositionID(){
  37. return ++positionInc;
  38. }
  39.  
  40. static long getNextEmployeeID(){
  41. return ++employeeInc;
  42. }
  43.  
  44.  
  45.  
  46.  
  47. public void savePosition(Position p)throws SQLException{
  48. Statement stmt = con.createStatement();
  49.  
  50. boolean succeed = stmt.execute("INSERT INTO stanowiska(nazwa) VALUES ('"+p.getPosition()+"')");
  51.  
  52. }
  53.  
  54.  
  55. public List getAllPositions() throws SQLException {
  56. List array = new ArrayList();
  57.  
  58. Object o;
  59.  
  60. String query = "select * from stanowiska";
  61.  
  62. Statement stmt = con.createStatement();
  63. ResultSet rs = stmt.executeQuery(query);
  64.  
  65. while (rs.next()){
  66. Position position = new Position(rs.getString("nazwa"));
  67. array.add(position);
  68. }
  69.  
  70. return array;
  71. }
  72.  
  73.  
  74. public Position getPosition(long id) throws SQLException {
  75. Position p =new Position();
  76. String sql ="SELECT * FROM stanowiska WHERE id = "+id;
  77. Statement stmt = con.createStatement();
  78. ResultSet rs = stmt.executeQuery(sql);
  79. if(rs.next())
  80. {
  81. Integer i = rs.getInt("id");
  82.  
  83. String name = rs.getString("nazwa");
  84. p.setPosition(name);
  85. }
  86. return p;
  87. }
  88.  
  89.  
  90.  
  91.  
  92. public List getAllEmployees() throws SQLException {
  93. List array = new ArrayList();
  94.  
  95. Object o;
  96.  
  97. String query = "select p.id, p.imie, p.nazwisko, s.nazwa from stanowiska as s, pracownicy as p where p.id_stanowiska = s.id;";
  98.  
  99. Statement stmt = con.createStatement();
  100. ResultSet rs = stmt.executeQuery(query);
  101.  
  102. while (rs.next()){
  103. Employee employee = new Employee(rs.getString("imie"), rs.getString("nazwisko"), new Position(rs.getString("nazwa")));
  104. employee.setId(rs.getInt("id"));
  105. array.add(employee);
  106. }
  107.  
  108. return array;
  109. }
  110.  
  111.  
  112. public Employee getEmployee(long id) throws SQLException {
  113. Employee e =new Employee();
  114. String sql ="select p.id, p.imie, p.nazwisko, s.nazwa from stanowiska as s, pracownicy as p where p.id_stanowiska = s.id and p.id = "+id;
  115.  
  116. Statement stmt = con.createStatement();
  117. ResultSet rs = stmt.executeQuery(sql);
  118. if(rs.next())
  119. {
  120. Integer i = rs.getInt("id");
  121.  
  122. String imie = rs.getString("imie");
  123. String nazwisko = rs.getString("nazwisko");
  124. String stanowisko = rs.getString("nazwa");
  125.  
  126. e.setId(i);
  127. e.setName(imie);
  128. e.setSurname(nazwisko);
  129. e.setPosition(new Position(stanowisko));
  130.  
  131. return e;
  132. }
  133. return null;
  134.  
  135. }
  136.  
  137.  
  138. public void saveEmployee(Employee emp) throws SQLException {
  139. Position p =new Position();
  140. String sql ="SELECT * FROM stanowiska WHERE nazwa = '"+emp.getPosition().getPosition() + "'";
  141. Statement stmt = con.createStatement();
  142. ResultSet rs = stmt.executeQuery(sql);
  143.  
  144. int id = 0;
  145. if(rs.next())
  146. {
  147. id = rs.getInt("id");
  148. }
  149.  
  150.  
  151. //Statement stmt = con.createStatement();
  152. System.out.println(id);
  153. boolean succeed = stmt.execute("INSERT INTO pracownicy(imie, nazwisko, id_stanowiska) VALUES ('"+emp.getName()+"', '"+emp.getSurname()+"', '"+id+"')");
  154.  
  155. }
  156.  
  157. public void deleteEmployee(Employee emp) throws SQLException {
  158. Statement stmt = con.createStatement();
  159.  
  160. int modifiedRows = stmt.executeUpdate("DELETE from pracownicy WHERE id=" + emp.getId());
  161.  
  162.  
  163. }
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement