Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package ua.dex.data;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. * Created by dexter on 07.01.17.
  7. */
  8. public class Data {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. System.out.println("Loading driver...");
  13.  
  14. try {
  15. Class.forName("com.mysql.jdbc.Driver").newInstance();
  16. System.out.println("Driver loaded!");
  17. } catch (ClassNotFoundException e) {
  18. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  19. } catch (IllegalAccessException e) {
  20. e.printStackTrace();
  21. } catch (InstantiationException e) {
  22. e.printStackTrace();
  23. }
  24.  
  25.  
  26. String url = "jdbc:mysql://localhost:3306/dexchat";
  27. String username = "root";
  28. String password = "root";
  29.  
  30. System.out.println("Connecting database...");
  31.  
  32. try (Connection connection = DriverManager.getConnection(url, username, password)) {
  33. System.out.println("Database connected!");
  34.  
  35. PreparedStatement statement = connection.prepareStatement("SELECT * FROM clients");
  36. ResultSet rs = statement.executeQuery();
  37. System.out.println("All clients");
  38. while (rs.next()){
  39. System.out.println("id : " + rs.getInt("id"));
  40. System.out.println("email : " + rs.getString("email"));
  41. System.out.println("login : " + rs.getString("login"));
  42. System.out.println("name : " + rs.getString("name"));
  43. System.out.println("pass : " + rs.getString("pass"));
  44. }
  45.  
  46. } catch (SQLException e) {
  47. throw new IllegalStateException("Cannot connect the database!", e);
  48. }
  49.  
  50.  
  51.  
  52. }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement