Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package jdbc;
  6.  
  7. import java.util.*;
  8. import java.sql.*;
  9.  
  10. /**
  11. *
  12. * @author U
  13. */
  14. public class JDBCUtility
  15. {
  16. Connection con;
  17. String driver;
  18. String url;
  19. String userName;
  20. String password;
  21.  
  22. PreparedStatement psInsertStudent = null;
  23. PreparedStatement psSelectAllStudent = null;
  24. PreparedStatement psSelectStudentViaMatriks = null;
  25. PreparedStatement psUpdateStudentNameViaMatriks = null;
  26. PreparedStatement psUpdateAll = null;
  27. PreparedStatement psDeleteStudentViaMatriks = null;
  28. PreparedStatement psVerifyStudentViaMatriks = null;
  29.  
  30. //use this constructor if using ConnectionPool
  31. public JDBCUtility()
  32. {
  33. }
  34.  
  35. //use this constructor if not using ConnectionPool
  36. //ConnectionPool is used for multi user!
  37. public JDBCUtility(String driver,
  38. String url,
  39. String userName,
  40. String password)
  41. {
  42. this.driver = driver;
  43. this.url = url;
  44. this.userName = userName;
  45. this.password = password;
  46. }
  47.  
  48. public void jdbcConnect()
  49. {
  50. try
  51. {
  52. Class.forName (driver);
  53. con = DriverManager.getConnection(url, userName, password);
  54. DatabaseMetaData dma = con.getMetaData ();
  55. System.out.println("\nConnected to " + dma.getURL());
  56. System.out.println("Driver " + dma.getDriverName());
  57. System.out.println("Version " + dma.getDriverVersion());
  58. System.out.println("");
  59. }
  60. catch (SQLException ex)
  61. {
  62. while (ex != null)
  63. {
  64. System.out.println ("SQLState: " +
  65. ex.getSQLState ());
  66. System.out.println ("Message: " +
  67. ex.getMessage ());
  68. System.out.println ("Vendor: " +
  69. ex.getErrorCode ());
  70. ex = ex.getNextException ();
  71. System.out.println ("");
  72. }
  73.  
  74. System.out.println("Connection to the database error");
  75. }
  76. catch (java.lang.Exception ex)
  77. {
  78. ex.printStackTrace ();
  79. }
  80. }
  81.  
  82. public Connection jdbcGetConnection()
  83. {
  84. return con;
  85. }
  86.  
  87. public void jdbcConClose()
  88. {
  89. try
  90. {
  91. con.close();
  92. }
  93. catch (Exception ex)
  94. {
  95. }
  96. }
  97.  
  98. public void prepareSQLStatement()
  99. {
  100. try
  101. {
  102. //select all student
  103. String sqlSelectAllStudent = "SELECT * FROM student";
  104. psSelectAllStudent = con.prepareStatement(sqlSelectAllStudent);
  105.  
  106. //select student with matriks = ?
  107. String sqlSelectStudentViaMatriks = "SELECT * FROM student where matriks = ?";
  108. psSelectStudentViaMatriks = con.prepareStatement(sqlSelectStudentViaMatriks);
  109.  
  110. //insert student
  111. String sqlInsertStudent = "INSERT INTO student(matriks, name, ic, age) VALUES(?, ?, ?, ?)";
  112. psInsertStudent = con.prepareStatement(sqlInsertStudent);
  113.  
  114. //update student name via matriks
  115. String sqlUpdateStudentNameViaMatriks = "UPDATE student SET name = ? WHERE matriks = ?";
  116. psUpdateStudentNameViaMatriks = con.prepareStatement(sqlUpdateStudentNameViaMatriks);
  117.  
  118. //update student via matriks
  119. String sqlUpdateAllViaMatriks = "UPDATE student SET name = ?, ic = ?, age = ? WHERE matriks = ?";
  120. psUpdateAll = con.prepareStatement(sqlUpdateAllViaMatriks);
  121.  
  122. //delete student via matriks
  123. String sqlDeleteStudentViaMatriks = "DELETE FROM student WHERE matriks = ?";
  124. psDeleteStudentViaMatriks = con.prepareStatement(sqlDeleteStudentViaMatriks);
  125.  
  126. //verify student via matriks
  127. String sqlVerifyStudentViaMatriks = "UPDATE student SET verified = ? WHERE matriks = ?";
  128. psVerifyStudentViaMatriks = con.prepareStatement(sqlVerifyStudentViaMatriks);
  129.  
  130. System.out.println(psSelectAllStudent);
  131. }
  132. catch (SQLException ex)
  133. {
  134. while (ex != null)
  135. {
  136. System.out.println ("SQLState: " +
  137. ex.getSQLState ());
  138. System.out.println ("Message: " +
  139. ex.getMessage ());
  140. System.out.println ("Vendor: " +
  141. ex.getErrorCode ());
  142. ex = ex.getNextException ();
  143. System.out.println ("");
  144. }
  145.  
  146. System.out.println("Connection to the database error");
  147. }
  148. catch (java.lang.Exception ex)
  149. {
  150. ex.printStackTrace ();
  151. }
  152. }
  153.  
  154. public PreparedStatement psSelectAllStudent()
  155. {
  156. return psSelectAllStudent;
  157. }
  158.  
  159. public PreparedStatement psSelectStudentViaMatriks()
  160. {
  161. return psSelectStudentViaMatriks;
  162. }
  163.  
  164. public PreparedStatement psInsertStudent()
  165. {
  166. return psInsertStudent;
  167. }
  168.  
  169. public PreparedStatement psUpdateStudentNameViaMatriks()
  170. {
  171. return psUpdateStudentNameViaMatriks;
  172. }
  173.  
  174. public PreparedStatement sqlUpdateAllViaMatriks()
  175. {
  176. return psUpdateAll;
  177. }
  178.  
  179. public PreparedStatement psDeleteStudentViaMatriks()
  180. {
  181. return psDeleteStudentViaMatriks;
  182. }
  183.  
  184. //psVerifyStudentViaMatriks
  185. public PreparedStatement psVerifyStudentViaMatriks()
  186. {
  187. return psVerifyStudentViaMatriks;
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement