Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sklep;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class SklepBazaDanych {
- private Connection con;
- public SklepBazaDanych() throws SQLException {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- System.err.println("Brak biblioteki");
- System.exit(1);
- }
- con = DriverManager.getConnection(
- "jdbc:mysql://localhost/sklep?serverTimezone=UTC",
- "root",
- ""
- );
- }
- public void wyswietlWszystko() throws SQLException {
- Statement st = con.createStatement();
- ResultSet rs = st.executeQuery(
- "SELECT * FROM produkty ORDER BY nazwa");
- while ( rs.next() ) {
- int id = rs.getInt("id");
- String nazwa = rs.getString("nazwa");
- double cena = rs.getDouble("cena");
- int ilosc = rs.getInt("ilosc");
- System.out.println(id+" "+nazwa+" "+cena+" ");
- }
- st.close();
- }
- public void dodajProdukt() throws SQLException {
- Statement st = con.createStatement();
- Scanner scan = new Scanner(System.in);
- System.out.print("Podaj cenΔ produktu: ");
- double cena = Double.parseDouble( scan.nextLine() );
- String nazwa = scan.nextLine();
- int ilosc = Integer.parseInt( scan.nextLine() );
- String query = "INSERT INTO produkty (nazwa, cena, ilosc)"
- + " VALUES ( '"+nazwa+"',"+cena+", "+ilosc+")";
- st.executeUpdate(query);
- st.close();
- }
- public void usunProdukt() {
- }
- public void edytujProdukt() {
- }
- public void sprzedajProdukt() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement