0

I'm new to using Database in java.

I would like to know how to retrieve data from MS Access and display it in my java program.

Also, how can I make the scores sorted from highest to lowest since it is a game program and I want to display the high scores.

I place my code below, it is supposed to save the names and scores to an MS access database.

package Final;

import java.sql.*;

public class GameDatabase{

Connection connect;
Statement state;
ResultSet result;

GameDatabase(){ 
    try{    
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connect = DriverManager.getConnection("jdbc:odbc:GameDB");
        state = connect.createStatement();  

        addProfile();
    }
    catch(Exception e){}
}
public void addProfile(){
    try{
        result = state.executeQuery("SELECT * FROM tblScores");
        String s1="INSERT INTO tblScores(Name, Score) VALUES('"+Character.playerName+"',"+Character.score+")";
        state.executeUpdate(s1);
    }
    catch(Exception e){}
}

}
2
  • Your DB url seems to be wrong "jdbc:odbc:GameDB" Oct 5, 2013 at 16:31
  • 1
    can you share the error you're getting? where exactly is the problem?
    – Mureinik
    Oct 5, 2013 at 16:41

1 Answer 1

1

this line is wrong

state.executeUpdate(s1);

you should write state.executeQuery(s1);

executeUpdate() is used for inserting or updating or deleting while executeQuery() is used for select queries.

1
  • My code above is really for inserting names and scores in the database. I just need codes on how to retrieve those data that i inserted. thanks for the quick reply anyway. :)
    – Kev Nish
    Oct 5, 2013 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.