2017-08-30 6 views
0

SQLのpreparedstatementで特定の行を検索しようとしています。 私はSQLが働いています。必要なすべての情報を私に与えますが、JSPで呼び出すときには最初の行しか表示されません。SQLでpreparedstatementを使用してjspで行を検索

次の行はどのように呼び出すことができますか?

私はこのような最初の行の情報を呼び出します。

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

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

余分な情報ならば誰もがそれを必要とする:

メールを

SELECT * FROM raw_purp_rec WHERE batchno=?; 

出力のResultSetのです

  | 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を終了することなく、最初の行以上の情報を取得したいと考えています。私はそれはあなたが結果セットを読み込み、その後、直接結果セットにアクセスしようとするのではなく、それを視覚化します中間POJOを使用するのが最善だと思います

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 /利益で 'ResultSet'を使用して停止してください –

+0

また見てくださいhttps:// stackoverflow .com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq = 1 –

答えて

0

:のコンストラクタを使用して

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

} 

。 JSPから直接結果セットにアクセスする必要がある場合は、スクリプトレットを使用する必要があります。これはひどい考えです。

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(); 
      } 
     } 
    } 
} 
関連する問題