Advertisement
Guest User

Java DB to Variable

a guest
May 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. public class main extends safe{
  2.     static Scanner sc = new Scanner(System.in);
  3.     public static String PID = "76561198142035536";
  4.     public static String conURL = "jdbc:mysql://185.243.8.136:3306/altislife?verifyServerCertificate=false&useSSL=true";
  5.     public static void main(String [] args){
  6.         try{
  7.             Class.forName("com.mysql.jdbc.Driver");
  8.             Connection con=DriverManager.getConnection(conURL, user, pw);
  9.  
  10.  
  11.             Statement stmt=con.createStatement();
  12.  
  13.             //String input = "SELECT bankacc FROM players WHERE pid'76561198142035536'";
  14.             String DBinput = "SELECT pid, name, cash, bankacc, adminlevel, donorlevel, playtime FROM players WHERE pid='"+ PID+ "'";
  15.             ResultSet rs=stmt.executeQuery(DBinput);
  16.             System.out.println("1 Ausgabe | 2 Eingabe | 3 Beenden");
  17.             int eingabe = sc.nextInt();
  18.             switch(eingabe){
  19.             case 1: readDB(rs,stmt,con) ;break;
  20.             case 2: break;
  21.             case 3: System.out.println("System ist beendet!"); System.exit(0)  ;break;
  22.             }
  23.             conClose(con);
  24.  
  25.         }catch(Exception e){ System.out.println(e);}
  26.     }
  27.     private static void conClose(Connection con) throws SQLException {
  28.         con.close();
  29.     }
  30.     private static void readDB(ResultSet db,Statement stmt,Connection con) throws SQLException {
  31.         while(db.next()){
  32.             int test1  = db.getInt("bankacc");
  33.             System.out.println("Geben sie einen Betrag ein: ");
  34.             int test2 =  sc.nextInt();
  35.             test1 = test1+test2;
  36.             System.out.println(test1);
  37.             updatedb(stmt, con, test1);
  38.         }
  39.     }
  40.     private static void updatedb(Statement stmt, Connection con, int money) throws SQLException {
  41.         String DBUpdater = "UPDATE players SET bankacc='" + money +"' WHERE pid='" + PID + "'";
  42.         int rs = stmt.executeUpdate(DBUpdater);
  43.         System.out.println(rs);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement