2011-12-01 643 views
1

我正在測試將結果查詢的數據傳輸到一個字符串變量,但是當我做了我收到這一堆錯誤,我不知道爲什麼發生這種情況,我在MySQL中測試了我的查詢,它的工作,但我的代碼怎麼沒有?這裏是我的測試代碼JDBC模板PreparedStatementCallback錯誤幫助?

package test; 
import dao.FinanceDao; 
import javax.swing.JTextArea; 
import java.util.Scanner; 
import java.util.List; 
import javax.swing.JOptionPane; 
import domainmodel.User; 
import javax.sql.DataSource; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 

public class TestDrive { 

    public static void main(String[] args){ 
     String employeeID = null; 
     String password = null; 
     JTextArea text = new JTextArea(); 
     FinanceDao finance = new FinanceDao(); 
     DataSource dataSource = SourceObj.getSource(); 

     finance.setDataSource(dataSource); 

     /*Scanner input = new Scanner(System.in); 
     System.out.println("Enter your Choice for Employee ID"); 
     String empID = input.nextLine(); 
     System.out.println("Enter your choice for Password"); 
     String password = input.nextLine(); 
     finance.Add(empID,password); 
     */ 
     String idNumber = JOptionPane.showInputDialog("Enter Employee ID to Search"); 

     List<User> test = finance.select(idNumber,"51010"); 
     for(User u: test){ 
     employeeID = u.getEmpID(); 
     password = u.getPassword(); 
     if(employeeID == null){ 
      text.setText("No Result"); 
     }else{ 
      text.setText("\tEmployeeID: "+employeeID+"\n\tPassword: "+password); 
      } 
     } 

     UI U = new UI(text); 
    } 
} 

而這一次

package dao; 

import javax.sql.DataSource; 
import java.util.List; 
import org.springframework.jdbc.core.JdbcTemplate; 
import dao.mapper.UserRowMapper; 
import domainmodel.User; 

public class FinanceDao implements Manage { 

    private DataSource ds; 

    @Override 
    public void setDataSource(DataSource ds) { 
     this.ds = ds; 

    } 

    @Override 
    public void Add(String empID, String password) { 
     JdbcTemplate Add = new JdbcTemplate(ds); 
     Add.update("INSERT INTO user (empID,password) VALUES(?,?)", 
     new Object[] { empID, password }); 
    } 

    @Override 
    public void Delete(String empID , String password) { 
     JdbcTemplate Delete = new JdbcTemplate(ds); 
     Delete.update("Delete from User where emp_id = '?'",new Object[]{empID}); 
    } 


    public List<User> select(String empID,String password) { 
     JdbcTemplate select = new JdbcTemplate(ds); 
     return select 
       .query(
         "SELECT EMPID ,PASSWORD from USER where empID = '?' AND password = ?", 
         new Object[] {empID , password}, 
         new UserRowMapper()); 
    } 

} 

錯誤

編輯 ,這是我的錯誤

Dec 2, 2011 6:02:29 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName 
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver 
Dec 2, 2011 6:02:34 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 
Dec 2, 2011 6:02:34 AM org.springframework.jdbc.support.SQLErrorCodesFactory <init> 
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase] 
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [SELECT EMPID ,PASSWORD from USER where empID = '?' AND password = ?]; SQL state [S1009]; error code [0]; Parameter index out of range (2 > number of parameters, which is 1).; nested exception is java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1). 
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:120) 
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:276) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:553) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:587) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:616) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:624) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:656) 
    at dao.FinanceDao.select(FinanceDao.java:36) 
    at test.TestDrive.main(TestDrive.java:32) 
Caused by: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1). 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729) 
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3713) 
    at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4553) 
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:236) 
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:94) 
    at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:51) 
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:592) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:537) 
    ... 6 more 
+3

引號和錯誤是什麼? –

+0

更新它好看 – user962206

+1

請粘貼堆棧跟蹤。 – JBaruch

回答

1

在搜索方法你在哪裏將empID傳遞給查詢?

@Override 
    public List<User> Search(String empID) { 
     JdbcTemplate search = new JdbcTemplate(ds); 
     return search.query("Select empID from user where empID = '?'", 
     new UserRowMapper()); 
    } 

對我來說,看起來相當可疑。它不應該是這樣的

 return search.query("Select empID from user where empID = ?", new Object[]{empID}),new UserRowMapper()); 

編輯 ,你也需要刪除周圍的問號

+0

我再次更新了它,它正在工作,但它給我1的結果爲什麼? – user962206

+0

你正在通過它的empID選擇一個用戶,是不是一個結果的預期結果? –

+0

@ user962206你是什麼意思1的結果?你的意思是一個單一的記錄?這是MarkRotteveel指出的 –