读书人

JAVABEAN中写数据库查询语句的有关问题

发布时间: 2011-12-25 23:21:20 作者: rapoo

JAVABEAN中写数据库查询语句的问题
环境access+tomcat
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111
access中查询语句:
***************
select * from products where category= 'computer '
或:
select * from products where category= "computer "
******************************
javabean中语句:
***************
ResultSet rst=stmt.executeQuery( "select * from Products where category= ' "+categoryId+ " ' ");
22222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222
access中查询语句:
***************
select * from products where name like '*1* ';

select * from products where name like "*1* ";
***************************
javabean中语句:
***************
ResultSet rst=stmt.executeQuery( "select * from Products where name like '% "+nameId+ "% ' ");


谁能给我解释下: '% "+nameId+ "% ' 这段文字。。。
*******************************************************
具体文件如下:
*********************
public class ProductBean
{
private Connection con;
//构造方法,获得数据库的连接。
public ProductBean(){
this.con=DataBaseConnection.getConnection();
}
/按照商品的类别查找商品,
public Collection getProductByCategory(String categoryId)throws Exception
{
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery( "select * from Products where category= ' "+categoryId+ " ' ");
Collection ret=new ArrayList();
while(rst.next())
{
Product temp=new Product();
temp.setProductId(rst.getString( "productid "));
temp.setCategoryId(rst.getString( "category "));
temp.setName(rst.getString( "name "));
temp.setDescription(rst.getString( "descn "));
temp.setProducer(rst.getString( "producer "));
temp.setPrice(rst.getString( "price "));

ret.add(temp);
}
con.close();
return ret;
}

//按名称匹配查询。
public Collection getProductByName(String nameId)throws Exception
{
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery( "select * from Products where name like '% "+nameId+ "% ' ");
Collection ret=new ArrayList();
while(rst.next())
{
Product temp=new Product();
temp.setProductId(rst.getString( "productid "));
temp.setCategoryId(rst.getString( "category "));
temp.setName(rst.getString( "name "));
temp.setDescription(rst.getString( "descn "));
temp.setProducer(rst.getString( "producer "));
temp.setPrice(rst.getString( "price "));

ret.add(temp);
}
con.close();
return ret;
}
}

*************************************************

[解决办法]
System.out.println( "select * from Products where name like '% "+nameId+ "% ' ");


就知道是什么意思了......
要不,简单点

System.out.println( "1月有 "+31+ "天 ");

然后转换下
int day = 31;
System.out.println( "1月有 "+day+ "天 ");

然后再转换下
public void printTest(int day)
{
System.out.println( "1月有 "+day+ "天 ");
}

读书人网 >Java Web开发

热点推荐