Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package database_repeat;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Database_Repeat
  6. {
  7. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  8. static final String DB_URL = "jdbc:mysql://localhost:3306/example";
  9.  
  10. static final String USER = "root";
  11. static final String PASS = "";
  12.  
  13. public static void main(String[] args)
  14. {
  15. Connection conn = null;
  16. Statement stmt = null;
  17.  
  18. try
  19. {
  20. Class.forName("com.mysql.jdbc.Driver");
  21.  
  22. System.out.println("Connecting to database...");
  23. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  24.  
  25. System.out.println("Creating statement...");
  26. stmt = conn.createStatement();
  27. String sql;
  28. sql = "SELECT id, first, last, age FROM example";
  29. ResultSet rs = stmt.executeQuery(sql);
  30.  
  31. while(rs.next())
  32. {
  33. int id = rs.getInt("id");
  34. int age = rs.getInt("age");
  35. String first = rs.getString("first");
  36. String last = rs.getString("last");
  37.  
  38. System.out.print("ID: " + id);
  39. System.out.print(", Age: " + age);
  40. System.out.print(", First: " + first);
  41. System.out.println(", Last: " + last);
  42. }
  43. rs.close();
  44. stmt.close();
  45. conn.close();
  46. }
  47. catch(SQLException se)
  48. {
  49. se.printStackTrace();
  50. }
  51. catch(Exception e)
  52. {
  53. e.printStackTrace();
  54. }
  55. finally
  56. {
  57. try
  58. {
  59. if(stmt!= null)
  60. {
  61. stmt.close();
  62. }
  63. }
  64. catch(SQLException se)
  65. {
  66. se.printStackTrace();
  67. }
  68. }
  69. System.out.print("Goodbye! :)");
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement