Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.69 KB | None | 0 0
  1. package casemanagementsystem;
  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.Iterator;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class DatabaseTalker {
  15.  
  16. public static String createCase(Case newCase) throws SQLException, ClassNotFoundException {
  17. Connection con = null;
  18. String back = "Failure";
  19. try {
  20. con = connect();
  21.  
  22. PreparedStatement stmt = con.prepareStatement("INSERT INTO cases (caseNo,idNo,supportCategory,comment,status,budgetTime,acctualTime) VALUES (?,?,?,?,?,?,?)");
  23.  
  24. stmt.setInt(1, newCase.getCaseNo());
  25. stmt.setInt(2, newCase.getManageNo());
  26. stmt.setString(3, newCase.getSupportCategory());
  27. stmt.setString(4, newCase.getComment());
  28. stmt.setString(5, newCase.getStatus());
  29. stmt.setInt(6, newCase.getBudgetTime());
  30. stmt.setInt(7, newCase.getAcctualTime());
  31.  
  32. int i = stmt.executeUpdate();
  33.  
  34. if (i > 0) {
  35. back = "Success";
  36. }
  37.  
  38. } catch (ClassNotFoundException ex) {
  39. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  40. } finally {
  41. closeCon(con);
  42. }
  43. return back;
  44. }
  45.  
  46. public static String createTask(Task newTask) throws SQLException, ClassNotFoundException {
  47. Connection con = null;
  48. String back = "Failure";
  49. try {
  50. con = connect();
  51.  
  52. PreparedStatement stmt = con.prepareStatement("INSERT INTO taskInstance (taskInstNo,caseNo,taskNo,idNo,acctualTime,status,comment) VALUES (?,?,?,?,?,?,?)");
  53.  
  54. stmt.setInt(1, newTask.getTaskInstNo());
  55. stmt.setInt(2, newTask.getCaseNo());
  56. stmt.setInt(3, newTask.getTaskNo());
  57. stmt.setInt(4, newTask.getTechNo());
  58. stmt.setInt(5, newTask.getAcctualTime());
  59. stmt.setString(6, newTask.getStatus());
  60. stmt.setString(7, newTask.getComment());
  61.  
  62. int i = stmt.executeUpdate();
  63.  
  64. if (i > 0) {
  65. back = "Success";
  66. }
  67.  
  68. } catch (ClassNotFoundException ex) {
  69. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  70. } finally {
  71. closeCon(con);
  72. }
  73. return back;
  74.  
  75. }
  76.  
  77. public static String updateCase(Case newCase) throws SQLException, ClassNotFoundException {
  78. Connection con = null;
  79. String back = "Failure";
  80. try {
  81. con = connect();
  82.  
  83. PreparedStatement stmt = con.prepareStatement("UPDATE cases SET caseNo=?,idNo=?,supportCategory=?,comment=?,status=?,budgetTime=?,acctualTime=? WHERE caseNo=?");
  84.  
  85. stmt.setInt(1, newCase.getCaseNo());
  86. stmt.setInt(2, newCase.getManageNo());
  87. stmt.setString(3, newCase.getSupportCategory());
  88. stmt.setString(4, newCase.getComment());
  89. stmt.setString(5, newCase.getStatus());
  90. stmt.setInt(6, newCase.getBudgetTime());
  91. stmt.setInt(7, newCase.getAcctualTime());
  92. stmt.setInt(8, newCase.getCaseNo());
  93.  
  94. int i = stmt.executeUpdate();
  95.  
  96. if (i > 0) {
  97. back = "Success";
  98. }
  99. } catch (ClassNotFoundException ex) {
  100. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  101. } finally {
  102. closeCon(con);
  103. }
  104. return back;
  105.  
  106. }
  107.  
  108. public static String updateTask(Task newTask) throws SQLException, ClassNotFoundException {
  109. Connection con = null;
  110. String back = "Failure";
  111. try {
  112. con = connect();
  113.  
  114. PreparedStatement stmt = con.prepareStatement("UPDATE taskInstance SET taskInstNo=?,caseNo=?,taskNo=?,idNo=?,acctualTime=?,status=?,comment=? WHERE taskInstNo=?");
  115.  
  116. stmt.setInt(1, newTask.getTaskInstNo());
  117. stmt.setInt(2, newTask.getCaseNo());
  118. stmt.setInt(3, newTask.getTaskNo());
  119. stmt.setInt(4, newTask.getTechNo());
  120. stmt.setInt(5, newTask.getAcctualTime());
  121. stmt.setString(6, newTask.getStatus());
  122. stmt.setString(7, newTask.getComment());
  123. stmt.setInt(8, newTask.getTaskInstNo());
  124.  
  125. int i = stmt.executeUpdate();
  126.  
  127. if (i > 0) {
  128. back = "Success";
  129. }
  130. } catch (ClassNotFoundException ex) {
  131. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  132. } finally {
  133. closeCon(con);
  134. }
  135. return back;
  136.  
  137. }
  138.  
  139. public static ArrayList retriveCaseList(int idNo) throws SQLException, ClassNotFoundException {
  140. Connection con = null;
  141. ArrayList caseList = new ArrayList();
  142. try {
  143. con = connect();
  144. PreparedStatement stmt = con.prepareStatement("SELECT caseNo FROM cases WHERE idNo=?");
  145. stmt.setString(1, "" + idNo);
  146.  
  147. ResultSet rs = stmt.executeQuery();
  148.  
  149. while (rs.next()) {
  150. int caseNumber = Integer.parseInt(rs.getString("caseNo"));
  151. caseList.add(caseNumber);
  152. }
  153. } catch (ClassNotFoundException ex) {
  154. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  155. } finally {
  156. closeCon(con);
  157. }
  158. return caseList;
  159.  
  160. }
  161.  
  162. public static ArrayList retriveTasksList(int caseNo) throws SQLException, ClassNotFoundException {
  163. Connection con = null;
  164. ArrayList tasksList = new ArrayList();
  165. try {
  166. con = connect();
  167. PreparedStatement stmt = con.prepareStatement("SELECT taskInstNo FROM taskInstance WHERE caseNo=?");
  168. stmt.setString(1, "" + caseNo);
  169.  
  170. ResultSet rs = stmt.executeQuery();
  171.  
  172. while (rs.next()) {
  173. String taskInstNumber = rs.getString("taskInstNo");
  174. tasksList.add(taskInstNumber);
  175. }
  176. } catch (ClassNotFoundException ex) {
  177. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  178. } finally {
  179. closeCon(con);
  180. }
  181. return tasksList;
  182.  
  183. }
  184.  
  185. public static Case getCase(int caseNo) throws SQLException, ClassNotFoundException {
  186. Connection con = null;
  187. Case storedCase = null;
  188. try {
  189. con = connect();
  190. PreparedStatement stmt = con.prepareStatement("SELECT caseNo, idNo, supportCategory, comment, status, budgetTime, acctualTime FROM cases WHERE caseNo=?");
  191. stmt.setInt(1, caseNo);
  192.  
  193. ResultSet rs = stmt.executeQuery();
  194. rs.next();
  195. String supportCategory = rs.getString("supportCategory");
  196. int caseNum = rs.getInt("caseNo");
  197. int manageNo = rs.getInt("idNo");
  198. String status = rs.getString("status");
  199. String comment = rs.getString("comment");
  200. storedCase = new Case(supportCategory, caseNum, manageNo, status, comment);
  201. storedCase.setBudgetTime(Integer.parseInt(rs.getString("budgetTime")));
  202. storedCase.setAcctualTime(Integer.parseInt(rs.getString("acctualTime")));
  203.  
  204. } catch (ClassNotFoundException ex) {
  205. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  206. } finally {
  207. closeCon(con);
  208. }
  209. return storedCase;
  210.  
  211. }
  212.  
  213. public static Task getTaskInst(int taskInstNo) throws SQLException, ClassNotFoundException {
  214. Connection con = null;
  215. Task storedTaskInst = null;
  216. try {
  217. con = connect();
  218.  
  219. PreparedStatement stmt = con.prepareStatement("SELECT caseNo,task.taskNo,idNo,budgetTime,acctualTime,taskName,status,comment FROM taskInstance JOIN task ON taskInstance.taskNo = task.taskNo WHERE taskInstNo=?");
  220. stmt.setInt(1, taskInstNo);
  221.  
  222. ResultSet rs = stmt.executeQuery();
  223. if (rs.next()) {
  224. int caseNo = rs.getInt("caseNo");
  225. int taskNo = rs.getInt("taskNo");
  226. int techNo = rs.getInt("idNo");
  227. int budgetTime = rs.getInt("budgetTime");
  228. int acctualTime = rs.getInt("acctualTime");
  229. String task = rs.getString("taskName");
  230. String status = rs.getString("status");
  231. String comment = rs.getString("comment");
  232. storedTaskInst = new Task(task, taskInstNo, taskNo, caseNo, acctualTime, techNo, status, comment);
  233. storedTaskInst.setBudgetTime(budgetTime);
  234. }
  235. } catch (ClassNotFoundException ex) {
  236. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  237. } finally {
  238. closeCon(con);
  239. }
  240. return storedTaskInst;
  241. }
  242.  
  243. public static ArrayList compileCaseNoList(String caseNo) throws SQLException, ClassNotFoundException {
  244. Connection con = null;
  245. ArrayList caseNoList = new ArrayList();
  246. try {
  247. con = connect();
  248. PreparedStatement stmt = con.prepareStatement("SELECT caseNo FROM cases");
  249.  
  250. ResultSet rs = stmt.executeQuery();
  251.  
  252. while (rs.next()) {
  253. String caseNumber = rs.getString("caseNo");
  254. caseNoList.add(caseNumber);
  255. }
  256. } catch (ClassNotFoundException ex) {
  257. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  258. } finally {
  259. closeCon(con);
  260. }
  261. return caseNoList;
  262.  
  263. }
  264.  
  265. public static ArrayList compileTaskInstNoList(String taskInstNo) throws SQLException, ClassNotFoundException {
  266. Connection con = null;
  267. ArrayList taskInstNoList = new ArrayList();
  268. try {
  269. con = connect();
  270. PreparedStatement stmt = con.prepareStatement("SELECT taskInstNo FROM taskInstance");
  271.  
  272. ResultSet rs = stmt.executeQuery();
  273.  
  274. while (rs.next()) {
  275. String caseNumber = rs.getString("taskInstNo");
  276. taskInstNoList.add(caseNumber);
  277. }
  278. } catch (ClassNotFoundException ex) {
  279. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  280. } finally {
  281. closeCon(con);
  282. }
  283. return taskInstNoList;
  284.  
  285. }
  286.  
  287. public static ArrayList<String> compileManagerNameList() throws SQLException, ClassNotFoundException {
  288. Connection con = null;
  289. ArrayList<String> nameList = new ArrayList();
  290. String fullName = "Fail!";
  291. try {
  292. con = connect();
  293.  
  294. PreparedStatement stmt = con.prepareStatement("SELECT fName, lName FROM users WHERE possition=?");
  295. stmt.setString(1, "MANAGER");
  296. ResultSet rs = stmt.executeQuery();
  297. while (rs.next()) {
  298. fullName = rs.getString("fName") + " " + rs.getString("lName");
  299. nameList.add(fullName);
  300. }
  301. } catch (ClassNotFoundException ex) {
  302. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  303. } finally {
  304. closeCon(con);
  305. }
  306. return nameList;
  307. }
  308.  
  309. public static String retriveTaskBudgetTime(String task) throws SQLException, ClassNotFoundException {
  310. Connection con = null;
  311. String budgetTime = "Fail!";
  312. try {
  313. con = connect();
  314. PreparedStatement stmt = con.prepareStatement("SELECT budgetTime FROM task WHERE taskName=?");
  315. stmt.setString(1, task);
  316. ResultSet rs = stmt.executeQuery();
  317. rs.next();
  318. budgetTime = rs.getString("budgetTime");
  319.  
  320. } catch (ClassNotFoundException ex) {
  321. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  322. } finally {
  323. closeCon(con);
  324. }
  325. return budgetTime;
  326. }
  327.  
  328. public static String retriveTaskNo(String task) throws SQLException, ClassNotFoundException {
  329. Connection con = null;
  330. String budgetTime = "Fail!";
  331. try {
  332. con = connect();
  333.  
  334. PreparedStatement stmt = con.prepareStatement("SELECT taskNo FROM task WHERE taskName=?");
  335. stmt.setString(1, task);
  336. ResultSet rs = stmt.executeQuery();
  337. rs.next();
  338. budgetTime = rs.getString("taskNo");
  339.  
  340. } catch (ClassNotFoundException ex) {
  341. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  342. } finally {
  343. closeCon(con);
  344. }
  345. return budgetTime;
  346.  
  347. }
  348.  
  349. public static String retriveIdNo(String fName) throws SQLException, ClassNotFoundException {
  350. Connection con = null;
  351. String idNum = "Fail!";
  352. try {
  353. con = connect();
  354.  
  355. PreparedStatement stmt = con.prepareStatement("SELECT idNo FROM users WHERE fName=?");
  356. stmt.setString(1, fName);
  357. ResultSet rs = stmt.executeQuery();
  358. rs.next();
  359. idNum = rs.getString("idNo");
  360.  
  361. } catch (ClassNotFoundException ex) {
  362. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  363. } finally {
  364. closeCon(con);
  365. }
  366. return idNum;
  367. }
  368.  
  369. public static int compileCaseBudgetTime(int caseNo) throws SQLException, ClassNotFoundException {
  370. Connection con = null;
  371. int budgetTime = 0;
  372. try {
  373. con = connect();
  374.  
  375. PreparedStatement stmt = con.prepareStatement("SELECT budgetTime FROM task JOIN taskInstance ON task.taskNo = taskInstance.taskNo WHERE caseNo=?");
  376. stmt.setString(1, "" + caseNo);
  377. ResultSet rs = stmt.executeQuery();
  378. while (rs.next()) {
  379. budgetTime += rs.getInt("budgetTime");
  380. }
  381. } catch (ClassNotFoundException ex) {
  382. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  383. } finally {
  384. closeCon(con);
  385. }
  386. return budgetTime;
  387. }
  388.  
  389. public static int compileCaseAcctualTime(int caseNo) throws SQLException {
  390. Connection con = null;
  391. int acctualTime = 0;
  392. try {
  393. con = connect();
  394.  
  395. PreparedStatement stmt = con.prepareStatement("SELECT acctualTime FROM task JOIN taskInstance ON task.taskNo = taskInstance.taskNo WHERE caseNo=?");
  396. stmt.setString(1, "" + caseNo);
  397. ResultSet rs = stmt.executeQuery();
  398. while (rs.next()) {
  399. acctualTime += rs.getInt("acctualTime");
  400. }
  401. } catch (ClassNotFoundException ex) {
  402. Logger.getLogger(DatabaseTalker.class.getName()).log(Level.SEVERE, null, ex);
  403. } finally {
  404. closeCon(con);
  405. }
  406. return acctualTime;
  407. }
  408.  
  409. private static Connection connect() throws SQLException, ClassNotFoundException {
  410. Connection con = null;
  411. Class.forName("org.apache.derby.jdbc.ClientDriver");
  412. con = DriverManager.getConnection("jdbc:derby://localhost:1527/CMS;create=true;user=FiktivAB;password=12345");
  413. if (con == null) {
  414. throw new SQLException("Connection failed! ");
  415. }
  416. return con;
  417. }
  418.  
  419. private static void closeCon(Connection con) throws SQLException {
  420. if (con != null) {
  421. con.close();
  422. }
  423. }
  424.  
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement