Pregunta

Got a java class that connects to an apache derby database. Suddenly stopped working and i can't work out why.

Basically a client connects to the server, the server calls the "contact" class, and then the contact class queries the database and returns the result.

Here is the code from the Contact class:

import java.sql.*;

//Contact Class. Used to get information from the contact database.
//Takes the database connection and the student ID as arguments
public class Contact {
//Define the original variables
String student = null;
Connection db = null;
PreparedStatement selectStatement = null;
ResultSet resultSet = null;
StringBuilder result = null;

//Constructor method. Used to set passed arguments to class variables.
public Contact(Connection conn, String studentNumber)
{
    this.student = studentNumber;
    this.db = conn;
}

//getResult method used to query the database and return result.
public String getResult()
{
    //Wrap it all in a try loop to catch sql errors.
    try {
        //Set up the statement, prepare the statement and set the variable.
        String selectSQL = "SELECT * FROM Contact WHERE STUID = ?";
        selectStatement = db.prepareStatement(selectSQL);                   
        selectStatement.setString(1, this.student);
        //Execute the query
        resultSet = selectStatement.executeQuery();
        //If there is no results, set up a string to return letting the user know.
        this.result = new StringBuilder();
        if(!resultSet.next())
        {
            result.append("No record for ");
            result.append(this.student);
            result.append(".");
        }
        else
        {
            //If there are results, loop through the result and build a string
            //To be able to return to the user with the correct details.
            System.out.println("FOUND A RESULT");
            while(resultSet.next())
            {
                System.out.println("TEST");
                System.out.println(resultSet.getString("STUID"));
                result.append(resultSet.getString("STUID"));
                result.append(" ");
                result.append(resultSet.getString("STUNAME"));
                result.append(" ");
                result.append(resultSet.getString("ADDRESS"));
                result.append(" ");
                result.append(resultSet.getString("POSTCODE"));
                result.append(" ");
                result.append(resultSet.getString("EMAIL"));
            }
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Return the built string with correct information.
    return this.result.toString();
}
}

If i input a STUID that isn't in the database, it successfully let's me know by returning "No Record For ID". However if i input one that is in the database, it prints the line "FOUND A RESULT" (just a test line), but never actually gets to the "TEST" output - and therefore never builds any string with the result.

I know it's not the database because i tested some code inside my thread class (before calling this contact class), and querying the same database works:

    Statement s = null;
    ResultSet rs = null;
    try{
        s = dbConnection.createStatement();
        rs = s.executeQuery("SELECT * FROM Contact");
        while(rs.next())
        {
            System.out.println(rs.getString("STUID"));
        }
    }catch(SQLException e)
    {
        e.printStackTrace();
    }

That test code works.

So i'm really confused as to why it can successfully query the database, and work out that there is no result (by using if(!resultSet.next() ), but if it works out that there is actually a result it can't manage to loop through to give me the details. Any help would be appreciated!

¿Fue útil?

Solución

This happens because you are moving past the first result in the resultset:

if(!resultSet.next())
{
    result.append("No record for ");
    result.append(this.student);
    result.append(".");
}
else // <-- failurehere
{

your else is implicitly calling resultSet.next() which will move past the first element. If you queried against something with two elements you would only get the second one returned.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top