Commit f4a93912 by Patryk Czarnik

Podstawowe przykłady dostępu do bazy sklep

parent a3dd1d77
package dostep_podstawowy;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Locale;
import java.util.Scanner;
public class DodajProdukt {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.US);
final String url = "jdbc:postgresql://localhost:5432/sklep";
final String user = "kurs";
final String password = "abc123";
try(Connection c = DriverManager.getConnection(url , user, password)) {
c.setAutoCommit(false);
while(true) {
System.out.print("Podaj nazwę nowego produktu (pusty napis, aby zakończyć): ");
String name = scanner.nextLine();
if(name.isEmpty())
break;
System.out.print("Podaj cenę: ");
BigDecimal price = scanner.nextBigDecimal();
scanner.nextLine();
System.out.print("Podaj stawkę VAT, np 23 : ");
int vat = scanner.nextInt();
scanner.nextLine();
System.out.print("Podaj opis: ");
String description = scanner.nextLine();
if(description.isEmpty()) {
description = null;
}
// aby wpisać rekord do bazy, tworzymy obiekt bez określonego ID
final String sql = "INSERT INTO products(product_name, price, vat, description) VALUES(?, ?, ?, ?)";
final String[] kolumnyID = {"product_id"};
try(PreparedStatement stmt = c.prepareStatement(sql, kolumnyID)) {
stmt.setString(1, name);
stmt.setBigDecimal(2, price);
stmt.setBigDecimal(3, BigDecimal.valueOf(vat).movePointLeft(2));
stmt.setString(4, description);
stmt.executeUpdate();
// teraz mogę odczytać wygenerowane ID
try(ResultSet rs = stmt.getGeneratedKeys()) {
if(rs.next()) {
int id = rs.getInt(1);
System.out.println("Nowy produkt uzyskał numer " + id);
}
}
}
}
System.out.println("Czy zapisać zmiany? [T/N]");
String wybor = scanner.next().toUpperCase();
switch(wybor) {
case "T":
System.out.println("Zatwierdzam transakcję");
c.commit();
break;
case "N":
System.out.println("Cofam transakcję");
c.rollback();
break;
default:
System.out.println("Rozłączam się bez zatwierdzenia transakcji");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
package dostep_podstawowy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class WypiszProdukty {
public static void main(String[] args) {
final String url = "jdbc:postgresql://localhost:5432/sklep";
final String user = "kurs";
final String password = "abc123";
final String sql = "SELECT * FROM products ORDER BY product_id";
try(Connection c = DriverManager.getConnection(url , user, password);
PreparedStatement stmt = c.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
System.out.printf("%3d: %s, cena %.2f, vat %.0f%%, opis: %s\n",
rs.getInt("product_id"),
rs.getString("product_name"),
rs.getBigDecimal("price"),
rs.getBigDecimal("vat").movePointRight(2), // jeśli nie null
rs.getString("description"));
}
} catch (SQLException e) {
e.printStackTrace();
};
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment