SQL and JDBC question

Anything that's not related to F-SIM Space Shuttle.

Moderators: blackbird1955, frankynov, desktopsimmer, Elefant

SQL and JDBC question

UNREAD_POSTby frankynov » Fri Apr 06, 2012 4:02 pm

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
expired.png (6.81 KiB) Viewed 13367 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 :

code.PNG


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(SQLError.java:1073)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
   at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:843)
   at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5649)
   at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5569)


Any idea ?
frankynov
 
Posts: 845
Joined: Sun Feb 14, 2010 4:48 pm
Location: Namur, Belgium

Re: SQL and JDBC question

UNREAD_POSTby Sascha » Fri Apr 06, 2012 4:30 pm

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",…)

use

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).
Sascha
Site Admin
 
Posts: 3232
Joined: Wed Jan 27, 2010 12:43 pm

Re: SQL and JDBC question

UNREAD_POSTby frankynov » Fri Apr 06, 2012 4:57 pm

Thanks Sascha, I'll try and investigate your suggestions :)
frankynov
 
Posts: 845
Joined: Sun Feb 14, 2010 4:48 pm
Location: Namur, Belgium

Re: SQL and JDBC question

UNREAD_POSTby frankynov » Mon Apr 09, 2012 7:49 pm

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 :)
frankynov
 
Posts: 845
Joined: Sun Feb 14, 2010 4:48 pm
Location: Namur, Belgium

Re: SQL and JDBC question

UNREAD_POSTby rabie » Tue Aug 26, 2014 1:44 pm

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.
rabie
rabie
 
Posts: 1
Joined: Tue Aug 26, 2014 1:42 pm


Return to Off Topic

Who is online

Users browsing this forum: No registered users and 4 guests

cron