2017-08-30 44 views
0

尋找幫助,試圖在SQL中準備語句中查找給定行。 我有SQL工作。給我所需要的所有信息,但是當我嘗試在JSP中調用它時,它只給了我第一行。在jsp中查找一行,並在SQL中準備語句

我該怎麼稱呼下一行?

我稱之爲第一排的是這樣的信息:

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

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

額外的信息,如果有人需要它:

郵件是的結果集

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); 
+0

你知道'ResultSet'是什麼嗎?我想你應該1 /讀取https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html 2 /停止在某些JSP 3/Profit – 2017-08-30 17:18:53

+0

中使用「ResultSet」另請參閱https:// stackoverflow .COM /問題/ 3177733 /如何對避免-java的代碼中,JSP的文件嗎?RQ = 1 – 2017-08-30 17:22:19

回答

0

我認爲這是最好使用一箇中間POJO在那裏你會在結果讀出,然後進行可視化,而不是試圖直接訪問結果集。如果您必須直接從JSP訪問結果集,則需要使用Scriptlets,這是一個糟糕的主意。

0

您需要通過使用結果集接口對象

實例行重述: -

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

下面是whihc可用於參考目的的示例代碼。

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(); 
      } 
     } 
    } 
}