Reputation: 371
In my DB theres a field named 'failcounter' and its an int.
Which query i have to use to receive the int?
i tried with:
SELECT `failcounter` FROM `randomstuff`
and tried to receive the int with:
if(zrs.next()){
return zrs.getInt(1);
}else{
return -99;
}
but im not able to get the int.
Whats wrong? =)
Here is the whole method, maybe theres something wrong:
public static int getFailCounter() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection(""+MyBot.mysqlDbPath+"",""+MyBot.mysqlDbUsername+"",""+MyBot.mysqlDbPassword+"");
PreparedStatement zpst=null;
ResultSet zrs=null;
zpst=connect.prepareStatement("SELECT `failcounter` FROM `randomstuff`");
zrs=zpst.executeQuery();
if(zrs.next()){
return zrs.getInt(1);
}else{
return -99;
}
}catch (Exception e) {
throw e;
} finally {
close();
}
}
Upvotes: 0
Views: 73
Reputation: 8197
From your comment I always get -99
It means that ,
if(zrs.next()){
return zrs.getInt(1);
}
doesnt gets executed . Also understand the differences using if(zrs.next())
and while(zrs.next())
You usually use "while" as you want to loop through all the data in the result set. You use "if" when the query returns one row by definition
So in your case the ResultSet
must be null or the column index might be wrong . so check for the Data in the table first
Hope this helps !
Upvotes: 2
Reputation: 659
Try you this.
ResultSet res = st.executeQuery("SELECT * FROM event");
while (res.next())
{
int id = res.getInt("id");
String msg = res.getString("msg");
System.out.println(id + "\t" + msg);
}
And follow more read [How to connect with MySQL database using Java
Read more: http://mrbool.com/how-to-connect-with-mysql-database-using-java/25440#ixzz30N93JAkm]1
Best of Luck!
Upvotes: -1