回复:(hanshengchua)我想问大家一个问题:用struts...
2:我看别人写的还有一种方法:(代码贴出)
package com.wnkj.dbutil;
import java.util.*;
import java.sql.*;
import com.wnkj.propertites.*;
import com.wnkj.pubclass.*;
public class Connect extends WriteLog
{
public Connection con=null;
public PreparedStatement pst=null;
public ResultSet rs=null;
private ArrayList celList=null;
private ArrayList rowList=null;
private int count=0;
public Connect()
{
try
{
String strCon="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String strUrl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=NPK2007";
/*
GetConnect.GetConnection();
Class.forName(GetConnect.ClassConnection);
con=java.sql.DriverManager.getConnection(GetConnect.ConnectionUrl,GetConnect.uid,GetConnect.pwd);
*/
Class.forName(strCon);
con=java.sql.DriverManager.getConnection(strUrl,"npk2007","npk2007");
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex,this.toString());
}
}
/**
* 查询信息
*/
public ArrayList select()
{
try
{
rowList=new ArrayList();
rs=pst.executeQuery();
while(rs.next())
{
celList=new ArrayList();
for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
{
celList.add(rs.getString(i)+"");
}
rowList.add(celList);
}
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex, this.toString());
} finally {
try {
if (rs != null)
rs.close();
if (pst != null)
pst.close();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Write(e, this.toString());
}
}
return rowList;
}
/**
* 查询信息记录数
* @return
*/
public int selectCount()
{
try
{
rowList=new ArrayList();
rs=pst.executeQuery();
if(rs.next())
{
count=rs.getInt(1);
}
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex, this.toString());
} finally {
try {
if (rs != null)
rs.close();
if (pst != null)
pst.close();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Write(e, this.toString());
}
}
return count;
}
/**
* 添加一条新的信息
* @return
*/
public int insert()
{
try
{
count=this.pst.executeUpdate();
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex,this.toString());
} finally {
try {
if (pst != null)
pst.close();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Write(e, this.toString());
}
}
return count;
}
/**
* 更新表中的数据
* @return
*/
public int update()
{
try
{
count=this.pst.executeUpdate();
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex,this.toString());
} finally {
try {
if (pst != null)
pst.close();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Write(e, this.toString());
}
}
return count;
}
/**
* 删除表中的数据
* @return
*/
public int delete()
{
try
{
count=this.pst.executeUpdate();
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex,this.toString());
} finally {
try {
if (pst != null)
pst.close();
close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Write(e, this.toString());
}
}
return count;
}
/**
* 关闭数据库连接
*
*/
public void close()
{
try
{
if(this.con!=null)
{
con.close();
}
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex,this.toString());
}
}
}
可以看出,这个属于数据库相关操作的类。另外相应的具体操作数据库还有一个类,贴了:
package com.wnkj.dbutil;
import java.util.*;
public class DB_GuanLiMember extends Connect {
private String sql = "";
/**
* 添加一个新的职员
*
* @return
*/
public int insertinfor(String guanlinum, String memnum, String memname,
String deptnum, String guantime, String note) {
try {
sql = "insert into guanlimember(guanlinum,memnum,memname,deptnum, guantime,note) values(?,?,?,?,?,?)";
this.pst = this.con.prepareStatement(sql);
this.pst.setString(1, guanlinum);
this.pst.setString(2, memnum);
this.pst.setString(3, memname);
this.pst.setString(4, deptnum);
this.pst.setString(5, guantime);
this.pst.setString(6, note);
} catch (Exception ex) {
ex.printStackTrace();
Write(ex, this.toString());
}
return this.insert();
}
public int updateById(String guanlinum, String memnum, String memname,
String deptnum,String guantime, String note,
int id) {
try {
sql = "update guanlimember set guanlinum=?,memnum=?,memname=?,deptnum=?,guantime=?,note=? where id=?";
this.pst = this.con.prepareStatement(sql);
this.pst.setString(1, guanlinum);
this.pst.setString(2, memnum);
this.pst.setString(3, memname);
this.pst.setString(4, deptnum);
this.pst.setString(5, guantime);
this.pst.setString(6, note);
this.pst.setInt(7, id);
} catch (Exception ex) {
ex.printStackTrace();
Write(ex, this.toString());
}
return this.update();
}
/**
* 根据id删除某条记录信息
* @param id
* @return
*/
public int deleteinfor(int id)
{
try
{
sql="delete from guanlimember where id=?";
this.pst=this.con.prepareStatement(sql);
this.pst.setInt(1, id);
}catch(Exception ex)
{
ex.printStackTrace();
Write(ex,this.toString());
}
return this.delete();
}
/**
* 查询某一个会员的id信息
*
* @param deptnum
* @return
*/
public ArrayList selectById(int id) {
try {
sql = "select * from guanlimember where id=?";
this.pst = this.con.prepareStatement(sql);
this.pst.setInt(1, id);
} catch (Exception ex) {
ex.printStackTrace();
Write(ex, this.toString());
}
return this.select();
}
/**
* 查询某一个部门的会员信息
*
* @param deptnum
* @return
*/
public ArrayList selectByDeptNum(String deptnum) {
try {
sql = "select * from guanlimember where deptnum=?";
this.pst = this.con.prepareStatement(sql);
this.pst.setString(1, deptnum);
} catch (Exception ex) {
ex.printStackTrace();
Write(ex, this.toString());
}
return this.select();
}
/**
* 查询管理表中的所有数据信息
*
* @return
*/
public ArrayList selectAll() {
try {
sql = "select guanlimember.id,guanlinum,memnum,memname,jigou.deptnum,jigou.deptname,guantime,guanlimember.note from guanlimember,jigou where guanlimember.deptnum=jigou.deptnum";
this.pst = this.con.prepareStatement(sql);
} catch (Exception ex) {
ex.printStackTrace();
Write(ex, this.toString());
}
return this.select();
}
}
最后是相应的action类:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.wnkj.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.wnkj.dbutil.DB_GuanLiMember;
import com.wnkj.dbutil.DB_ZhangQi;
import com.wnkj.formbean.AddZhiYuanForm;
/**
* MyEclipse Struts Creation date: 05-14-2007
*
* XDoclet definition:
*
* @struts.action path="/addZhiyuan" name="addZhiyuanForm"
* input="/form/addZhiyuan.jsp" scope="request" validate="true"
*/
public class AddZhiYuanAction extends Action {
/*
* Generated Methods
*/
public static int flag = 0;
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
AddZhiYuanForm addZhiYuanForm = (AddZhiYuanForm) form;
String guanlinum = addZhiYuanForm.getGuanlinum();
String memnum = addZhiYuanForm.getMemnum();
String memname = addZhiYuanForm.getMemname();
String deptnum = addZhiYuanForm.getDeptnum();
String guantime = addZhiYuanForm.getGuantime();
String note = addZhiYuanForm.getNote();
int row = new DB_GuanLiMember().insertinfor(guanlinum, memnum, memname,
deptnum, guantime, note);
if (row == 1) {
flag = 1;
} else {
flag = -1;
}
response.sendRedirect(request.getContextPath()+ "/nongzi/addZhiYuan.jsp");
return null;
}
}
贴码完毕,请问:在struts开发中用哪一个模式比较好?或者还有其他更优秀的做法,请大家多多指点。
以上两个代码中,我有两个地方不是很理解:
1:使用datasource配置时候,每个action都要打开-操作-关闭数据库,很繁琐。
2:在第二类代码中,已经写好try catch finally 之后是不是就不用在相关子类或者action中调用关闭方法了?
敬请高人指点,谢谢!!