JSP Oracel JavaBean prepareStatement 条件查询无结果
//按某一关键字精确查询publicList<Phone>query1(Stringlei,Stringzhi)throwsException{//Listall=new...
//按某一关键字精确查询
public List<Phone> query1(String lei, String zhi) throws Exception{
//List all = new ArrayList() ;
List<Phone> all=new ArrayList<Phone>();
String sql = "SELECT * FROM phone where ? = ?" ;
// String sql = "SELECT * FROM phone where id = '102' " ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,lei);
pstmt.setString(2,"'"+zhi+"'");
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给person对象
Phone phone = new Phone() ;
phone.setId(rs.getInt(1)) ;
phone.setName(rs.getString(2)) ;
phone.setNumberGu(rs.getString(3)) ;
phone.setNumberYi(rs.getString(4)) ;
phone.setRemark(rs.getString(5)) ;
// 将查询出来的数据加入到List对象之中
all.add(phone) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.print(e);
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return all ;
}
//按某一关键字模糊查询
public List<Phone> query2(String lei, String zhi) throws Exception{
//List all = new ArrayList() ;
List<Phone> all=new ArrayList<Phone>();
String sql = "SELECT * FROM phone where ? like ?" ;
// String sql = "SELECT * FROM phone where name like '%大%' ";
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,lei);
pstmt.setString(2,"'%"+zhi+"%'");
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给Phone对象
Phone phone = new Phone() ;
phone.setId(rs.getInt(1)) ;
phone.setName(rs.getString(2)) ;
phone.setNumberGu(rs.getString(3)) ;
phone.setNumberYi(rs.getString(4)) ;
phone.setRemark(rs.getString(5)) ;
// 将查询出来的数据加入到List对象之中
all.add(phone) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.print(e);
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return all ;
}
这是关键的一段儿,我调试增、删、改、查询全部都没有问题,只有条件查询出了问题,应该就出在prepareStatement参数设定那块儿了,我换用简单的调试语句试过了,没问题,谁帮我看下?我积分不多了
数据库结构:
SQL> desc phone;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(32)
NAME VARCHAR2(20)
NUMBERGU CHAR(12)
NUMBERYI CHAR(11)
REMARK VARCHAR2(50) 展开
public List<Phone> query1(String lei, String zhi) throws Exception{
//List all = new ArrayList() ;
List<Phone> all=new ArrayList<Phone>();
String sql = "SELECT * FROM phone where ? = ?" ;
// String sql = "SELECT * FROM phone where id = '102' " ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,lei);
pstmt.setString(2,"'"+zhi+"'");
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给person对象
Phone phone = new Phone() ;
phone.setId(rs.getInt(1)) ;
phone.setName(rs.getString(2)) ;
phone.setNumberGu(rs.getString(3)) ;
phone.setNumberYi(rs.getString(4)) ;
phone.setRemark(rs.getString(5)) ;
// 将查询出来的数据加入到List对象之中
all.add(phone) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.print(e);
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return all ;
}
//按某一关键字模糊查询
public List<Phone> query2(String lei, String zhi) throws Exception{
//List all = new ArrayList() ;
List<Phone> all=new ArrayList<Phone>();
String sql = "SELECT * FROM phone where ? like ?" ;
// String sql = "SELECT * FROM phone where name like '%大%' ";
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
// 下面是针对数据库的具体操作
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,lei);
pstmt.setString(2,"'%"+zhi+"%'");
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给Phone对象
Phone phone = new Phone() ;
phone.setId(rs.getInt(1)) ;
phone.setName(rs.getString(2)) ;
phone.setNumberGu(rs.getString(3)) ;
phone.setNumberYi(rs.getString(4)) ;
phone.setRemark(rs.getString(5)) ;
// 将查询出来的数据加入到List对象之中
all.add(phone) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.print(e);
}
finally
{
// 关闭数据库连接
dbc.close() ;
}
return all ;
}
这是关键的一段儿,我调试增、删、改、查询全部都没有问题,只有条件查询出了问题,应该就出在prepareStatement参数设定那块儿了,我换用简单的调试语句试过了,没问题,谁帮我看下?我积分不多了
数据库结构:
SQL> desc phone;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(32)
NAME VARCHAR2(20)
NUMBERGU CHAR(12)
NUMBERYI CHAR(11)
REMARK VARCHAR2(50) 展开
2个回答
展开全部
String sql = "SELECT * FROM phone where ? like ?" ;
此句换成
String table_name = "";
String sql = "SELECT * FROM phone where "+table_name+" like ?" ;
pstmt.setString(1,lei);
pstmt.setString(2,"'%"+zhi+"%'");
换成:
pstmt.setString(1,"'%"+zhi+"%'");
此句换成
String table_name = "";
String sql = "SELECT * FROM phone where "+table_name+" like ?" ;
pstmt.setString(1,lei);
pstmt.setString(2,"'%"+zhi+"%'");
换成:
pstmt.setString(1,"'%"+zhi+"%'");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询