Kev Nish
Kev Nish

Reputation: 11

How to retrieve data from MS Access?

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){}
}

}

Upvotes: 0

Views: 3258

Answers (1)

user2380133
user2380133

Reputation:

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.

Upvotes: 1

Related Questions