Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. Connection conn = null;
  2.  
  3.  
  4. PreparedStatement ps = null;
  5.  
  6. ResultSet rs = null;
  7.  
  8. try {
  9.  
  10. conn = JDBCUtilsWithProperties.getConnection();
  11.  
  12.  
  13.  
  14. String sql = "select * from users where username = ? and password = ?;";
  15.  
  16. ps = conn.prepareStatement(sql);
  17.  
  18.  
  19. ps.setString(1, username);
  20. ps.setString(2, password);
  21.  
  22.  
  23. rs = ps.executeQuery(sql);
  24.  
  25. if(rs.next()){
  26. System.out.println("SUCESS");
  27. }else{
  28. System.out.println("FAIL");
  29. }
  30.  
  31. } catch (Exception e) {
  32. // TODO: handle exception
  33. e.printStackTrace();
  34. }finally{
  35.  
  36. JDBCUtilsWithProperties.release(rs, ps, conn);
  37. }
  38.  
  39. }
  40.  
  41. private static String driverClass;
  42. private static String url;
  43. private static String username;
  44. private static String password;
  45.  
  46.  
  47. private JDBCUtilsWithProperties(){}
  48.  
  49.  
  50. static{
  51. try {
  52.  
  53. readConfig();
  54.  
  55. Class.forName(driverClass);
  56. } catch (Exception e) {
  57. // TODO: handle exception
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. public static void readConfig(){
  66.  
  67. Properties pp = new Properties();
  68. try {
  69.  
  70. pp.load(new FileInputStream("src//config.properties"));
  71.  
  72. driverClass = pp.getProperty("driverClass");
  73. url = pp.getProperty("url");
  74. username = pp.getProperty("username");
  75. password = pp.getProperty("password");
  76.  
  77. } catch (Exception e) {
  78. // TODO: handle exception
  79. }
  80. }
  81.  
  82.  
  83. public static Connection getConnection(){
  84. try {
  85. return DriverManager.getConnection(url,username,password);
  86. } catch (Exception e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. }
  90. return null;
  91. }
  92.  
  93. public static Statement createStatement(){
  94. Connection conn = getConnection();
  95. Statement stat = null;
  96. try {
  97. stat = conn.createStatement();
  98. } catch (Exception e) {
  99. // TODO Auto-generated catch block
  100. e.printStackTrace();
  101. }
  102. return stat;
  103. }
  104.  
  105.  
  106. public static void release(ResultSet rs,Statement stat,Connection conn){
  107. if(rs != null){
  108. try {
  109. rs.close();
  110. } catch (Exception e) {
  111. // TODO: handle exception
  112. e.printStackTrace();
  113. }
  114. rs = null;
  115. }
  116. if(stat != null){
  117. try {
  118. stat.close();
  119. } catch (Exception e) {
  120. // TODO: handle exception
  121. e.printStackTrace();
  122. }
  123. stat = null;
  124. }
  125. if(conn != null){
  126. try {
  127. conn.close();
  128. } catch (Exception e) {
  129. // TODO: handle exception
  130. e.printStackTrace();
  131. }
  132. conn = null;
  133. }
  134. }
  135.  
  136.  
  137. public static void release(Statement stat,Connection conn){
  138. if(stat != null){
  139. try {
  140. stat.close();
  141. } catch (Exception e) {
  142. // TODO Auto-generated catch block
  143. e.printStackTrace();
  144. }
  145. stat = null;
  146. }
  147. if(conn != null){
  148. try {
  149. conn.close();
  150. } catch (Exception e) {
  151. // TODO Auto-generated catch block
  152. e.printStackTrace();
  153. }
  154. conn = null;
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement