Guest User

Untitled

a guest
Feb 14th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package com.gces.jdbc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class Test {
  10.  
  11. public static void main(String[] args) throws NullPointerException {
  12.  
  13. String sql = "SELECT * FROM `employees`";
  14.  
  15. try
  16. {
  17. // Load and register driver.
  18. Class.forName("com.mysql.cj.jdbc.Driver"); // Driver Class Name
  19. // Establish connection between apps and database.
  20. Connection connectionObject = DriverManager.getConnection("jdbc:mysql://localhost:3306/gces","root","");
  21. // Creation of Statement Object
  22. Statement statementObject = connectionObject.createStatement();
  23. // Send and execute SQL Query
  24. ResultSet rs = statementObject.executeQuery(sql);
  25. // Process Result from ResultSet
  26. while(rs.next()) {
  27. System.out.println("ID : " + rs.getInt(1));
  28. System.out.println("Name : " + rs.getString(2));
  29. System.out.println("Address : " + rs.getString(3));
  30. System.out.println("Salary : " + rs.getFloat(4));
  31. }
  32. // Close Connection
  33. connectionObject.close();
  34. }
  35. catch(SQLException exc)
  36. {
  37. exc.printStackTrace();
  38. }
  39. catch(ClassNotFoundException exc)
  40. {
  41. exc.printStackTrace();
  42. }
  43. }
  44. }
Add Comment
Please, Sign In to add comment