日期:2014-05-18  浏览次数:20768 次

JSP中调用的一个查询方法,不知道哪里有错,帮忙看看啊
public IClr findByLsh(String lsh) throws SysException
{
//在findByLsh中
Connection conn = null;
DBConnection db = new DBConnection("ce.dbcon.dbConfig");
PreparedStatement pstmt = null;

IClr clr = null;

try
{
conn = db.getConnection("ce.dbcon.dbConfig");
if(conn != null)
{
String sql = "select * from PUBLIC_CLR_TB where lsh=? and id=(select max(id) from PUBLIC_CLR_TB where lsh=?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(3, lsh);//对应问号位置参数的
ResultSet rs = pstmt.executeQuery();

while(rs.next())
{
clr = new ClrBean();
clr.setGh(rs.getString(2));
clr.setLsh(rs.getString(3));
}
rs.close();
}
} catch (SQLException e)
{
e.getMessage();
throw new SysException(e.getMessage());
}finally{
if(pstmt != null)
try
{
pstmt.close();
} catch (SQLException e)
{
e.getMessage();
}
db.closeConnection();
}
//退出findByLsh方法
return clr;
}

------解决方案--------------------
String sql = "select * from PUBLIC_CLR_TB where lsh=? and id=(select max(id) from PUBLIC_CLR_TB where lsh=?)"; 
pstmt = conn.prepareStatement(sql); 
pstmt.setString(3, lsh);//对应问号位置参数的 

两个问号3哪来的
------解决方案--------------------
Invalid parameter binding(s). 
无效的参数帮顶。

------解决方案--------------------
String sql = "select * from PUBLIC_CLR_TB where lsh=? and id=(select max(id) from PUBLIC_CLR_TB where lsh=?)"; 
pstmt = conn.prepareStatement(sql); 
pstmt.setString(3, lsh);//对应问号位置参数的 
ResultSet rs = pstmt.executeQuery(); 

sql中的的"?"和pstmt.setString(index,lsh);中的index对应
从1开始