Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package toka;
  2. import java.sql.*;
  3.  
  4. public class Test {
  5. static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
  6. //static final String DB_URL = "jdbc:mariadb://localhost/db20";
  7. static final String DB_URL = "jdbc:mysql://localhost/db20?";
  8. // Database credentials
  9. static final String USER = "mikko20";
  10. static final String PASS = "kt123456";
  11.  
  12. public static void main(String[] args) {
  13. Connection conn = null;
  14. Statement stmt = null;
  15. try {
  16. //STEP 2: Register JDBC driver
  17. Class.forName(JDBC_DRIVER);
  18. System.out.println("Connecting to a selected database...");
  19. /*conn = DriverManager.getConnection(
  20. DB_URL, USER, PASS);*/
  21. conn = DriverManager.getConnection(
  22. DB_URL + "user=" + USER + "&password=" + PASS);
  23. System.out.println("Connected database successfully...");
  24. stmt = conn.createStatement();
  25. String sql = "SELECT * FROM REGISTRATION;";
  26. ResultSet rs = stmt.executeQuery(sql); // rs olioon palautuu tietuejoukko
  27. while (rs.next())
  28. {
  29. int id = rs.getInt("id");
  30. String firstName = rs.getString("first");
  31. String lastName = rs.getString("last");
  32. int age = rs.getInt("age");
  33. // print the results
  34. System.out.format("%s, %s, %s, %s\n", id, firstName, lastName, age);
  35. }
  36. stmt.close();
  37. }
  38. catch (SQLException se) {
  39. se.printStackTrace();
  40. }
  41. catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. finally {
  45. try {
  46. if (stmt != null) {
  47. conn.close();
  48. }
  49. }
  50. catch (SQLException se) {
  51.  
  52. }
  53. try {
  54. if (conn != null) {
  55. conn.close();
  56. }
  57. } catch (SQLException se) {
  58. se.printStackTrace();
  59. }
  60. }
  61. System.out.println("Goodbye!");
  62.  
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement