Guest User

Untitled

a guest
Nov 23rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. public static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  2. public static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/myDatabase";
  3. public static final String DB_USER = "root";
  4. public static final String DB_PASSWORD = "password";
  5.  
  6. try {
  7. Class.forName(Database.DB_DRIVER);
  8. } catch (ClassNotFoundException e) {
  9. System.out.println(e);
  10. }
  11.  
  12. Connection connection = null;
  13. try {
  14. connection = DriverManager.getConnection(Database.DB_CONNECTION,
  15. Database.DB_USER,
  16. Database.DB_PASSWORD);
  17. } catch (SQLException e) {
  18. System.out.println(e);
  19. }
  20.  
  21. try {
  22. PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM USERS WHERE USER_NAME = ?");
  23. preparedStatement.setString(1, "Alex");
  24. ResultSet rs = preparedStatement.executeQuery();
  25. while (rs.next()) {
  26. String result = rs.getString("USER_NAME")
  27. System.out.println(result);
  28. }
  29. } catch (SQLException ex) {
  30. System.out.println(ex);
  31. }
Add Comment
Please, Sign In to add comment