日期:2014-05-19  浏览次数:20646 次

连接oracle出问题,求解答
public class UseDb {

public List<Seach> queryByEName(String eName){
List<Seach> seachs = new ArrayList<Seach>();
Seach seach=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott",
"tiger");
String sql = "SELECT * FROM EMP WHERE ENAME LIKE '?%?'";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, eName);
ResultSet rs = pstmt.executeQuery();
for(;;){
if(rs.next()) break;
seach = new Seach();
seach.setEmpNo(rs.getInt("empNo"));
seachs.add(seach);
//System.out.println("测试…………………………………………………………………………………………………………………………………………………………………………"+rs.getInt("empNo"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return seachs;
}

}


错误提示:
java.sql.SQLException: ORA-01006: 绑定变量不存在
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:573)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1891)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:830)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2391)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2672)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)

------解决方案--------------------
恐怕你这个SQL和设置值不能这么写,你这种写法,问号根本是字符串中的一个正常字符,不是参数。

要修改下:
String sql = "SELECT * FROM EMP WHERE ENAME LIKE ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + eName + "%");


------解决方案--------------------
将你的这段代码:

Java code

for(;;){
if(rs.next()) break;
seach = new Seach();
seach.setEmpNo(rs.getInt("empNo"));
seachs.add(seach);

}

------解决方案--------------------
SQL 语句就有问题,2个问号。一个赋值