简体   繁体   中英

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM