Advertisement
Guest User

Untitled

a guest
Sep 14th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. private Connection connection;
  2. private Statement statement;
  3. private ResultSet result;
  4.  
  5. public DBConnect(){
  6. try {
  7. Class.forName("com.mysql.jdbc.Driver");
  8. connection = DriverManager.getConnection("jdbc:mysql://localhost:1234/practicedb"); //code stucks here and after some minutes it is throwing an exception
  9. System.out.println("Connected");//this is never executed.
  10. statement = connection.createStatement();
  11.  
  12. } catch (Exception ex) {
  13. System.out.print("Error in Constractor: "+ex);
  14. }
  15. }
  16.  
  17. public void getData() {
  18.  
  19. try {
  20.  
  21. String query = "select * from cars";
  22.  
  23. result = statement.executeQuery(query);
  24. while (result.next()) {
  25. String name = result.getString("carName");
  26. String id = result.getString("carID");
  27. String modelNum = result.getString("modelNumber");
  28. System.out.println("Car name: " + name + ", " + "Car ID: " + id + ", " + "Car model number: " + modelNum);
  29. }
  30.  
  31. } catch (Exception ex) {
  32. System.out.println(ex);
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement