2016-04-05 18 views
0

私は、クラスCreditCardDaoImplでJUnitテスト(CreditcardDAOTest)を実行しようとしています。私はエラー"java.sql.SQLException: No value specified for parameter 2"を得続けるが、私は2つのパラメータがあるとは思わない。 ご協力いただければ幸いです。私が信じているエラーは、"ps.setLong(1, customerID);"から来ています。私は"ps.setLong(x,x);"があるはずですが、私はそれが何のためになっているのかわかりません。私は、デバッグを試み、それが私の文の名前を入力する必要がありますように思える:SQL JDBCとJava JUnitテストに失敗しました

final static String retrieveForCustomerID = 
     "SELECT name = ?, cc_number = ?, exp_date = ?, security_code = ?, WHERE customerID = ?;"; 

が、再び私は全くわかりません。私はこれをしばらく把握しようとしており、単にガイダンスを探していることをご存知でしょうか。

//Method in CreditCardDAOImpl 
public CreditCard retrieveForCustomerID(Connection connection, Long customerID) throws SQLException, DAOException 
{ 
    if (customerID == null) 
    { 
     throw new DAOException("Trying to retrieve credit card with NULL customerID"); 
    } 

    PreparedStatement ps = null; 
    try 
    { 
     ps = connection.prepareStatement(retrieveForCustomerID); 
     ps.setLong(1, customerID); 
     ResultSet rs = ps.executeQuery(); 
     CreditCard cc = new CreditCard(); 
     cc.setName(rs.getString("name")); 
     cc.setCcNumber(rs.getString("cc_number")); 
     cc.setExpDate(rs.getString("exp_date")); 
     cc.setSecurityCode(rs.getString("security_code")); 
     return cc; 
    } 
    finally 
    { 
    } 
} 

//Method in CreditCardDAOTest 
@Test 
public void testRetrieveForCustomerID() throws Exception 
{ 
    DataSource ds = DataSourceManager.getDataSource(); 
    Connection connection = ds.getConnection(); 
    CreditCardDAO dao = new CreditCardDaoImpl(); 

    CreditCard ccard = dao.retrieveForCustomerID(connection, customerID); 
    assertNotNull(ccard); 
    assertNotNull(ccard.getCcNumber()); 
    assertNotNull(ccard.getExpDate()); 
    assertNotNull(ccard.getName()); 
    assertNotNull(ccard.getSecurityCode()); 

    connection.close(); 
} 
+0

(!rs.next()){ \t \t \tが返されますか? \t \t} – user3512367

+0

最初の変更は、以下のようにクエリを更新してから、結果セットをrs.'getXXX() 'と呼ぶ前にrs.next()を呼び出すことです。この[リンク](http://alvinalexander.com/blog/post/jdbc/jdbc-preparedstatement-select-like)は助けてください –

+0

それは素晴らしいです!ありがとう! – user3512367

答えて

0

使用rs.next()とあなたのクエリが

final static String retrieveForCustomerID = 
     "SELECT name, cc_number, exp_date, security_code from TABLE_NAME WHERE customerID = ?" 

NOTEのようになります。実際のテーブル名で上記のクエリにTABLE_NAMEを交換してください。

0
  1. クエリにテーブル名を指定し、selectブロックの疑問符を削除する必要があります。

    SELECT name, cc_number , exp_date, security_code FROM your_table_name WHERE customerID = ? 
    
  2. 事前にすべての詳細を送信する必要がありますか?プリペアドステートメントを実行した後

    ps.setInt(), ps.setLong(), ps.setString().... etc. 
    
  3. を使用して、結果セットを取得するクエリでは、あなたのresutsetを反復するために

    rs.next() 
    

を使用する必要があります。

関連する問題