Advertisement
Guest User

SklepBazaDanych

a guest
Oct 28th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1.  
  2. package sklep;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Scanner;
  10.  
  11.  
  12. public class SklepBazaDanych {
  13.    
  14.     private Connection con;
  15.    
  16.     public SklepBazaDanych() throws SQLException {
  17.         try {
  18.             Class.forName("com.mysql.cj.jdbc.Driver");
  19.         } catch (ClassNotFoundException ex) {
  20.             System.err.println("Brak biblioteki");
  21.             System.exit(1);
  22.         }
  23.         con = DriverManager.getConnection(
  24.                     "jdbc:mysql://localhost/sklep?serverTimezone=UTC",
  25.                     "root",
  26.                     ""
  27.             );
  28.     }
  29.    
  30.     public void wyswietlWszystko() throws SQLException {
  31.         Statement st = con.createStatement();
  32.  
  33.         ResultSet rs = st.executeQuery(
  34.                "SELECT * FROM produkty ORDER BY nazwa");
  35.  
  36.         while ( rs.next() ) {
  37.            int id = rs.getInt("id");
  38.            String nazwa = rs.getString("nazwa");
  39.            double cena = rs.getDouble("cena");
  40.            int ilosc = rs.getInt("ilosc");
  41.            System.out.println(id+" "+nazwa+" "+cena+" ");
  42.         }      
  43.         st.close();
  44.     }
  45.    
  46.     public void dodajProdukt() throws SQLException {    
  47.         Statement st = con.createStatement();
  48.         Scanner scan = new Scanner(System.in);
  49.         System.out.print("Podaj cenΔ™ produktu: ");
  50.         double cena = Double.parseDouble( scan.nextLine() );
  51.  
  52.         String nazwa = scan.nextLine();
  53.  
  54.         int ilosc = Integer.parseInt( scan.nextLine() );
  55.  
  56.  
  57.         String query = "INSERT INTO produkty (nazwa, cena, ilosc)"
  58.         + " VALUES ( '"+nazwa+"',"+cena+", "+ilosc+")";
  59.         st.executeUpdate(query);
  60.         st.close();
  61.     }
  62.    
  63.     public void usunProdukt() {      
  64.        
  65.     }
  66.  
  67.     public void edytujProdukt() {        
  68.     }
  69.    
  70.     public void sprzedajProdukt() {        
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement