Page 1 of 1

SQL and JDBC question

UNREAD_POSTPosted: Fri Apr 06, 2012 4:02 pm
by frankynov
Hello everyone !

I know it's maybe not the right forum here, but I've a developper question...
I'm trying to develop a java software to access my personnal Mysql database located on a web server.
So far, I can read some of the variables and SELECT some others. That's pretty cool and works well.

But I've a problem with a variable type, named 'expired', it's the enum type. As to PhpMyadmin, it's an extension of a String. The result should be 'True' or 'False' Here is how my data look like :

expired.png (6.81 KiB) Viewed 15060 times

But when I try to access this data with my software, it doesn't work :( I'm trying to access it as a String but I think I have to user another type. But I've absolutely no idea at all about how to get this data...

Here is the code :


I've already tried with a result.getBoolean(); but nothing is working.
Error log :

Code: Select all
java.sql.SQLException: Before start of result set
   at com.mysql.jdbc.SQLError.createSQLException(
   at com.mysql.jdbc.SQLError.createSQLException(
   at com.mysql.jdbc.SQLError.createSQLException(
   at com.mysql.jdbc.SQLError.createSQLException(
   at com.mysql.jdbc.ResultSetImpl.checkRowPos(
   at com.mysql.jdbc.ResultSetImpl.getStringInternal(
   at com.mysql.jdbc.ResultSetImpl.getString(

Any idea ?

Re: SQL and JDBC question

UNREAD_POSTPosted: Fri Apr 06, 2012 4:30 pm
by Sascha
I don’t know.
Check the JDBC mySql driver documentation, it should tell you which Java datatype mySql enum is mapped to. In general, when working with JDBC, avoid fancy datatypes in the mysql database and use only primitives (you know, int, float, String, date, etc.)

A better (and more portable way) to implement an enum type in a database would be to use a separate table, containing an index and a String, and join them in the select statement

instead of

enum ("alpha", "beta", "gamma",…)


table myEnum
int key, String value
0, "alpha"
1, "beta",
2, "gamma"

(and don’t forget to add an index to get acceptable performance)

This way you don’t rely on any mySql proprietary datatypes and can easily migrate to a different RDBMS or switch to a different front end.

A quick hack would be to simply use integers in your database (instead of enums), and map them to the correct string-representation in the Java front-end (e.g. read them as strings and then manually map them to a Java enum type).

Re: SQL and JDBC question

UNREAD_POSTPosted: Fri Apr 06, 2012 4:57 pm
by frankynov
Thanks Sascha, I'll try and investigate your suggestions :)

Re: SQL and JDBC question

UNREAD_POSTPosted: Mon Apr 09, 2012 7:49 pm
by frankynov
I got it !
And OMG... I really shouldn't code so late ! :)

With a simple
Code: Select all
ResultSet result = state.executeQuery("SELECT id FROM `keys` WHERE expired='False'");

instead of
Code: Select all
ResultSet result = state.executeQuery("SELECT id FROM `keys`);

It selects the correct row with the filter expired...
Thanks again for the clues :)

Re: SQL and JDBC question

UNREAD_POSTPosted: Tue Aug 26, 2014 1:44 pm
by rabie
Different Android devices require different versions of F-Sim Space Shuttle. Usually the Google Play store handles that and sends the correct version to your device.