對一個簡單的JDBC包裝器的擴展及應用(1)
發表于:2007-07-14來源:作者:點擊數:
標簽:
對一個簡單的JDBC包裝器的擴展及應用(1) 本文將對《一個簡單的 JDBC 包裝器》中的JDBC包裝器進行一些擴展,然后介紹一下其在jsp+ java bean 開發 模式中的應用。 最近看了《一個簡單的 JDBC 包裝器》,覺得這篇文章很有應用價值,我便在自己的開發中使用了
對一個簡單的JDBC包裝器的擴展及應用(1)
本文將對《一個簡單的 JDBC 包裝器》中的JDBC包裝器進行一些擴展,然后介紹一下其在jsp+
javabean
開發模式中的應用。
最近看了《一個簡單的 JDBC 包裝器》,覺得這篇文章很有應用價值,我便在自己的開發中使用了它,不過這個包裝器也存在一些不足,于是我對它進行了一些擴展。首先原文中的Table類缺少刪除功能,我便增加了刪除功能。代碼如下:
public void delRow(Row row) throws
SQLException {
String ss="";
ss = "delete from "+name+" where ";
for (int i=0; i<row.length(); ++i) {
String k = row.getKey( i );
String v = row.get( i );
ss += k+"=´"+v+"´";
if (i != row.length()-1)
ss += " and ";
}
Connection con = database.getConnection();
Statement st = con.createStatement();
st.executeUpdate( ss );
}
public void delRow(String conditions)throws SQLException {
String ss="";
ss = "delete from "+name+" where ";
ss +=conditions;
Connection con = database.getConnection();
Statement st = con.createStatement();
st.executeUpdate( ss );
}
這兩個函數分別用于刪除一個Row和滿足一定條件的記錄。對于具有主關鍵字的表,我們可以用下面代碼中的方法二來進行刪除,如果沒有主關鍵字,我們可以用方法一刪除。
示例如下:
//方法一
Row e = table.getRow( "id=2001" );
table.delRow(e);
//方法二
table.delRow("id=2001");
另外這個包裝器沒有對查詢結果為NULL的情況作處理,我通過修改Table類的execute函數和RowSet類的get函數對這種情況作了處理。具體代碼見附件。
下面談談利用這個JDBC包裝器實現對
數據庫的封裝,假定我們有一個表:student,創建表的Sql語句如下:
create table student(
id varchar(10) not null primary key,
name varchar(16) not null,
sex char(2) not null,
password varchar(16) not null,
department varchar(32) not null
)
我們對這個表進行封裝,下面是Student類的主要代碼:
public class Student{
private Row r;
public Student() {
r=new Row();
}
public Student(Row row) {
this.r=row;
}
private Table getTable() {
Database db =
new Database( "jdbc:
mysql://localhost:3306/manger",
"zf", "zf" );
return db.getTable("student");
}
public void setName(String name){
r.put("name",name);
}
public void setPassword(String pass){
r.put("password",pass);
}
public void setId(String number){
r.put("id",number);
}
public void setDepart(String depart){
r.put("department",depart);
}
public void setSex(String sex){
r.put("sex",sex);
}
public String getName(){
return r.get("name");
}
public String getPassword(){
return r.get("password");
}
public String getId(){
return r.get("id");
}
public String getDepart(){
return r.get("department");
}
public String getSex(){
return r.get("sex");
}
/**
*condition表示限制條件,如果為空,則插入新記錄,否則更新記錄
*/
public void save(String conditions) throws SQLException{
if(conditions==null)
{getTable().putRow(r);}
else
getTable().putRow(r,conditions);
}
/**
*由于id作為主關鍵字,所以我們使用字符串為參數的delRow()函數
*/
public void delete()throws SQLException{
//getTable().delRow(this.r);
String conditions="";
conditions = "id=" + "´"+getId()+"´";
getTable().delRow(conditions);
}
}
下面這個類是相應的一個查詢類的主要代碼:
public class StudentFactory{
public static Student findStudentById(String id)
throws SQLException{
Row r=getTable().getRow("id="+id);
if(r==null)
return null;
else
return new Student(r);
}
public static Student[] findAllStudents()
throws SQLException{
RowSet rs=getTable().getRows("1>0");
if (rs==null)
return null;
else
Student[] stu=null;
stu=new Student[rs.length()];
for(int i=0;i<rs.length(); i++){
stu[i]=new Student(rs.get(i));
}
return stu;
}
}
(未完,待續)
原文轉自:http://www.anti-gravitydesign.com