SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package opgaver;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. public class TestSQL {
  11.  
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.    
  15.         try {
  16.             System.out.println("Program startet");
  17.             Connection minConnection;
  18.             BufferedReader inLine = new BufferedReader(new InputStreamReader(System.in));
  19.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  20.             minConnection = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;" +
  21.                     "databaseName=konto;user=sa;password=mathlikser1;");
  22.            
  23.             Statement stmt = minConnection.createStatement();
  24.             System.out.println("indtast frakontoNR:");
  25.             int frakonto = Integer.parseInt(inLine.readLine());
  26.             System.out.println("indtast tilkontoNR:");
  27.             int tilkonto = Integer.parseInt(inLine.readLine());
  28.             System.out.println("indtast beløb:");
  29.             int beløb = Integer.parseInt(inLine.readLine());
  30.            
  31.             ResultSet res=stmt.executeQuery("select * from konto where kontonr = " + frakonto);
  32.             if (res.next()) {
  33.                 int fraSaldo = res.getInt(2);
  34.                 System.out.println("frakonto har saldo: " + fraSaldo);
  35.                 if (fraSaldo > beløb) {
  36.                     ResultSet res2=stmt.executeQuery("select * from konto where kontonr = " + tilkonto);
  37.                     if (res2.next()) {
  38.                         int tilSaldo = res2.getInt(2);
  39.                         System.out.println("tilkonto har saldo: " + tilSaldo);
  40.                         fraSaldo = fraSaldo - beløb;
  41.                         tilSaldo = tilSaldo + beløb;
  42.                         stmt.execute("update konto set saldo = " + fraSaldo + " where kontonr = " + frakonto);
  43.                         stmt.execute("update konto set saldo = " + tilSaldo + " where kontonr = " + tilkonto);
  44.                         System.out.println("Kontoer opdateret!");
  45.                     }
  46.                 }
  47.             }
  48.         if (stmt != null)
  49.           stmt.close();
  50.         if (minConnection != null)
  51.           minConnection.close();
  52.         }
  53.         catch (Exception e) {
  54.             System.out.print("fejl:  "+e.getMessage());
  55.         }
  56.        
  57.     }
  58.    
  59.  
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top