Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5.  
  6. public class Test {
  7. public static void main(String[] args) {
  8. try {
  9. // 1. Register JDBC Driver
  10. Class.forName("com.mysql.jdbc.Driver");
  11.  
  12. // 2. Create Connection Object
  13. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/demoDB","root", "");
  14.  
  15. // 3. Create Statement object and Execute a Query
  16. Statement stmt = con.createStatement();
  17.  
  18. // 4. Extract data from result set
  19. ResultSet rs = stmt.executeQuery("select * from users");
  20. while(rs.next()) {
  21. System.out.println(rs.getInt("id") + " " + rs.getString("name"));
  22. }
  23.  
  24. // 5. Close the connection
  25. con.close();
  26.  
  27. } catch(Exception e){
  28. // Handle errors here
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement