Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package library.tables;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import library.beans.Admin;
  10. import library.db.ConnectionManager;
  11.  
  12. public class AdminManager {
  13.  
  14. private static Connection conn = ConnectionManager.getInstance().getConnection();
  15.  
  16. public static void displayAllRows() throws SQLException {
  17.  
  18. String sql = "SELECT * FROM admin";
  19. try (
  20. Statement stmt = conn.createStatement();
  21. ResultSet rs = stmt.executeQuery(sql);
  22. ){
  23.  
  24. System.out.println("Admin table");
  25. System.out.println("AdminId:\tUserName:\tPassword");
  26. while (rs.next()) {
  27. StringBuffer bf = new StringBuffer();
  28. bf.append(rs.getInt("adminId") + "\t\t ");
  29. bf.append(rs.getString("userName") + "\t\t ");
  30. bf.append(rs.getString("password"));
  31. System.out.println(bf.toString());
  32. }
  33. }
  34.  
  35. }
  36.  
  37. public static Admin getRow(int adminId) throws SQLException {
  38.  
  39. String sql = "SELECT * FROM admin WHERE adminId = ?";
  40. ResultSet rs = null;
  41.  
  42. try (
  43. PreparedStatement stmt = conn.prepareStatement(sql);
  44. ){
  45. stmt.setInt(1, adminId);
  46. rs = stmt.executeQuery();
  47.  
  48. if(rs.next()) {
  49. Admin bean = new Admin();
  50. bean.setAdminId(adminId);;
  51. bean.setUserName(rs.getString("userName"));
  52. bean.setPassword(rs.getString("password"));
  53. return bean;
  54. } else {
  55. return null;
  56. }
  57. } catch (SQLException e) {
  58. System.err.println(e);
  59. return null;
  60. } finally {
  61. if (rs != null) {
  62. rs.close();
  63. }
  64. }
  65.  
  66. }
  67.  
  68. public static boolean isExists(int adminId) throws Exception {
  69.  
  70. String sql = "SELECT EXISTS(SELECT * FROM admin WHERE adminId = ?) as 'Exists'";
  71. ResultSet rs = null;
  72.  
  73. try (
  74. PreparedStatement stmt = conn.prepareStatement(sql);
  75. ) {
  76.  
  77. stmt.setInt(1, adminId);
  78. rs = stmt.executeQuery();
  79.  
  80. if(rs.next()) {
  81. return rs.getInt("Exists") == 1;
  82. }
  83. } catch(SQLException e) {
  84. System.err.println(e);
  85. e.printStackTrace();
  86. return false;
  87. }
  88.  
  89. return false;
  90. }
  91.  
  92. public static boolean insert(Admin bean) throws Exception {
  93.  
  94. String sql = "INSERT INTO admin VALUES (default, ?, ?)"; //ako ne radi, dodaj (default, ?, ?)
  95.  
  96. try (
  97. PreparedStatement stmt = conn.prepareStatement(sql);
  98. ) {
  99.  
  100. stmt.setString(1, bean.getUserName());
  101. stmt.setString(2, bean.getPassword());
  102. stmt.executeUpdate();
  103.  
  104. } catch (SQLException e) {
  105. System.err.println(e);
  106. return false;
  107. }
  108.  
  109. return true;
  110. }
  111.  
  112.  
  113. public static boolean update(Admin bean) throws Exception {
  114.  
  115. String sql = "UPDATE admin SET userName = ?, password = ? " +
  116. "WHERE adminId = ?";
  117.  
  118. try (
  119. PreparedStatement stmt = conn.prepareStatement(sql);
  120. ){
  121.  
  122. stmt.setString(1, bean.getUserName());
  123. stmt.setString(2, bean.getPassword());
  124. stmt.setInt(3, bean.getAdminId());
  125.  
  126. int affected = stmt.executeUpdate();
  127. if (affected == 1) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. catch(SQLException e) {
  134. System.err.println(e);
  135. return false;
  136. }
  137.  
  138. }
  139.  
  140. public static boolean delete(int adminId) throws Exception {
  141.  
  142. String sql = "DELETE FROM admin WHERE adminId = ?";
  143. try (
  144. PreparedStatement stmt = conn.prepareStatement(sql);
  145. ){
  146.  
  147. stmt.setInt(1, adminId);
  148. int affected = stmt.executeUpdate();
  149.  
  150. if(affected == 1) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155.  
  156. }
  157. catch(SQLException e) {
  158. System.err.println(e);
  159. return false;
  160. }
  161.  
  162. }
  163.  
  164. public static boolean isEmpty() throws SQLException {
  165.  
  166. String sql = "SELECT COUNT(*) FROM admin";
  167. ResultSet rs = null;
  168. try(
  169. PreparedStatement stmt = conn.prepareStatement(sql);
  170. ){
  171.  
  172. rs = stmt.executeQuery();
  173. rs.next();
  174. return rs.getInt("COUNT(*)") == 0;
  175.  
  176. } catch (SQLException e) {
  177. e.printStackTrace();
  178. return false;
  179. }
  180. }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement