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.Statement;
- public class HighScores {
- public static void main(String[] args) {
- new HighScores();
- }
- private int choice;
- public HighScores(){
- try{
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/?autoRecconect=true&&useSSL=false", "root", "");
- Statement createDatabase = con.createStatement();
- createDatabase.executeUpdate("CREATE DATABASE ludo");
- } catch(Exception ex){
- System.err.println(ex.getMessage());
- }
- try{
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ludo?autoRecconect=true&&useSSL=false", "root", "");
- Statement createTable = con.createStatement();
- createTable.executeUpdate("CREATE TABLE highscores(ime varchar(30), brojBacanja int, PRIMARY KEY(ime) )");
- } catch(Exception ex){
- System.err.println(ex.getMessage());
- }
- try{
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ludo?autoRecconect=true&&useSSL=false", "root", "");
- if(choice == 1){
- //add a high score
- PreparedStatement addHighScore = con.prepareStatement("INSERT INTO highscores(ime, brojBacanja) VALUES(?, ?)");
- String ime = "nekoIme";
- int brojBacanja = 15;
- addHighScore.setString(1, ime);
- addHighScore.setInt(2, brojBacanja);
- addHighScore.executeUpdate();
- } else if(choice == 2){
- //show high scores
- Statement showHighScores = con.createStatement();
- ResultSet rs = showHighScores.executeQuery("SELECT * FROM highscores");
- while(rs.next()){
- System.out.println(rs.getString("ime") + " " + rs.getInt("brojBacanja"));
- }
- }
- } catch(Exception ex){
- System.err.println(ex.getMessage());
- }
- }
- public int getChoice() {
- return choice;
- }
- public void setChoice(int choice) {
- this.choice = choice;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement