Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Mysql executeQuery...

Thread view: 
Event Horizon - 02 Jul 2007 14:47 GMT
I'm stuck with my code :(
I changing from Interbase to mysql but for some reason I cannot make
them work if resultsets should be Map[]

This works with
Interbase:#######################################################

private Map[] information(int OrderID, int nro) throws Exception
{
 String queryFile = "SELECT ORDER_DET.REMOTENAME, ORDER_DET.PID,
ORDER_DET.QTY, ORDER_DET.VARIABLELENGTH FROM ORDER_DET WHERE
(ORDER_DET.ORDID = ?) AND (ORDER_DET.RNBR = ?)";
 String[] argsFile = new String[2];
 argsFile[0] = OrderID + "";
 argsFile[1] = nro + "";
 Map[] retmapFile = null;
 try
 {
  retmapFile = connection.executeQuery(queryFile, argsFile);
 }
 catch (Exception e)
 {
  System.out.println ("Failed to query information ...");
  e.printStackTrace ();
 }
 return retmapFile;
}

This is what I'm
trying#########################################################

private Map[] information(int OrderID, int nro) throws Exception
{
 String queryFile = "SELECT ORDER_DET.REMOTENAME, ORDER_DET.PID,
ORDER_DET.QTY, ORDER_DET.VARIABLELENGTH FROM ORDER_DET WHERE
(ORDER_DET.ORDID = ?) AND (ORDER_DET.RNBR = ?)";
 String[] argsFile = new String[2];
 argsFile[0] = OrderID + "";
 argsFile[1] = nro + "";
 Map[] retmapFile = null;
 try
 {
  Class.forName(DRIVER).newInstance();
  Connection conn=DriverManager.getConnection(URL);
  PreparedStatement smt = conn.prepareStatement(queryFile, argsFile);
  retmapFile = smt.executeQuery();  }
 catch (Exception e)
 {
  System.out.println ("Failed to query information ...");
  e.printStackTrace ();
 }
 return retmapFile;
}

Error:
found   : java.sql.ResultSet
required: java.util.Map[]
                       retmapFile = smt.executeQuery();

java.util.Map is imported. What I'm doing wrong?

-Event
HightowerC - 02 Jul 2007 15:46 GMT
> I'm stuck with my code :(

(snip)

>   Map[] retmapFile = null;

(snip)

>    retmapFile = smt.executeQuery();  }

(snip)

> Error:
> found   : java.sql.ResultSet
[quoted text clipped - 4 lines]
>
> -Event

PreparedStatement.executeQuery() returns a ResultSet object. You
cannot store a ResultSet object in a Map[] type.

HightowerC
Event Horizon - 02 Jul 2007 15:55 GMT
"HightowerC" <chris.hightower@gmail.com> kirjoitti
viestissä:1183387569.099327.49180@n60g2000hse.googlegroups.com...

> PreparedStatement.executeQuery() returns a ResultSet object. You
> cannot store a ResultSet object in a Map[] type.

Thanks, I noticed that...
I just cannot find a way to get Map type results.
Maybe I had too many beers at my summer vacation...

-Event
HightowerC - 02 Jul 2007 16:29 GMT
> Thanks, I noticed that...
> I just cannot find a way to get Map type results.
> Maybe I had too many beers at my summer vacation...
>
> -Event

something like this:

ResultSet rset = stmt.executeQuery();
while (rset.next) {
  //add stuff into your Map[]
}
return retmapFile;

HightowerC
HightowerC - 02 Jul 2007 16:32 GMT
> while (rset.next) {

Sorry...that should be

while (rset.next()) {

HightowerC
Event Horizon - 02 Jul 2007 17:04 GMT
"HightowerC" <chris.hightower@gmail.com> kirjoitti
viestissä:1183390198.464448.132440@k79g2000hse.googlegroups.com...
>> -Event
>
[quoted text clipped - 5 lines]
> }
> return retmapFile;

Thanks again,

That's exactly what I tried earlier today, but I think my problem was
adding stuff to Map. I'm completely newbie with Java :(
Could you maybe show me the syntax to do that?
I tried to Google but no help....

Thanks in advanced,

-Event
HightowerC - 02 Jul 2007 21:58 GMT
> Could you maybe show me the syntax to do that?
> I tried to Google but no help....
>
> Thanks in advanced,
>
> -Event

Well, I'm not very experienced in Java either, but as I understand it,
Map is an interface.
You will have to instantiate a class that implements Map (such as
HashMap).

Do you really need an array of Map objects, or do you just want 1 Map
object that contains data returned by the Query?

If you only need 1 map object containing data, then look at the
JavaDocs for the HashMap class, and specifically the .put(K key, V
value) method.

If you need an array of Map objects, then create a new map on each
iteration and then add this Map object into the array. (Note that you
will have to set the size of the array beforehand, and you may not
know exactly how many elements will be returned from the database via
the Query).

BTW, I'm not sure that HashMap is the best choice for this situation.
I don't know a whole lot about the different collections in Java.
Maybe someone else in the NG can point out a better solution.

HightowerC


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.