Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 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.Statement;
  6.  
  7. public class HighScores {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         new HighScores();
  12.        
  13.     }
  14.    
  15.     private int choice;
  16.    
  17.     public HighScores(){
  18.        
  19.         try{
  20.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/?autoRecconect=true&&useSSL=false", "root", "");
  21.            
  22.             Statement createDatabase = con.createStatement();
  23.             createDatabase.executeUpdate("CREATE DATABASE ludo");
  24.            
  25.         } catch(Exception ex){
  26.             System.err.println(ex.getMessage());
  27.         }
  28.        
  29.         try{
  30.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ludo?autoRecconect=true&&useSSL=false", "root", "");
  31.            
  32.             Statement createTable = con.createStatement();
  33.             createTable.executeUpdate("CREATE TABLE highscores(ime varchar(30), brojBacanja int, PRIMARY KEY(ime) )");
  34.            
  35.         } catch(Exception ex){
  36.             System.err.println(ex.getMessage());
  37.         }
  38.        
  39.         try{
  40.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ludo?autoRecconect=true&&useSSL=false", "root", "");
  41.            
  42.             if(choice == 1){
  43.                 //add a high score
  44.                
  45.                 PreparedStatement addHighScore = con.prepareStatement("INSERT INTO highscores(ime, brojBacanja) VALUES(?, ?)");
  46.                
  47.                 String ime = "nekoIme";
  48.                 int brojBacanja = 15;
  49.                
  50.                 addHighScore.setString(1, ime);
  51.                 addHighScore.setInt(2, brojBacanja);
  52.                
  53.                 addHighScore.executeUpdate();
  54.                
  55.                
  56.             } else if(choice == 2){
  57.                 //show high scores
  58.                
  59.                 Statement showHighScores = con.createStatement();
  60.                
  61.                 ResultSet rs = showHighScores.executeQuery("SELECT * FROM highscores");
  62.                
  63.                 while(rs.next()){
  64.                     System.out.println(rs.getString("ime") + " " + rs.getInt("brojBacanja"));
  65.                 }
  66.             }
  67.            
  68.         } catch(Exception ex){
  69.             System.err.println(ex.getMessage());
  70.         }
  71.        
  72.        
  73.     }
  74.  
  75.     public int getChoice() {
  76.         return choice;
  77.     }
  78.  
  79.     public void setChoice(int choice) {
  80.         this.choice = choice;
  81.     }
  82.    
  83.    
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement