当前位置: 技术问答>java相关
请高手们指教:我写的一个Dataaccess Bean,专门用来处理数据库操作,不知道行不行?
来源: 互联网 发布时间:2015-08-26
本文导语: 我想把这个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;//事务处理中...
我想把这个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
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