Advertisement
Guest User

Untitled

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