Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Scanner;
  8.  
  9. public class Upggift4 {
  10. public static void main(String[] args){
  11.  
  12. Connection conn = null;
  13. PreparedStatement pstm = null;
  14. ResultSet rs = null;
  15. Statement stm = null;
  16. PreparedStatement pstm2 = null;
  17.  
  18. try {
  19. conn = DriverManager.getConnection("jdbc:mysql://localhost/Butik", "root", "");
  20.  
  21. conn.setAutoCommit(false);
  22.  
  23. stm = conn.createStatement();
  24.  
  25. rs = stm.executeQuery("select * from slutilager");
  26.  
  27. int produktnyckel = 0;
  28. while (rs.next()) {
  29. produktnyckel=rs.getInt("Produkt");
  30. System.out.println("Slut i lager: "+rs.getString("Datum")+ ", produktnummer: "+ rs.getString("produkt"));
  31. }
  32. Scanner scanner = new Scanner(System.in);
  33. System.out.println("Vill du ändra i lagerantalet JA/NEJ");
  34.  
  35. String val = scanner.nextLine();
  36.  
  37. int nyttAntal = 0;
  38. switch (val.toUpperCase()) {
  39. case "JA":
  40. System.out.println("Ange hur många i siffror: ");
  41. nyttAntal = scanner.nextInt();
  42. break;
  43. case "NEJ":
  44. System.out.println("Avslutat");
  45. System.exit(0);
  46. break;
  47.  
  48. default:
  49. break;
  50. }
  51.  
  52.  
  53. pstm = conn.prepareStatement("update produkt set lager =? where produktnummer=?");
  54. pstm2 = conn.prepareStatement("delete from slutilager where produkt=?");
  55. pstm2.setInt(1, produktnyckel);
  56. pstm.setInt(1, nyttAntal);
  57. pstm.setInt(2, produktnyckel);
  58.  
  59. int rows1 = pstm2.executeUpdate();
  60. int rows = pstm.executeUpdate();
  61.  
  62. if (rows1 == 1) {
  63. System.out.println(rows + " rows affected");
  64. }
  65.  
  66. if (rows == 1) {
  67. System.out.println(rows + " rows affected");
  68. }
  69.  
  70. scanner.close();
  71. conn.commit();
  72.  
  73. } catch (SQLException e) {
  74. try {
  75. conn.rollback();
  76. e.printStackTrace();
  77. } catch (SQLException e1) {
  78.  
  79.  
  80. }
  81. e.printStackTrace();
  82. } finally {
  83. if (rs != null)
  84. try {
  85. rs.close();
  86. pstm.close();
  87. conn.close();
  88. } catch (SQLException e) {
  89. e.printStackTrace();
  90. }
  91.  
  92. }
  93.  
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement