作者longlyheart (longlyheart)
看板java
标题Re: [JSP ] 请问如何将搜寻与javaBean共同使用??
时间Mon Apr 2 20:25:50 2007
: 我是瞎猜的,不过我想唯一的可能性应该也只有这个。
: 你的SQL中有一个叫 dbname的 parameter,
: 你的JavaBean应该写成一个method去接收它。
: 比方说
: public String doSearch(String dbName){
: StringBuffer output=new StringBuffer();
: /* do connection */
: String comSQL = "SELECT * FROM articledb WHERE articlename
: like'%"+dbname+"
: while( /* expression */ ){
: /* do something */
: output.append( /* HTML tag with data */);
: }
: ..............
: return output.toString();
: }
: 如果说只会接收到最後一个,这句话带来的讯息是资料库的查询有正确运作,
: 你的除错方向就可以朝资料来源和SQL叙述去下手。
以下是小弟的Bean档案,因为之前不能用的被我丢掉了(爆炸)
所以花了一些时间把他从我的脑袋里挖出来(拖走)
package com.BuyPlus.Production;
import java.sql.*;
public class connectBean{
//设定资料库参数
private String driver="com.mysql.jdbc.Driver";
private String userName="帐号";
private String password="密码";
private String url="jdbc:mysql://localhost:3306/资料库名称";
//宣告资料库物件
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
//宣告SQL字串
private String comSQL = null;
//宣告暂存变数
private String error = null;
private int atSn;
private String atName = null;
private String atSize = null;
private int atPrice;
private String atPicture = null;
private String atNotice = null;
private String atClass = null;
private String SQLString = null;
//建立连线
public connectBean(){
try{
//载入JDBC驱动程式
Class.forName(driver);
//取得与资料库的连线
conn = DriverManager.getConnection(url,userName,password);
}
catch(SQLException se){
error = se.toString();
}
catch(Exception e){
error = e.toString();
}
}
//资料库控制
public void setdbControl(String dbname){
try{
//建立Statement物件
stmt = conn.createStatement();
//执行查询
comSQL = "SELECT * FROM articledb WHERE articlesn like'%"+dbname+"%';";
rs = stmt.executeQuery(comSQL);
//读取资料
while(rs.next()){
atSn = rs.getInt("articlesn");
atName = rs.getString("articlename");
atSize = rs.getString("size");
atPrice = rs.getInt("price");
atPicture = rs.getString("picture");
atNotice = rs.getString("notice");
atClass = rs.getString("class");
}
//关闭ResultSet物件
rs.close();
//关闭Statement物件
stmt.close();
}
catch(SQLException se){
error = se.toString();
}
catch(Exception e){
error = e.toString();
}
}
//中断Connection方法
public void getconnOff(){
try{
//关闭Connection物件
conn.close();
}
catch(SQLException se){
error = se.toString();
}
}
//回传参数
public int getatSn(){
return atSn;
}
public String getatName(){
return atName;
}
public String getatSize(){
return atSize;
}
public int getatPrice(){
return atPrice;
}
public String getatPicture(){
return atPicture;
}
public String getatNotice(){
return atNotice;
}
public String getatClass(){
return atClass;
}
}
之前的PO文如果引起大大们的不悦,在此致上十二万分的歉意,请原谅..(磕头)
这只Bean我不知道要怎麽改才能让使用的网页收到全部的查询结果,请大大指教
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.120.96.194