Reputation: 11
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
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