Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import beans.Employee;
  10. import beans.Employees;
  11.  
  12. public class DatabaseService {
  13.  
  14. public int create(Employee e) throws SQLException {
  15. // connection data
  16. String dbUrl = "jdbc:derby:C:\\Users\\jordan\\MyDB";
  17. String user = "user";
  18. String pass = "derby";
  19.  
  20. // three objects used in every database operation. Notice the import java.sql.* is needed
  21.  
  22. Connection myConn = null;
  23. Statement myStmt = null;
  24. ResultSet myRs = null;
  25.  
  26. try {
  27. // 1. Get a connection to database
  28.  
  29. myConn = DriverManager.getConnection(dbUrl, user, pass);
  30.  
  31. System.out.println("Database conntection to " + dbUrl + "successful! a" + user + "\n");
  32.  
  33.  
  34. // 2. create a statement
  35. myStmt = myConn.createStatement();
  36.  
  37.  
  38. // 3. Insert a new employee
  39.  
  40. System.out.println("Inserting a new employee to database");
  41.  
  42. int rowsAffected = myStmt.executeUpdate("insert into bigcompanydatabase.employees " + "(last_name, first_name, email, department, salary)" + " values " + "('"+e.getLast_name()+"', '"+e.getFirst_name()+"', '"+e.getEmail()+"', '"+e.getDepartment()+"', "+e.getSalary()+")");
  43.  
  44. System.out.println(rowsAffected + " items were inserted");
  45.  
  46. // 4. Verify this by getting a list of employees
  47.  
  48. myRs = myStmt.executeQuery("select * from bigcompanydatabase.employees order by last_name");
  49.  
  50. System.out.println("Here is the new list of employees");
  51.  
  52. // 5. Process the result set
  53.  
  54. while (myRs.next()) {
  55. System.out.println(myRs.getString("last_name") + ", " + myRs.getString("first_name"));
  56. }
  57.  
  58. return rowsAffected;
  59. }
  60.  
  61.  
  62. catch (Exception exc)
  63. {
  64. System.out.println("Error connection to" + dbUrl);
  65. exc.printStackTrace();
  66.  
  67. }
  68.  
  69. finally {
  70. if (myRs !=null)
  71. {
  72. myRs.close();
  73. }
  74. if (myStmt !=null)
  75. {
  76. myStmt.close();
  77. }
  78. if (myConn !=null)
  79. {
  80. myConn.close();
  81. }
  82.  
  83. }
  84.  
  85. return 0;
  86.  
  87. }
  88.  
  89. public int remove(int id) throws SQLException {
  90.  
  91. // connection data
  92. String dbUrl = "jdbc:derby:C:\\Users\\jordan\\MyDB";
  93. String user = "user";
  94. String pass = "derby";
  95.  
  96. // three objects used in every database operation. Notice the import java.sql.* is needed
  97.  
  98. Connection myConn = null;
  99. Statement myStmt = null;
  100. ResultSet myRs = null;
  101.  
  102. try {
  103. // 1. Get a connection to database
  104.  
  105. myConn = DriverManager.getConnection(dbUrl, user, pass);
  106.  
  107.  
  108. // 2. create a statement
  109. myStmt = myConn.createStatement();
  110.  
  111. // Call helper method to display the employee's information
  112.  
  113. System.out.println("BEFORE THE DELETE");
  114. //displayEmployee(myConn, "Donald", "Trump");
  115.  
  116.  
  117. System.out.println("\nDELETING THE EMPLOYEE: "+id+"\n");
  118.  
  119. int rowsAffected = myStmt.executeUpdate("delete from bigcompanydatabase.employees "+
  120. "where ID="+id);
  121.  
  122. System.out.println(rowsAffected+" rows deleted\n");
  123.  
  124. System.out.println("AFTER THE DELETE");
  125. //displayEmployee(myConn, "Donald", "Trump");
  126.  
  127.  
  128. return rowsAffected;
  129.  
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. finally {
  134.  
  135. if (myRs != null) {
  136. myRs.close();
  137. }
  138. if (myStmt != null) {
  139. myStmt.close();
  140. }
  141. if (myConn != null) {
  142. myConn.close();
  143. }
  144. }
  145. return 0;
  146.  
  147. }
  148.  
  149. public int update(Employee e, int id) throws SQLException {
  150.  
  151.  
  152.  
  153. // connection data
  154. String dbUrl = "jdbc:derby:C:\\Users\\jordan\\MyDB";
  155. String user = "user";
  156. String pass = "derby";
  157.  
  158. // three objects used in every database operation. Notice the import java.sql.*
  159. // is needed
  160.  
  161. Connection myConn = null;
  162. Statement myStmt = null;
  163. ResultSet myRs = null;
  164.  
  165. try {
  166. // 1. Get a connection to database
  167.  
  168. myConn = DriverManager.getConnection(dbUrl, user, pass);
  169.  
  170. System.out.println("Database conntection to " + dbUrl + "successful! a" + user + "\n");
  171.  
  172. // 2. create a statement
  173. myStmt = myConn.createStatement();
  174.  
  175. // Call helper method to display the employee's information
  176.  
  177. System.out.println("BEFORE THE UPDATE");
  178. //displayEmployee(myConn, "Carl", "Adams");
  179.  
  180. // Update the employee
  181. System.out.println("\n EXECUTING THE UPDATE FOR: "+e.getFirst_name()+" "+e.getLast_name()+"\n");
  182.  
  183. int rowsAffected = myStmt.executeUpdate(
  184. "update bigcompanydatabase.employees" + " set email='"+e.getEmail()+"', first_name='"+e.getFirst_name()+"',"
  185. + "last_name='"+e.getLast_name()+"', department='"+e.getDepartment()+"', salary="+e.getSalary()
  186. + " where ID="+id);
  187.  
  188. System.out.println(rowsAffected + "rows changed");
  189.  
  190. // Call helper method to display the employees information
  191.  
  192. System.out.println("AFTER THE UPDATE");
  193. //displayEmployee(myConn, "Carl", "Adams");
  194. }
  195. catch (Exception exc) {
  196. exc.printStackTrace();
  197.  
  198. }
  199.  
  200. finally {
  201. if (myRs != null) {
  202. myRs.close();
  203. }
  204. if (myStmt != null) {
  205. myStmt.close();
  206. }
  207. if (myConn != null) {
  208. myConn.close();
  209. }
  210. }
  211.  
  212.  
  213. return 0;
  214. }
  215.  
  216.  
  217. public Employees findAll() throws SQLException {
  218. Employees employees = new Employees();
  219.  
  220.  
  221. // connection data
  222. String dbUrl = "jdbc:derby:C:\\Users\\jordan\\MyDB";
  223. String user = "user";
  224. String pass = "derby";
  225.  
  226. // three objects used in every database operation. Notice the import java.sql.* is needed
  227.  
  228. Connection myConn = null;
  229. Statement myStmt = null;
  230. ResultSet myRs = null;
  231. try {
  232. // 1. Get a connection to database
  233.  
  234. myConn = DriverManager.getConnection(dbUrl, user, pass);
  235.  
  236. System.out.println("Database conntection to " + dbUrl + "successful! a" + user + "\n");
  237.  
  238.  
  239. // 2. create a statement
  240. myStmt = myConn.createStatement();
  241.  
  242.  
  243.  
  244. // 3. Execute SQL query
  245. myRs = myStmt.executeQuery("Select * from bigcompanydatabase.employees");
  246.  
  247.  
  248. // 4. Pricess the result set
  249. while (myRs.next())
  250. {
  251. System.out.println(myRs.getString("first_name") + ", " + myRs.getString("last_name") + ", " + myRs.getString("email") + ", "+ myRs.getString("department") + ", " + myRs.getDouble("salary"));
  252.  
  253.  
  254. employees.add(new Employee(myRs.getInt("ID"), myRs.getString("first_name"), myRs.getString("last_name"),
  255. myRs.getString("email"), myRs.getString("department"), myRs.getDouble("salary")));
  256.  
  257. }
  258. }
  259.  
  260.  
  261. catch (Exception exc)
  262. {
  263. System.out.println("Error connection to" + dbUrl);
  264. exc.printStackTrace();
  265.  
  266. }
  267.  
  268. finally {
  269. if (myRs !=null)
  270. {
  271. myRs.close();
  272. }
  273. if (myStmt !=null)
  274. {
  275. myStmt.close();
  276. }
  277. if (myConn !=null)
  278. {
  279. myConn.close();
  280. }
  281. }
  282.  
  283.  
  284. return employees;
  285. }
  286.  
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement