Guest User

Untitled

a guest
Jan 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. String newq="select *from account";
  2. ResultSet rs = s.executeQuery(newq);
  3.  
  4. package exotiqueDatabaseConnection;
  5.  
  6. import java.sql.*;
  7. import java.io.PrintWriter;
  8. import java.io.Serializable;
  9.  
  10. public class ConnectDatabase implements Serializable {
  11.  
  12. Statement stmt;
  13. Connection conn;
  14. ResultSet rs;
  15. String username;
  16. String password;
  17. String driver;
  18. String url;
  19.  
  20. public ConnectDatabase()
  21. {
  22. try{
  23. username="root";
  24. password="does001";
  25.  
  26. driver="com.mysql.jdbc.Driver";
  27. Class.forName(driver);
  28.  
  29. url="jdbc:mysql://localhost/exotique";
  30. conn=DriverManager.getConnection(url, username, password);
  31.  
  32. stmt=conn.createStatement();
  33.  
  34. }
  35. catch(ClassNotFoundException cnfe)
  36. {
  37.  
  38. }
  39. catch (SQLException sq)
  40. {
  41.  
  42. }
  43.  
  44.  
  45.  
  46. }
  47.  
  48. public void populateUsers(String usernameIn, String emailIn, String passwordIn)
  49. {
  50. try{
  51.  
  52. /* username="root";
  53. password="does001";
  54.  
  55. driver="com.mysql.jdbc.Driver";
  56. try {
  57. Class.forName(driver);
  58. } catch (ClassNotFoundException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62.  
  63. url="jdbc:mysql://localhost/exotique";
  64. conn=DriverManager.getConnection(url, username, password);
  65.  
  66. stmt=conn.createStatement();*/
  67.  
  68.  
  69. CallableStatement cs=conn.prepareCall("{call populateusers(?,?,?)}");
  70. cs.setString(1, usernameIn);
  71. cs.setString(2, emailIn);
  72. cs.setString(3, passwordIn);
  73. cs.execute();
  74. }
  75. catch(SQLException sq)
  76. {
  77. System.out.print(sq.getMessage());
  78. }
  79. catch(Exception ex)
  80. {
  81. System.out.print(ex.getMessage());
  82. }
  83. finally
  84. {
  85. close(conn);
  86. close(rs);
  87. close(stmt);
  88. }
  89. }
  90.  
  91. @SuppressWarnings("finally")
  92. public ResultSet showAllBooks()
  93. {
  94. try{
  95.  
  96.  
  97.  
  98. CallableStatement cs=conn.prepareCall("{call getAllBooks}");
  99.  
  100. cs.execute();
  101. rs=cs.getResultSet();
  102.  
  103.  
  104.  
  105.  
  106. }
  107. catch(SQLException sq)
  108. {
  109. System.out.print(sq.getMessage());
  110. }
  111. catch(Exception ex)
  112. {
  113. System.out.print(ex.getMessage());
  114. }
  115. finally
  116. {
  117. close(conn);
  118. close(rs);
  119. close(stmt);
  120. return rs;
  121. }
  122.  
  123.  
  124. }
  125.  
  126. public void close(Statement stmt)
  127. {
  128. if(stmt!=null)
  129. {
  130. try{
  131. stmt.close();
  132. }
  133. catch(SQLException e)
  134. {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139.  
  140. public void close(Connection conn)
  141. {
  142. if(conn!=null)
  143. {
  144. try
  145. {
  146. conn.close();
  147. }
  148. catch(SQLException e)
  149. {
  150. e.printStackTrace();
  151. }
  152. }
  153. }
  154.  
  155. public void close(ResultSet rs)
  156. {
  157. if(rs!=null)
  158. {
  159. try
  160. {
  161. rs.close();
  162. }
  163. catch(SQLException e)
  164. {
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169.  
  170.  
  171.  
  172. }
Add Comment
Please, Sign In to add comment