Advertisement
Guest User

Untitled

a guest
May 20th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package getReq;
  2.  
  3. import java.sql.*;
  4.  
  5. import com.mysql.jdbc.ResultSet;
  6. import com.mysql.jdbc.Statement;
  7.  
  8. public class DatabaseCon {
  9.  
  10. // JDBC driver name and database URL
  11. static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  12. static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
  13.  
  14. // Database credentials
  15. static final String USER = "root";
  16. static final String PASS = "qwaszxrc/";
  17. int id;
  18. String firstname;
  19. String lastname;
  20. String email =null;
  21. Date reg_date;
  22. Connection conn = null;
  23. java.sql.Statement stmt = null;
  24.  
  25.  
  26.  
  27. public void getConn() {
  28.  
  29. try {
  30. // STEP 2: Register JDBC driver
  31. Class.forName(JDBC_DRIVER).newInstance();
  32.  
  33. // STEP 3: Open a connection
  34. System.out.println("Connecting to a selected database...");
  35. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  36. System.out.println("Connected database successfully...");
  37.  
  38. // STEP 4: Execute a query
  39. System.out.println("Creating statement...");
  40. stmt = conn.createStatement();
  41.  
  42. String sql = "SELECT id, firstname, lastname,email,reg_date FROM MyGuests";
  43. java.sql.ResultSet rs = stmt.executeQuery(sql);
  44. // STEP 5: Extract data from result set
  45. while (rs.next()) {
  46. // Retrieve by column name
  47. id = rs.getInt("id");
  48. firstname = rs.getString("firstname");
  49. lastname = rs.getString("lastname");
  50. email = rs.getString("email");
  51. reg_date = rs.getDate("reg_date");
  52.  
  53. // Display values
  54. System.out.print("ID: " + id);
  55. System.out.print(",FirstName: " + firstname);
  56. System.out.print(", LastName: " + lastname);
  57. System.out.println(", Email: " + email);
  58. System.out.println(", RegistrationDate is: " + reg_date);
  59.  
  60. }
  61.  
  62. rs.close();
  63.  
  64. } catch (SQLException se) {
  65. // Handle errors for JDBC
  66. se.printStackTrace();
  67. } catch (Exception e) {
  68. // Handle errors for Class.forName
  69. e.printStackTrace();
  70. } finally {
  71. // finally block used to close resources
  72. try {
  73. if (stmt != null)
  74. conn.close();
  75. } catch (SQLException se) {
  76. } // do nothing
  77. try {
  78. if (conn != null)
  79. conn.close();
  80. } catch (SQLException se) {
  81. se.printStackTrace();
  82.  
  83. }
  84. }
  85. }
  86.  
  87.  
  88. public String getEmail() {
  89. return email;
  90. }
  91.  
  92.  
  93. public void setEmail(String email) {
  94. this.email = email;
  95. }
  96.  
  97.  
  98. public static void main(String[] args) {
  99. DatabaseCon testcon = new DatabaseCon();
  100. testcon.getConn();
  101. testcon.getEmail();
  102. System.out.println(testcon.getEmail());
  103. }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement