Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Upggift4 {
- public static void main(String[] args){
- Connection conn = null;
- PreparedStatement pstm = null;
- ResultSet rs = null;
- Statement stm = null;
- PreparedStatement pstm2 = null;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost/Butik", "root", "");
- conn.setAutoCommit(false);
- stm = conn.createStatement();
- rs = stm.executeQuery("select * from slutilager");
- int produktnyckel = 0;
- while (rs.next()) {
- produktnyckel=rs.getInt("Produkt");
- System.out.println("Slut i lager: "+rs.getString("Datum")+ ", produktnummer: "+ rs.getString("produkt"));
- }
- Scanner scanner = new Scanner(System.in);
- System.out.println("Vill du ändra i lagerantalet JA/NEJ");
- String val = scanner.nextLine();
- int nyttAntal = 0;
- switch (val.toUpperCase()) {
- case "JA":
- System.out.println("Ange hur många i siffror: ");
- nyttAntal = scanner.nextInt();
- break;
- case "NEJ":
- System.out.println("Avslutat");
- System.exit(0);
- break;
- default:
- break;
- }
- pstm = conn.prepareStatement("update produkt set lager =? where produktnummer=?");
- pstm2 = conn.prepareStatement("delete from slutilager where produkt=?");
- pstm2.setInt(1, produktnyckel);
- pstm.setInt(1, nyttAntal);
- pstm.setInt(2, produktnyckel);
- int rows1 = pstm2.executeUpdate();
- int rows = pstm.executeUpdate();
- if (rows1 == 1) {
- System.out.println(rows + " rows affected");
- }
- if (rows == 1) {
- System.out.println(rows + " rows affected");
- }
- scanner.close();
- conn.commit();
- } catch (SQLException e) {
- try {
- conn.rollback();
- e.printStackTrace();
- } catch (SQLException e1) {
- }
- e.printStackTrace();
- } finally {
- if (rs != null)
- try {
- rs.close();
- pstm.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement