在SQL中使用preparedstatement在jsp中查找一行

时间:2017-08-30 17:05:43

标签: java sql jsp

寻找帮助尝试在SQL中的预备语句中查找给定行。 我有SQL工作。给我所需的所有信息,但当我尝试在JSP中调用它时,它只给我第一行。

如何拨打下一行?

我像这样调用第一行的信息:

<%=mails.getString("Col 1")%>
<%=mails.getString("Col 2")%>
<%=mails.getString("Col 3")%>
 ....
<%=mails.getString("Col n")%>

=====================

如果有人需要,

额外信息:

邮件是

的ResultSet
SELECT * FROM raw_purp_rec WHERE batchno=?;

输出:

           | Col 1 | Col 2 | Col 3|....| Col n |
    Row1       1        A       B     .     S
    Row2       2        Z       Z     .     Q
    Row3       3        E       M     .     L
                        ....
    Row7       7        W       E     .     X

我希望从第一行获取信息而不结束ResultSet。

Connection con = null;
PreparedStatement sendLogin = null;
ResultSet resultTest = null;
protected class Mail{
        public Mail(){
            try{
                con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            sendLogin = con.prepareStatement(
            "SELECT * "
            + "FROM raw_purp_rec "
            + "where raw_purp_rec.batchno= ?;");

            }catch(Exception e){
            e.printStackTrace();
            }
        }


        public ResultSet getMail(String thing){
            try{
                sendLogin.setString(1, thing);

                resultTest = sendLogin.executeQuery();
            }catch(Exception e){
                e.printStackTrace();
            }
            return resultTest;
        }

}

使用以下构造函数:

String packet = request.getParameter("name");
Mail mail = new Mail();
ResultSet mails = mail.getMail(packet);

2 个答案:

答案 0 :(得分:0)

我认为最好使用中间POJO,您将在结果集中读取,然后将其可视化,而不是尝试直接访问结果集。如果必须直接从JSP访问结果集,则需要使用Scriptlets这是一个糟糕的想法。

答案 1 :(得分:0)

您需要遍历行使用结果集接口对象

示例: -

 while (resultSet.next()) {
                System.out.println("Printing result...");

以下是您可以用作参考目的的示例代码。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ResultSetExample {

    public static void main(String[] args) {
        // The credentials that we need to have available for the connection to the database.
        String username = "myusername";
        String password = "mypassword";
        String databaseName = "albums";

        Connection connect = null;
        Statement statement = null;

        try {
            // Load the MySQL driver.
            Class.forName("com.mysql.jdbc.Driver");

            // Setup the connection to the database.
            // Take notice at the different variables that are needed here:
            //      1. The name of the database and its location (currently localhost)
            //      2. A valid username/password for the connection.
            connect = DriverManager.getConnection("jdbc:mysql://localhost/"
                    + databaseName + "?"
                    + "user=" + username
                    + "&password=" + password);

            // Create the statement to be used to get the results.
            statement = connect.createStatement();

            // Create a query to use.
            String query = "SELECT * FROM table ORDER BY year";

            // Execute the query and get the result set, which contains
            // all the results returned from the database.
            ResultSet resultSet = statement.executeQuery(query);

            // We loop through the rows that were returned, and we can access the information
          use the appropriate methods.
            while (resultSet.next()) {
                System.out.println("Printing result...");

                // Now we can fetch the data by column name, save and use them!
                String albumName = resultSet.getString("name");
                String artist = resultSet.getString("artist");
                int year = resultSet.getInt("year");

                System.out.println("\tAlbum: " + albumName + 
                        ", by Artist: " + artist + 
                        ", released in: " + year);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // We have to close the connection and release the resources used.
            // Closing the statement results in closing the resultSet as well.
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}