Advertisement
Guest User

mysql java delen

a guest
Feb 27th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package mysqldataconnection;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class MysqlDataConnection {
  11.  
  12.     public static void main(String[] args) {
  13.         opgaver xd = new opgaver();
  14.        
  15.        
  16.         System.out.println("Dette er det ældste lån der findes i databasen: "+xd.opg4());
  17.     }
  18.  
  19. }
  20.  
  21. class opgaver {
  22.  
  23.     public String opg1() {
  24.         String sql = "select * from bog order by udgivelsesår";
  25.         String bog = "";
  26.         try (Connection conn = connection.getConnection();
  27.                 PreparedStatement ps = conn.prepareStatement(sql)) {
  28.             ResultSet rs = ps.executeQuery();
  29.             while (rs.next()) {
  30.                 int id = rs.getInt("BogID");
  31.                 String titel = rs.getString("Titel");
  32.                 String forfatter = rs.getString("Forfatternavn");
  33.                 String forlag = rs.getString("Forlag");
  34.                 int udgivelsesår = rs.getInt("Udgivelsesår");
  35.  
  36.                 bog += id + " " + titel + " " + forfatter + " " + forlag + " " + udgivelsesår + "\n";
  37.                 //System.out.println(id + " " + titel + " " + forfatter + " " + forlag + " " + udgivelsesår);
  38.  
  39.             }
  40.  
  41.         } catch (SQLException e) {
  42.             System.out.println("Fejl i connection til database");
  43.             e.printStackTrace();
  44.         }
  45.         return bog;
  46.     }
  47.  
  48.     public String opg4() {
  49.         String sql = "select * from låner inner join udlån using (lånerid) order by dato limit 1";
  50.         String ældsteLån = " ";
  51.         try (Connection conn = connection.getConnection();
  52.                 PreparedStatement ps = conn.prepareStatement(sql)) {
  53.             ResultSet rs = ps.executeQuery();
  54.             while (rs.next()) {
  55.                 int lånerID = rs.getInt("LånerID");
  56.                 String navn = rs.getString("Navn");
  57.                 String adresse = rs.getString("Adresse");
  58.                 int postnr = rs.getInt("Postnr");
  59.                 int bogID = rs.getInt("BogID");
  60.                 Date dato = rs.getDate("Dato");
  61.  
  62.                 ældsteLån += lånerID + " " + navn + " " + adresse + " " + postnr + " " + bogID + " " + dato + "\n";
  63.             }
  64.         } catch (SQLException e) {
  65.             System.out.println("Fejl i connection til database");
  66.             e.printStackTrace();
  67.  
  68.         }
  69.         return ældsteLån;
  70.     }
  71.  
  72. }
  73.  
  74. class connection {
  75.  
  76.     public static Connection getConnection() throws SQLException {
  77.         String url = "jdbc:mysql://root@localhost:3306/bibliotek";
  78.         String user = "root";
  79.         String password = "uqt42vqx";
  80.  
  81.         Connection conn = DriverManager.getConnection(url, user, password);
  82.  
  83.         return conn;
  84.  
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement