Java ArrayList acting up - java

I am running in to an odd issue. I have a database with 6 different "bids". I am trying to extract the bids with the following code:
public ArrayList<ProjectBid> getProjectBids(String projectID)
{
ArrayList<ProjectBid> result = new ArrayList<ProjectBid>();
ProjectBid bid = new ProjectBid();
try{
st = con.createStatement();
String query = "SELECT * FROM bids WHERE projectID=\""+projectID+"\"";
System.out.println(query);
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
bid.setProjectID(projectID);
bid.setBidID(rs.getString("bidID"));
bid.setBidderEmail(rs.getString("bidder_email"));
bid.setBidAmount(rs.getInt("bid_amount"));
bid.setBidMessage(rs.getString("bid_message"));
bid.setTimestamp(rs.getString("timestamp"));
result.add(bid);
}
st.close();
}
catch(Exception ex){
System.out.println("Exception: "+ex);
}
return result;
}
For some reason my ArrayList result is giving me 6 identical "bid" objects instead of adding each of the "bids" from my database individually. The code runs through the ResultSet as expected and the correct values for each run is printed correctly if I add "System.out.println(bid.getBidID);" inside my while-loop. Can anyone tell me what is wrong with my code?
Thank you

You should move ProjectBid bid = new ProjectBid(); inside the while loop. With the current code you are updating the same bid variable which is defined outside the while loop hence it gets the last record's value after the loop completes.

Add
ProjectBid bid = new ProjectBid();
in your while loop. As of now you are just overwriting the same ProjectBid object. So each reference in you ArrayList is pointing to the same object value of which you keep changing in your loop. So at the end you will have all elements in ArrayList with data same as last row retrieved from the database.
while (rs.next())
{
ProjectBid bid = new ProjectBid();
bid.setProjectID(projectID);
bid.setBidID(rs.getString("bidID"));
bid.setBidderEmail(rs.getString("bidder_email"));
bid.setBidAmount(rs.getInt("bid_amount"));
bid.setBidMessage(rs.getString("bid_message"));
bid.setTimestamp(rs.getString("timestamp"));
result.add(bid);
}

Since the reference to the bid is always the same thereby, it is overwriting the bid with the current value that in turn is changing all the bids in the result list giving you the last bid added.
Instantiate the bid object inside your loop and this should work fine.
while (rs.next())
{
ProjectBid bid = new ProjectBid();
...
...
...
result.add(bid);
}

Related

How to display record from oracle database using select keyword

I want to display a record from an Oracle database in Java using select keyword, but it's not working. When I run the program it displays the SQL command but not the result I want.
My code is like this:
static void modify()
{
System.out.println("Enter employee ID");
empid=sc.nextInt();
try{
con=Connection1.getConnection();
stmt=con.createStatement();
String d="select * from emp where empid='"+empid+"'";
rs=stmt.executeQuery(d);
System.out.println(""+d);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
When I run the application this result is shown instead of record from database:
select * from emp where empid='14'
The problem is you are just printing the name of your results not the results itself.
I have changed your code as below,
static void modify()
{
System.out.println("Enter employee ID");
empid=sc.nextInt();
try{
con=Connection1.getConnection();
stmt=con.createStatement();
String d="select * from emp where empid='"+empid+"'";
rs=stmt.executeQuery(d);
while(rs.next()){
//Getting column value from record by giving column no
System.out.println(rs.getString(1)); //line 1
//Getting column value from record by giving column name,
System.out.println(rs.getString("empid"));// line 2
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
Please note that above code (line 1) will print the first column of your each records( returned from db), here the order of the returned columns is not guaranteed.
If you want to get Specific column then you can specify the column name as argument in the getString method of resultSet (line 2).
You have to use appropriate methods to get values such that , if column 2 has the data type in DB as INTEGER then you have to use rs.getInt(2).
In your code,
System.out.println(""+rs);
In Java when you call println(Object) method in System.out,which will call String.valueOf(Object) method which is again will call toString() method, which would return the String representation of your object, here nothing but the query you passed, not the records.
You are printing the toString() representation of the Statement. Instead, you should execute the statement (which you did), and get the relevant string from the ResultSet.
Frankly, RestulSet doesn't give the most comfortable way to iterate its columns, but it's doable:
int colCount = rs.getMetaData().getColumnCount()
List<Object> columnValues = new ArrayList<>(colCount);
// Move to the first row
// If your query returns more than one row,
// you'd probably want to iterate over all of them
rs.next();
// Iterate the columns:
for (int i = 1; i <= colCount; ++i) {
list.add(rs.getObject(i));
}
System.out.println ("Record: " + list);

Result set stored in hashMap giving zero row count

I have two methods in my class, First I am calling method dbExecuteStatement(), which execute the sql query. After execution of sql query, I get a ResultSet object. I am saving this ResultSet object in a static hashMap, so that on my next method call fetchResults(), I can use the existing result set to retrieve the results. Reason for saving the ResultSet object in a map is ,in fetchResults() method request parameter, I will get the max fetch row size, and on basis of that value I will be iterating the result set. Both of this methods are supposed to be called individual from the client side.
Now the problem, I am facing is that, When I am iterating the ResultSet object in fetchResults() method, I am getting the row count zero. If I fetch the same ResultSet from a hashMap in dbExecuteStatement(), I get the actual row count i.e 5 in my case. I checked the ResultSet object that I have put in the hash map in fetchResults() method and dbExecuteStatement(), it is the same object. But If get the ResultSetMetaData object in fetchResults() method and dbExecuteStatement(), they are coming different. Can someone help me in understanding the cause, Why I am getting the result count zero.
Below is the code:
public class HiveDao1 {
private static Map<Object,Map<Object,Object>> databaseConnectionDetails
= new HashMap<Object,Map<Object,Object>>();
//This method will execute the sql query and will save the ResultSet obj in a hashmap for later use
public void dbExecuteStatement(DbExecuteStatementReq dbExecuteStatementReq){
//I already have a connection object saved in map
String uniqueIdForConnectionObject = dbExecuteStatementReq.getDbUniqueConnectionHandlerId();
Map<Object,Object> dbObject = databaseConnectionDetails.get(uniqueIdForConnectionObject);
Connection connection = (Connection) dbObject.get(DatabaseConstants.CONNECTION);
try {
Statement stmt = connection.createStatement() ;
// Execute the query
ResultSet resultSet = stmt.executeQuery(dbExecuteStatementReq.getStatement().trim()) ;
//save the result set for further use, Result set will be used in fetchResult() call
dbObject.put(DatabaseConstants.RESULTSET, resultSet);
/*
* Now below is the debugging code,which I put to compare the result set
* iteration dbExecuteStatement() and fetchResults method
*/
ResultSet rs = (ResultSet) dbObject.get(DatabaseConstants.RESULTSET);
ResultSetMetaData md = (ResultSetMetaData) dbObject.get(DatabaseConstants.RESULTSETMETADATA);
System.out.println("==ResultSet fethced in dbExecuteStatement=="+rs);
System.out.println("==ResultSet metadata fetched in dbExecuteStatement ==="+rs.getMetaData());
int count = 0;
while (rs.next()) {
++count;
}
if (count == 0) {
System.out.println("No records found");
}
System.out.println("No of rows found from result set in dbExecuteStatement is "+count);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* This method fetch the result set object from hashMap
* and iterate it on the basis of fetch size received in req parameter
*/
public void fetchResults(FetchResultsReq fetchResultsReq){
String uniqueIdForConnectionObject = fetchResultsReq.getDbUniqueConnectionHandlerId();
Map<Object,Object> dbObject = databaseConnectionDetails.get(uniqueIdForConnectionObject);
try {
//Fetch the ResultSet object that was saved by dbExecuteStatement()
ResultSet rs = (ResultSet) dbObject.get(DatabaseConstants.RESULTSET);
ResultSetMetaData md = (ResultSetMetaData) dbObject.get(DatabaseConstants.RESULTSETMETADATA);
System.out.println("ResultSet fethced in fetchResults at server side dao layer======"+rs);
System.out.println("ResultSet metadata fetched in fetchResults at server side dao layer======"+md);
int count = 0;
while (rs.next()) {
++count;
}
if (count == 0) {
System.out.println("No records found");
}
//Here the row count is not same as row count in dbExecuteStatement()
System.out.println("No of rows found from result set in fetchResults is "+count);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Expanding on my comment (And #Glenn's):
Using a ResultSet more than once
When you write debug code that iterates a ResultSet, the cursor moves to the end of the results. Of course, if you then call the same object and use next(), it will still be at the end, so you won't get any more records.
If you really need to read from the same ResultSet more than once, you need to execute the query such that it returns a scrollable ResultSet. You do this when you create the statement:
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY );
The default statement created by connection.createStatement() without parameters returns a result set of type ResultSet.TYPE_FORWARD_ONLY, and that ResultSet object can only be read once.
If your result set type is scroll insensitive or scroll sensitive, you can use a statement like rs.first() to reset the cursor and then you can fetch the records again.
Keeping the statement in scope
#Glenn's comment is extremely important. The way your program works right now, it may work fine throughout the testing phase, and then suddenly in production, you'll sometimes have zero records in your ResultSet, and the error will be reproducible only occasionally - a debug nightmare.
If the Statement object that produces the ResultSet is closed, the ResultSet itself is also closed. Since you are not closing your Statement object yourself, this will be done when the Statement object is finalized.
The stmt variable is local, and it's the only reference to that Statement that we know of. Therefore, it will be claimed by the garbage collector. However, objects that have a finalizer are relegated to a finalization queue, and there is no way of knowing when the finalizer will be called, and no way to control it. Once it happens, the ResultSet becomes closed out of your control.
So be sure to keep a reference to the statement object alongside your ResultSet. And make sure you close it properly yourself once you are done with the ResultSet and will not be using it anymore. And after you close it remember to remove the reference you have kept - both for the statement and the result set - to avoid memory leaks. Closing is important, and relying on finalizers is a bad strategy. If you don't close it yourself, you might run out of cursors at some point in your database (depending on the DBMS and its configuration).

Java mysql query ("SELECT * FROM movies" is only returning 1 row

I´m doing a simple movie-renting application, and this method from the DAO section is supposed to return an array of "movie" objects, but only returns 1 movie.
I checked the database, connection is fine, but only list the first row and nothing else. There are currently 3 movies entries on the database.
public ArrayList<Pelicula> obtainMovies () {
ArrayList<Movie> p=new ArrayList<>();
Movie pelic=new Movie();
try{
conn=connect();
String sql="SELECT * FROM movies";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
pelic.setTitle(rs.getString("title"));
pelic.setGenre(rs.getString("genre"));
pelic.setRating(rs.getInt("rating"));
pelic.setRented(rs.getBoolean("rented"));
p.add(pelic);
}
else {return null;}
return p;
}
Assuming your table has more then one row, this
if(rs.next()){
should use a loop like
while(rs.next()){
Also, you'll need to add more then one instance to your List, so move pelic into the loop body
while(rs.next()){
Movie pelic=new Movie();
Or you'll add only one instance of Movie to the List (and modify that single instance with each loop iteration).
if(rs.next()){
You probably want to loop over all results:
while(rs.next()){

Database doesn't like reading values from a loop

I have a java database successfully connected to my java code. Thats all fine as it works and all.
When I store a result from the database into a variable ... it works perfectly.
Now as I have to do this 8 times I used a loop and a array however by using a try catch tool it gives out a error of, Error is: java.lang.NullPointerException
Futher investigation shows that it seems to not like the loop strangely.
public String Title []; //in class but out of any methods
public void gettinginfo ()
{
try
{
int AB = 0; //array base starts from 0
//ID in database starts from 1
for (int i = 1; i<=8; i++)
{
String query = "SELECT * FROM students WHERE ID = " + i;
Rs = St.executeQuery(query);
while (Rs.next())
{
Title[AB] = Rs.getString("StudentName");
AB++;
}
}
}
catch (Exception ex)
{
System.out.println("Error is: " + ex);
}
}
What line is your NullPointerException occurring on? Likely your Title array has not been initialized. If you know how many rows the query will return, you can say:
Title = new String[numRows];
But if you don't, you'll need to either run a SELECT count(*) ... query or use an ArrayList or other resizable list, instead of an array.
Your code is also very poorly structured, which is no small part of why you're having trouble debugging this. I've cleaned up your code below, with comments explaining my changes:
public class YourClass
{
private static final int MAX_ID = 8; // or however you want to set the size
private String[] title; // [] after the type is easier to read, lower case variables
private Connection conn; // I'm assuming the class will be provided a DB connection
// Note the Statement and ResultSet objects are not defined in the class, to
// minimize their scope.
public void queryInfo() // name suggests a query (potentially expensive) will be run
{
title = new String[MAX_ID]; // **initialize title**
// We use a try-with-resources block to ensure the statement is safely closed
// even better would be to use a PreparedStatement here
try(Statement st = conn.statement())
{
// You're executing 8 separate queries here, where one will do
String query = "SELECT * FROM students WHERE ID >= 1 AND ID <= "+MAX_ID;
// Again, we need to close the result set when we're done
try(ResultSet rs = st.executeQuery(query))
{
int i = 0;
while (rs.next())
{
title[i++] = rs.getString("StudentName");
}
} // close our ResultSet
} // close our Statement
}
// provide a separate getter method, rather than making the array public
public String[] getTitles()
{
return title;
}
}
There's still more that could be improved - using an array seems like a poor design, as does calling a method which populates a class variable rather than simply having queryInfo() return a new array. You can also look into using PreparedStatement. Hopefully these suggestions help.
Make sure that Title array and Statement St objects are and not null. These are the only two reasons that I suspect. Give FULL stacktrace if it doesn't work.
Title array is NULL. "new" this array to the size equal to number of rows. If you don't know the rows, fire a count(*) query first, find out the no of rows and then intantiate Title array or use ArrayList<String> instead of String array.
I am assuming that you have not initialized your Title array, you have to set it equal to something or it will just be null which will cause a nullPointerException, but as others have stated there is no way to be sure since your haven't given us a full stack trace or even the line number of the exception. In this case the exception should be handled as such:
try{
//your code here
}catch(Exception ex){
ex.printStackTrace();
}
This code will give you the full stack trace making it much easier to track down the issue.
Also you may want to consider using an ArrayList instead of an array:
List<String> Title = new ArrayList<String>();
Then to add to it:
Title.add(Rs.getString("StudentName"));
If you need it as an array later then:
String[] title = Title.toArray(new String[Title.size()]);
You can read more about ArrayLists here.

ResultSet reset when using recursion

I have a question regarding ResultSet objects in Java and recursion.
I was working on some code for university and whenever I found a descendant I recursed on with that new node but when I came out of the recursion and tried to rs.next() the pointer had gone from pointing to row 1 back to row 0 and when it hit row 0 the rs.next() failed and it returned! I knew there was one thing in there that it hadn't read yet! What is it that causes this?
The only way I got round that problem was to go through the resultset and get every element and add it into an array list, then loop through the arraylist doing the recursion on each element in the array! Surely this must be a better way around this?
This is the new code I'm using
private Vector<String> getDescendents(String dogname, Vector<String> anc) {
if (anc == null) anc = new Vector<String>();
ArrayList<String> tempList = new ArrayList<String>(2);
try {
System.out.println("Inside ");
childStmt.setString(1,dogname);
childStmt.setString(2,dogname);
ResultSet rs = childStmt.executeQuery();
System.out.println("Before while "+rs.getRow());
while (rs.next()){
String col1 = rs.getString(1);
tempList.add(col1);
anc.add(col1);
}
for (String s:tempList){
getDescendents(s,anc);
}
}
catch(Exception e) {
doError(e, "Failed to execute ancestor query in getBreeding");
}
return anc;
}
However before this, I had the getDescendents call inside the while loop and thus no for loop and no arraylist either, but whenever it actually recursed it would loose track of the resultset when it returned out of the recursion.
Further details :
When I used the debugger (nearly said gdb there lol far too much C) the ID of the result set was the same but the row pointer had returned to 0 and the rs.next call failed!
Once again any explanation is appreciated!
p.s it previously looked like
private Vector<String> getDescendents(String dogname, Vector<String> anc) {
if (anc == null) anc = new Vector<String>();
ArrayList<String> tempList = new ArrayList<String>(2);
try {
System.out.println("Inside ");
childStmt.setString(1,dogname);
childStmt.setString(2,dogname);
ResultSet rs = childStmt.executeQuery();
System.out.println("Before while "+rs.getRow());
while (rs.next()){
String col1 = rs.getString(1);
anc.add(col1);
getDescendendts(col1,anc);
}
}
catch(Exception e) {
doError(e, "Failed to execute ancestor query in getBreeding");
}
return anc;
}
It looks like you're re-using childStmt; don't do this. From the Statement javadoc:
By default, only one ResultSet object per Statement object can be open
at the same time. Therefore, if the reading of one ResultSet object is
interleaved with the reading of another, each must have been generated
by different Statement objects. All execution methods in the Statement
interface implicitly close a statment's current ResultSet object if an
open one exists.
You'll have to either save all the rows first, then do the recursive query, or create a new Statement for each ResultSet you want to fetch.

Categories

Resources