I have for example the following code line
ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemID='"+ X +"'");
if item table scheme is: ItemID,itemName,Count,location
how does the row from the table "sit" in the ResultSet?
is there a way to take the entire row and push it to the first cell of ArrayList (that is the value i must return and i can not change it, for other queries with multiple line results each line will be in a different cell of the array)?
you can iterate over your resultset using ResultSet.next() and get the row information on every iteration and add it into your array list.
List<YourObj> ref = new ArrayList<>();
ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemID='"+ X +"'");
while(rs.next())
ref.add(new YourObj(rs.getName(), rs.getWhatever()));
}
Here's the reference from Oracle Trails
Related
I am using the ucanaccess tool to interact with my access database, but I do not know how to display or store the resulting ResultSets as Strings.
Edit: I have tried =System.out.println(resultSetId.getString(1)); but that only returns errors.
ResultSet resultSetId;
String msAccDB = "D://Computer Science//passManager//src//Manager.accdb";
String dbURL = "jdbc:ucanaccess://" + msAccDB;
connection = DriverManager.getConnection(dbURL);
statement = connection.createStatement();
resultSetId = statement.executeQuery("SELECT ID FROM Passwords WHERE ID =
\""+identifier+"\";");
System.out.println(resultSetId.getString(1));
Gives the error Exception in thread "main" net.ucanaccess.jdbc.Ucanaccessenter code hereSQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is positioned before first row
Edit: Adding the .next() fixed it, thanks!
ResultSet is positioned before first row
When executeQuery returns the ResultSet it is not pointing at a row of data. You need to call resultSetId.next() in order to retrieve each row. That is often done in the context of a while loop when the ResultSet is expected to return more than one row, e.g.,
while (resultSetId.next()) {
System.out.println(resultSetId.getString(1)); // print value from each row
}
but you can simply call it once if you only expect a single row, e.g.,
if (resultSetId.next()) {
System.out.println(resultSetId.getString(1));
} else {
System.out.println("The ResultSet contained no rows.");
}
I want to retrieve rows from my database one by one and view it in jtextfields present in a GUI form which I made. I am able to retrieve the first row, but never the second or third or fourth,etc.
Please help. If you need more information please ask
String SQL = "select CATEGORY,ITEM,QTY,Avg_Price from (select CATEGORY,ITEM,QTY,AVG_PRICE, rownum rw from final_inv) where rw ="+i+"";
rs = stmt.executeQuery(SQL);
if(rs.next()){
jTextField1.setText(rs.getString(1));
jTextField2.setText(rs.getString(2));
jTextField3.setText(rs.getString(3));
jTextField4.setText(rs.getString(4));
}
i++;
rs=stmt.executeQuery(SQL);
if(rs.next()){
jTextField5.setText(rs.getString(1));
jTextField6.setText(rs.getString(2));
jTextField7.setText(rs.getString(3));
jTextField8.setText(rs.getString(4));
}
I am new to java so pelase excuse my newbie mistakes
Don't directly set text to your jTextField intance inside your loop otherwise your jTextField won't get updated. But temp those text in variable, then set it to jTextField after your loop ends. And don't use 'if' rather 'while' , because you want to iterate each row in database
String SQL = "select CATEGORY,ITEM,QTY,Avg_Price from (select CATEGORY,ITEM,QTY,AVG_PRICE, rownum rw from final_inv) where rw ="+i+"";
String text [] = new String [10]; // just for temp the strings
rs = stmt.executeQuery(SQL);
while(rs.next()){
text[1] +=rs.getString(1);
text[2] +=rs.getString(2);
text[3] +=rs.getString(3);
text[4] +=rs.getString(4);
}
jTextField1.setText(rs.getString(text[1] ));
jTextField2.setText(rs.getString(text[2] ));
jTextField3.setText(rs.getString(text[3] ));
jTextField4.setText(rs.getString(text[4] ));
i++;
rs=stmt.executeQuery(SQL);
while (rs.next()){
text[1] +=rs.getString(1);
text[2] +=rs.getString(2);
text[3] +=rs.getString(3);
text[4] +=rs.getString(4);
}
jTextField5.setText(rs.getString(text[1]));
jTextField6.setText(rs.getString(text[2]));
jTextField7.setText(rs.getString(text[3]));
jTextField8.setText(rs.getString(text[4]));
<%
String qTotal = "SELECT MsThread.ID, MsThread.ThreadName, Count(MsThread.ThreadName) AS TotalPost, ThreadCategory FROM MsThread LEFT OUTER JOIN MsPosts ON MsThread.ThreadName = MsPosts.ThreadName GROUP BY MsThread.ID, MsThread.ThreadName, MsThread.ThreadCategory;";
ResultSet rs = stmt.executeQuery(qTotal);
int size = 0;
while(rs.next())
{
out.print(size++);%><br/><%
}
rs.first();
out.println(rs.getString("id"));
rs.next();
out.println(rs.getString("id"));
rs.next();
out.println(rs.getString("id"));
if(!rs.next())
{
out.println("no data");
}
else
out.println(rs.getString("id"));
%>
Guys, here's what happened, when i tried to run the query it shows 3 result (http://s29.postimg.org/6qiv9kc9j/ss_forum.png) but when i run the
while(rs.next())size++;
it returns 4, and when i try to show the data, it gives me invalid cursor state, so is there anything wrong with my query?
thanks
The default sensitivity of a ResultSet is TYPE_FORWARD_ONLY, which means that it cannot be scrolled; you cannot call any of these methods that move the cursor -
previous
first
last
beforeFirst
afterLast
relative(int rows)
absolute(int row)
Except next, your ResultSet cannot be scrolled. If your resultset if created with default sensitivity then they can not be scrolled using rs.first(); after moving to last end via rs.next() in while statement.
but when i run the while(rs.next())size++; it returns 4,
When a ResultSet object is first created, the cursor is positioned before the first row, so you are getting 4.
Thanks
I have the following SQL line (within a loop):
ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemName='"+ string.get(1) +"'");
string.get(1) contains a different string each time the loop goes.
in the current SQL line I will revive only the lines that are equal to string.get(1),
but I'd like to get all lines that string.get(1) is a sub string of itemName
I know it should go: %string.get(1)% however I don't know the exact syntax.
Anyone can help?
Use the LIKE clause in SQL.
ResultSet rs = stmt.executeQuery
("SELECT * FROM item WHERE itemName LIKE '%"+ string.get(1) +"%'");
you mean this?
SELECT * FROM TABLE WHERE COL LIKE '%SOME_TEXT%';
This is java program for SQL statement. I have two queries. Result of the first query is required for second query.
How do I call it in second query?
These results are values for xml tags.
I need to get first query result for this tag
child1.setAttributeNS(xlink,"xlink:type","");
but this is located in 2nd query and if i try to merge those 2 query I get eror resultset closed.
while(rs1.next()){
int i=0,j=0 ,locid,supid;
int lc[]=new int[100];
int sp[]=new int[100];
lc[i] = rs1.getInt(2);
sp[j] = rs1.getInt(1);
lcid=lc[i++];*/
for(i=0;i<loc[i];i++){
for(j=0;j<sup[j];j++){
lcid=lc[i]; spid=sp[j];
System.out.print(spid +" ");
System.out.println(lcid);
String s = (lc[i]==1 ? "simple" : (lc[i]>1 ? "extended" : null));
System.out.println(s); }}}
String querystring=
" ";
rs = stmt.executeQuery(querystring );
while(rs.next()){
Element child1 = doc.createElement("slink");
/
Element element = doc.createElement("loc");
You need to create a brand new and separate Statement for the second ResultSet. Everytime you get a new ResultSet out of a single Statement, every previously opened one will namely be closed.
Replace
rs = stmt.executeQuery(querystring );
by
Statement stmt2 = connection.createStatement();
rs = stmt2.executeQuery(querystring);
Don't forget to add stmt2.close() to the finally block.
Unrelated to the concrete problem, have you considered just JOINing the both queries and using Javabeans to represent the model? This way you end up with a single query and more self-documenting code.