Guest User

Untitled

a guest
Mar 14th, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. package jtm.activity13;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5.  
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import org.apache.log4j.Logger;
  13.  
  14. public class TeacherManager {
  15.  
  16. protected Connection conn;
  17. public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  18.  
  19. public TeacherManager() {
  20.  
  21. // TODO #1 When new TeacherManager is created, create connection to the
  22. // database server:
  23.  
  24. final String url = "jdbc:mysql://localhost/?autoReconnect=true&useSSL=false&characterEncoding=utf8";
  25. final String user = "root";
  26. final String pass = "Student007";
  27. // conn.setAutoCommit(false);
  28.  
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver");
  31. conn = DriverManager.getConnection(url, user, pass);
  32. conn.setAutoCommit(true);
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36.  
  37. }
  38. // Hints:
  39. // 1. Do not pass database name into url, because some statements
  40. // for tests need to be executed server-wise, not just database-wise.
  41. // 2. Set AutoCommit to false and use conn.commit() where necessary in
  42. // other methods
  43.  
  44. /**
  45. * Returns a Teacher instance represented by the specified ID.
  46. *
  47. * @param id
  48. * the ID of teacher
  49. * @return a Teacher object
  50. * @throws SQLException
  51. */
  52. public Teacher findTeacher(int id) {
  53.  
  54. try {
  55. Statement stm = conn.createStatement();
  56.  
  57. ResultSet rs = stm.executeQuery("SELECT * FROM database_activity.Teacher WHERE id LIKE '" + id + "'");
  58.  
  59. while (rs.next()) {
  60. return new Teacher(rs.getInt("id"), rs.getString("firstname"), rs.getString("lastname"));
  61. }
  62.  
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. }
  66. return new Teacher(0, null, null);
  67.  
  68. //
  69. // return null;
  70. // Hint: Because default database is not set in connection,
  71. // use full notation for table "database_activity.Teacher"
  72.  
  73. }
  74.  
  75. /**
  76. * Returns a list of Teacher object that contain the specified first name
  77. * and last name. This will return an empty List of no match is found.
  78. *
  79. * @param firstName
  80. * the first name of teacher.
  81. * @param lastName
  82. * the last name of teacher.
  83. * @return a list of Teacher object.
  84. * @throws SQLException
  85. */
  86. public List<Teacher> findTeacher(String firstName, String lastName){
  87. // TODO #3 Write an sql statement that searches teacher by first and
  88. // last name and returns results as ArrayList<Teacher>.
  89. // Note that search results of partial match
  90. // in form ...like '%value%'... should be returned
  91. // Note, that if nothing is found return empty list!
  92.  
  93. List<Teacher> list = new ArrayList<Teacher>();
  94.  
  95. try {
  96. Statement stm = conn.createStatement();
  97. ResultSet res = stm.executeQuery("SELECT * FROM database_activity.Teacher WHERE firstname LIKE '%"
  98. + firstName + "%' AND lastname LIKE '%" + lastName + "%'");
  99.  
  100. while (res.next()) {
  101. list.add(new Teacher(res.getInt("id"), res.getString("firstname"), res.getString("lastname")));
  102. }
  103. } catch (SQLException e) {
  104. e.printStackTrace();
  105. }
  106. return list;
  107.  
  108. // conn.commit();
  109.  
  110. }
  111.  
  112. /**
  113. * Insert an new teacher (first name and last name) into the repository.
  114. *
  115. * @param firstName
  116. * the first name of teacher
  117. * @param lastName
  118. * the last name of teacher
  119. * @return true if success, else false.
  120. * @throws SQLException
  121. */
  122.  
  123. public boolean insertTeacher(String firstName, String lastName) {
  124.  
  125. try{
  126.  
  127. Statement stm = conn.createStatement();
  128. int status = stm.executeUpdate("INSERT INTO database_activity.Teacher (firstname, lastname) VALUES ('"
  129. + firstName + "', '" + lastName + "')");
  130.  
  131. while(status !=0){
  132. return true;
  133. }
  134.  
  135. } catch (SQLException e){
  136. e.printStackTrace();
  137. }
  138. return false;
  139.  
  140.  
  141.  
  142.  
  143. }
  144.  
  145. /**
  146. * Insert teacher object into database
  147. *
  148. * @param teacher
  149. * @return true on success, false on error (e.g. non-unique id)
  150. * @throws SQLException
  151. */
  152. public boolean insertTeacher(Teacher teacher) throws SQLException {
  153.  
  154. // String sql = "INSERT IGNORE INTO database_activity.Teacher (id,
  155. // firstname, lastname) VALUES (?, ?, ?)";
  156. // PreparedStatement preparedStatement = conn.prepareStatement(sql);
  157. // preparedStatement.setInt(1, teacher.getID());
  158. // preparedStatement.setString(2, teacher.getFirstName());
  159. // preparedStatement.setString(3, teacher.getLastName());
  160. // conn.commit();
  161. try {
  162. Statement stm = conn.createStatement();
  163.  
  164. int status = stm.executeUpdate("INSERT INTO database_activity.Teacher (id, firstname, lastname) VALUES ('"
  165. + teacher.getID() + "', '" + teacher.getFirstName() + "', '" + teacher.getLastName() + "')");
  166. while (status != 0) {
  167. return true;
  168. }
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. }
  172. return false;
  173.  
  174. //
  175. // int status = preparedStatement.executeUpdate();
  176.  
  177. // return false;
  178.  
  179. // TODO #5 Write an sql statement that inserts teacher in database.
  180. // return false;
  181. }
  182.  
  183. /**
  184. * Updates an existing Teacher in the repository with the values represented
  185. * by the Teacher object.
  186. *
  187. * @param teacher
  188. * a Teacher object, which contain information for updating.
  189. * @return true if row was updated.
  190. * @throws Exception
  191. */
  192. public boolean updateTeacher(Teacher teacher) throws Exception {
  193.  
  194. try {
  195.  
  196. Statement stm = conn.createStatement();
  197. int status = stm.executeUpdate("UPDATE database_activity.Teacher SET firstname='" + teacher.getFirstName()
  198. + "', lastname='" + teacher.getLastName() + "' WHERE id=" + teacher.getID());
  199.  
  200. while (status != 0) {
  201. return true;
  202. }
  203. } catch (SQLException e) {
  204. e.printStackTrace();
  205. }
  206. return false;
  207.  
  208. // TODO #6 Write an sql statement that updates teacher information.
  209.  
  210. }
  211.  
  212. /**
  213. * Delete an existing Teacher in the repository with the values represented
  214. * by the ID.
  215. *
  216. * @param id
  217. * the ID of teacher.
  218. * @return true if row was deleted.
  219. * @throws SQLException
  220. */
  221. public boolean deleteTeacher(int id) throws SQLException {
  222. // TODO #7 Write an sql statement that deletes teacher from database.
  223.  
  224. try {
  225. Statement stmt = conn.createStatement();
  226. int res = stmt.executeUpdate("DELETE FROM database_activity.Teacher WHERE id=" + id);
  227. while (res != 0) {
  228. return true;
  229. }
  230.  
  231. } catch (SQLException e) {
  232. e.printStackTrace();
  233. }
  234. return false;
  235. }
  236.  
  237. public void closeConnecion() {
  238.  
  239. try {
  240. conn.close();
  241. conn = null;
  242. } catch (SQLException e) {
  243. // TODO Auto-generated catch block
  244. e.printStackTrace();
  245. }
  246. }
  247.  
  248. // public static void main(String[] args) throws Exception {
  249. // TeacherManager manager = new TeacherManager();
  250. //// manager.insertTeacher("Janis", "Bekeris");
  251. //// manager.deleteTeacher(3);
  252. // Teacher lol = new Teacher(13, "Janka", "Garais");
  253. //// manager.insertTeacher(lol);
  254. // manager.deleteTeacher(3);
  255. //
  256. // }
  257. }
Add Comment
Please, Sign In to add comment