0

I am currently trying to read from a database and output the information to an html file. But I am having trouble reading from the database. Surely it is because of my lack of knowledge of java and database programming.

I am having no trouble connecting to the database and creating my query string. My query string is created from a series of checkboxes, so it is not the same every time. It also may contain strings and integers. That is where I feel my problem is. Since my query string is not the same everytime I don't know how to successfully output my data. When I execute my query is when I am having the problems. Here is a bit of my code.

public String getData( String query, StringBuffer back)
    {
        String query = query;
        ResultSet rs = null;

        try
        {
            rs = st.executeQuery(query);


            back.append( "<table border=\10\" >\n" );
            while(rs.next())
            {   
                back.append( "<tr><td>" + rs.getString(1) + "</td></tr>");
            }
            back.append( "</table>" );
        }
        catch( SQLException e )
        {
            back.append( "<h6>something bad is happening</h6>");
            e.printStackTrace();
            return null;
        }
        return new String( back );
    }

Any help would be great!

4
  • What is the query? Can you give an example? Commented Mar 9, 2011 at 21:31
  • SELECT Country, District, Population FROM City WHERE name ='Tulsa' Commented Mar 9, 2011 at 21:42
  • Population and Country are integers Commented Mar 9, 2011 at 21:42
  • IS this the actual code? Just asking because the code shouldn't compile as the parameter query and local variable query clashes. Have you tried to output the actual generated query before executing it? Have you copied the generates query and tested it in the database without using your application? Commented Mar 9, 2011 at 23:26

2 Answers 2

1

One problem is that you are missing a quote here:

back.append( "<table border=\10\" >\n" );

I think you probably meant this:

back.append( "<table border=\"10\" >\n" );

Another problem is that you seem to be using 0-based indexing for parameter to getString, but the first column is 1, not 0. So you probably mean this:

back.append( "<tr><td>" + rs.getString(2) + "</td></tr>");
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I missed that. But that isn't the reason why my programming isn't working.
The thing is since I am not printing to the console it doesn't display my errors. All I know is in my try catch statement my try is failing.
You need to get the error message. Are you using a logging framework? If so, log the error there. If all else fails, you could at least write the stacktrace to a file in a known location.
0

What is the error? No results? Exception? Which one?

As Mark Byers suggested, you can write it to the framework log. You can even simply write it down in the html. As it is debugging it does not matter much if it breaks the HTML, and if you see the source of the page you do not need to find which log file your framework uses :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.