Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class DBConnect {
  5. protected Connection conn;
  6. private String usrName = "root";
  7. private String passWd = "**";
  8. private Scanner sc;
  9.  
  10.  
  11.  
  12. public DBConnect() throws Exception {
  13. connect();
  14. sc = new Scanner(System.in);
  15. System.out.println("Skriv inn hvor mange økter du har lyst til å se:");
  16. int n = Integer.valueOf(sc.nextLine());
  17. showWorkOuts(n);
  18. }
  19.  
  20.  
  21. private void showWorkOuts(int n) throws Exception {
  22. Statement sporring = conn.createStatement();
  23. //Finner først IDen til de som er lagt til sist
  24. ResultSet result = sporring.executeQuery("select * from treningsøkt ORDER BY treningsøkt.treningsid DESC LIMIT "
  25. + " " + n + ";");
  26. while(result.next()){
  27. System.out.println("ID: " + result.getString(1) + " Varighet: " + result.getString(2) + " Form: " + result.getString(3) + " Prestasjon: " + result.getString(4) + " Tidspunkt: " + result.getString(5) + " Dato: " + result.getString(6) + " Notat: " + result.getString(7));
  28. }
  29.  
  30. }
  31.  
  32. private void connect() throws SQLException {
  33. try {
  34. Class.forName("com.mysql.jdbc.Driver");
  35. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/treningsdagbok?useSSL=false",usrName,passWd);
  36. System.out.println("Connection running");
  37. }
  38. catch (Exception ex) {
  39. System.out.println("SQLException " + ex.getMessage());
  40. }
  41.  
  42. }
  43.  
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement