public class databaseBean
{
//這是預設的資料庫連接方式
private String DBLocation="jdbc:mysql://localhost/onestoptech?user=root&password=password&useUnicode=true&characterEncoding=GB2312";
private String DBDriver="org.gjt.mm.mysql.Driver";
private Connection conn=null;
public databaseBean(){}
//通過set方法可以靈活設定資料庫的串連
public void setDBLocation(String location){DBLocation=location;}
public void setDBDriver(String driver){DBDriver=driver;}
public void setconn(Connection conn){this.conn=conn;}
public String getDBLocation(){return(DBLocation);}
public String getDBDriver(){return(DBDriver);}
public Connection getconn(){return(conn);}
/*public String DBConnect(){}
public String DBDisconnect(){}
public ResultSet query(String sql){}
public int getTotalPage(String sql,int pageSize){}
public ResultSet getPagedRs(String sql,int pageSize,int pageNumber){}
public String execute_sql(String sql){}*/
//通過傳入sql語句來返回一個結果集
public ResultSet query(String sql) throws SQLException,Exception
{
ResultSet rs=null;
if (conn==null)
{
DBConnect();
}
if (conn==null)
{
rs=null;
}
else
{
try
{
Statement s=conn.createStatement();
rs=s.executeQuery(sql);
}
catch(SQLException e){throw new SQLException("Cound not execute query.");}
catch(Exception e){throw new Exception("Cound not execute query.");}
}//then end of if
return(rs);
}//then end of the function executeQuery
//通過傳入sql語句和pageSize(每頁所顯示的結果數目)計算並返回總共的頁數
public int getTotalPage(String sql,int pageSize)
{
ResultSet rs=null;
int totalRows=0;
if (conn==null)
{
DBConnect();
}
if (conn==null)
{
rs=null;
}
else
try
{
Statement s=conn.createStatement();
if(!Exc.equals("Success!"))
{
//basicDB.DBDisconnect();
throw new Exception(Exc);
}
int pageSize=10; //定義每頁顯示的資料條數
int currentPage=1; //當前頁(第一次顯示的肯定是第一頁啦!~),以後的“當前頁”由下面出現的頁面中的pages參數傳入
int allPage=-1;
String pages=request.getParameter("pages");//取得頁面中pages參數,此參數代表的頁面的就是要顯示的“當前頁面”