Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class SearchBeer {
  4. public static void main(String[] args) {
  5. String sql = "SELECT * FROM Beers WHERE Alcohol = 12.0 ORDER BY Alcohol";
  6. try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost/StudentDB?useSSL=false", "root", "WACHTWOORDINVULLEN")) {
  7. System.out.println("Connection OK\n");
  8. Statement stmt = con.createStatement();
  9. ResultSet rs = stmt.executeQuery(sql);
  10. while(rs.next()) {
  11. int id = rs.getInt(1);
  12. String beerName = rs.getString(2);
  13. int brewerId = rs.getInt(3);
  14. int categoryId = rs.getInt(4);
  15. double price = rs.getDouble(5);
  16. int stock = rs.getInt(6);
  17. double alcohol = rs.getDouble(7);
  18. int version = rs.getInt(8);
  19.  
  20. System.out.printf("ID: %3d \tName: %-85s \t Brewer: %3d \t Category: %d \t %4.2f EUR \t Stock: %d \t %4.1f %% \t %d %n", id, beerName, brewerId, categoryId, price, stock, alcohol, version);
  21. }
  22. }
  23. catch (Exception ex) {
  24. System.out.println("Something went wrong!");
  25. ex.printStackTrace(System.err);
  26. }
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement