Mysql executeQuery...

E

Event Horizon

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
 
H

HightowerC

I'm stuck with my code :(
(snip)

Map[] retmapFile = null;
(snip)

retmapFile = smt.executeQuery(); }
(snip)

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

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

-Event


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

HightowerC
 
E

Event Horizon

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
 
H

HightowerC

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
 
E

Event Horizon

HightowerC said:

something like this:

ResultSet rset = stmt.executeQuery();
while (rset.next) {
//add stuff into your Map[]
}
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
 
H

HightowerC

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top