请高手们指教:我写的一个Dataaccess Bean,专门用来处理数据库操作,不知道行不行?

yadg 2002-09-07 12:04:19
我想把这个Bean作为一个application对象,在每个jsp中用,不知道行不行。
import java.util.Vector;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.text.*;
public class Dataaccess{
private Connection con=null;//事务处理中用的conn
private Statement stat=null;//事务处理中用的stat
private boolean hastrans=false;//是否有事务处理
private Context ctx=null;
private DataSource ds=null;

private String DATABASENAME="BlankWarrantManage";
private String S_URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName="+DATABASENAME+";user=sa";
private String S_URLMASTER="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=master;user=sa";
private String S_DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String BACKUP_DIR="e:\\resin-2.1.0\\doc\\backup";
//初始化时,给ds赋值。
//建立连接池的连接和事务处理中的连接。
public Dataaccess(){
try{
Class.forName(S_DRIVER);
}catch(ClassNotFoundException e){ System.out.print("error at add driver"+e.getMessage());
}//end of try
try{
con=DriverManager.getConnection(S_URL);
stat=con.createStatement();

}catch(SQLException e){ System.out.print("error at add driver"+e.getMessage());
}//end of try
try{
ctx = (Context)new InitialContext().lookup("java:comp/env");
ds =(DataSource)ctx.lookup("jdbc/BlankWarrantManage");
}catch(NamingException e){
System.out.print("databaseWork() naming:"+e.getMessage());
}//end of try
}

private Connection createConx(){
try{
return ds.getConnection();
}catch(SQLException e){
System.out.println("databaseWork() sql错误信息:"+e.getMessage());
return null;
}
}
private Statement createStat(Connection conx){
try{
return conx.createStatement();
}catch(SQLException e){
System.out.println("databaseWork() sql错误信息:"+e.getMessage());
return null;
}
}
public synchronized Vector executeQuery(String sql){
Connection conx=createConx();
Statement statx=createStat(conx);
ResultSet res=null;
Vector rows = new Vector();
ResultSetMetaData metaData;
try {
if(hastrans)//有事务处理,用conn执行查询。
res=stat.executeQuery(sql);
else
res = statx.executeQuery(sql);
metaData = res.getMetaData();
rows = new Vector();
int numberOfColumns = metaData.getColumnCount();
Vector columnNames=new Vector();
for(int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column+1));
}
rows.addElement(columnNames);
while (res.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
newRow.addElement(res.getObject(i));
}
rows.addElement(newRow);
}
res.close();
statx.close();
conx.close();
}
catch (SQLException ex) {
System.err.println(ex);
}
return rows;
}
//执行查询,返回若干条记录。
public synchronized Vector executeQuery(String sql,int start,int length){
Connection conx=createConx();
Statement statx=createStat(conx);
ResultSet res=null;
Vector rows = new Vector();
ResultSetMetaData metaData;
try {
if(hastrans)//有事务处理,用conn执行查询。
res=stat.executeQuery(sql);
else
res = statx.executeQuery(sql);
metaData = res.getMetaData();
rows = new Vector();
int numberOfColumns = metaData.getColumnCount();
Vector columnNames=new Vector();
for(int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column+1));
}
rows.addElement(columnNames);
for(int i=1;i<start;i++) res.next();
while (res.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
newRow.addElement(res.getObject(i));
}
rows.addElement(newRow);
length--;
if(length==0) break;
}
res.close();
statx.close();
conx.close();
}
catch (SQLException ex) {
System.err.println(ex);
}
return rows;
}

/////////////////////////////////////////////////////////////
/*
事务处理部分。
*/
/////////////////////////////////////////////////////////////
public void begin() {
//数据的事务开始
try{
con.setAutoCommit(false);
hastrans=true;
}catch(SQLException e){
System.out.print("error:"+e.getMessage());
}
}
public void commit() {
//数据的事务结束
try{
con.commit();
con.setAutoCommit(true);
hastrans=false;
}catch(SQLException e){
System.out.print("error:"+e.getMessage());
}
}

public void rollback() {
//数据的事务回滚
try{
con.rollback();
con.setAutoCommit(true);
hastrans=false;
}catch(SQLException e){
System.out.print("error:"+e.getMessage());
}
}

public synchronized boolean executeUpdate(String sql){
Connection conx=createConx();
Statement statx=createStat(conx);
int retcount=0;
try{
if(hastrans)//有事务处理,用conn执行查询。
res=stat.executeUpdate(sql);
else
res = statx.executeUpdate(sql);
}catch(SQLException e){System.out.println(e.getMessage());}
stat.close();
conx.close();
if(retcount>0) return true;
else return false;
}

public synchronized boolean backup(){
try{
String sql="BACKUP DATABASE blankwarrantmanage TO DISK = '"+BACKUP_DIR+"\\"+new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date())+".bak'";
stat.executeUpdate(sql);
return true;
}catch(SQLException e){
System.out.print("backup error:"+e.getMessage());
return false;
}
}
public synchronized boolean restore(String filename){
try{
con=DriverManager.getConnection(S_URLMASTER);
stat=con.createStatement();
//得到con连接的进程。
ResultSet res=stat.executeQuery("select @@spid");
String spidself="";
if(res.next())
spidself=res.getString(1);
//得到所有进程
res=stat.executeQuery("sp_who");
String spid[]=new String[100];
int i=0;
while(res.next())
{
spid[i]=res.getString(1);
String tmpdbname=res.getString("dbname");
if(tmpdbname==null)
continue;
//不为空,且不等于自身的spid,且是操作该数据库的spid
if(!spidself.equals("")&&tmpdbname.equalsIgnoreCase(DATABASENAME)&&!spid[i].equals(spidself))
{
i++;
}
}
for(int j=0;j<i;j++)
stat.executeUpdate("kill "+spid[j]);
String sql="restore DATABASE blankwarrantmanage from DISK = '"+BACKUP_DIR+"\\"+filename+"'";
stat.executeUpdate(sql);
con=DriverManager.getConnection(S_URL);
stat=con.createStatement();
return true;
}catch(SQLException e){
System.out.print("restore error:"+e.getMessage());
return false;
}
}

}
...全文
17 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
付一笑 2002-09-07
  • 打赏
  • 举报
回复
行的呀。你用了有什么问题吗?
zhu_liping 2002-09-07
  • 打赏
  • 举报
回复
好主意呀

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧