开发者

Java - Trouble Reading from SQL database

开发者 https://www.devze.com 2023-02-15 17:21 出处:网络
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 da

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!


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>");


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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消