Guest User

Untitled

a guest
Dec 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package dbTest;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.Statement;
  10.  
  11. /**
  12. * @author mn
  13. *
  14. */
  15. public class DbFtg1 {
  16.  
  17. static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  18. static final String DB_URL = "jdbc:mysql://java.bx.nimell.se:33306/javadb";
  19.  
  20. // Database credentials
  21. static final String USER = "coffee";
  22. static final String PASS = "hunter12";
  23.  
  24. /**
  25. * @param args
  26. */
  27. public static void main(String[] args) {
  28. // TODO Auto-generated method stub
  29.  
  30. Connection conn = null;
  31. Statement stmt = null;
  32. try {
  33. // Register JDBC driver
  34. Class.forName(JDBC_DRIVER);
  35.  
  36. // Open a connection
  37. System.out.println("Connecting to database...");
  38. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  39.  
  40. stmt = conn.createStatement();
  41. String sql = "SELECT id, first, last, salary FROM Employees";
  42.  
  43. ResultSet result = stmt.executeQuery(sql);
  44.  
  45. // Show result
  46. while (result.next()) {
  47. // Retrieve by column name
  48. int id = result.getInt("id");
  49. int age = result.getInt("salary");
  50. String first = result.getString("first");
  51. String last = result.getString("last");
  52.  
  53. // Display values
  54. System.out.print("ID: " + id);
  55. System.out.print("\t Pay: " + age);
  56. System.out.print("\tFirst: " + first);
  57. System.out.println("\tLast: " + last);
  58.  
  59. }
  60. // Clean-up
  61. result.close();
  62. stmt.close();
  63. conn.close();
  64.  
  65. } catch (Exception e) {
  66. System.out.println("Something went wrong");
  67. }
  68.  
  69. }
  70.  
  71. }
Add Comment
Please, Sign In to add comment