当前位置: 代码迷 >> Java Web开发 >> 连接数据库的javabean这样写有不妥吗
  详细解决方案

连接数据库的javabean这样写有不妥吗

热度:195   发布时间:2007-05-30 21:02:05.0
连接数据库的javabean这样写有不妥吗

package Data;
import java.sql.*;

public class database{
private Connection conn=null;
ResultSet rs=null;
private Statement stmt=null;
PreparedStatement pstmt=null;

public database(){}

public void OpenConn(String dsn,String uid,String pwd) throws Exception{
try{
dsn="jdbc:odbc:"+dsn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(dsn,uid,pwd);
}catch(Exception ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());}
}

public ResultSet executeQuery(String sql){
try{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){System.err.println(ex.getMessage());}
return rs;
}

public PreparedStatement executeUpdate(String sql){
try{
pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate();
}catch(SQLException ex){System.err.println(ex.getMessage());}
return pstmt;
}

public void closePstmt(){
try{pstmt.close();}
catch(SQLException ex){System.err.println(ex.getMessage());}
}

public void closeStmt(){
try{stmt.close();}
catch(SQLException ex){System.err.println(ex.getMessage());}
}

public void closeConn(){
try{conn.close();}
catch(SQLException ex){System.err.println("aq.executeQuery:"+ex.getMessage());}
}
}

感觉有冗余代码!或者在多次查询和多次更新的时候会出现错误?
请教了!

搜索更多相关主题的帖子: javabean  数据库  

----------------解决方案--------------------------------------------------------
回复:(guixiaolan)连接数据库的javabean这样写有不...

package Data;
import java.sql.*;

public class database{
private Connection conn=null;
ResultSet rs=null;
private Statement stmt=null;
PreparedStatement pstmt=null;

public database(){}

public void OpenConn(String dsn,String uid,String pwd) throws Exception{
try{
dsn="jdbc:odbc:"+dsn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(dsn,uid,pwd);
}catch(Exception ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());}
}

public ResultSet executeQuery(String sql){
try{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){System.err.println(ex.getMessage());}
return rs;
}

public PreparedStatement executeUpdate(String sql){
try{
pstmt=conn.prepareStatement(sql);
//这里参数不设了啊?下面3个close完全可以写到一个方法里,这样写没实际用处啊,
//要封装数据库操作请参考DAO模式
pstmt.executeUpdate();
}catch(SQLException ex){System.err.println(ex.getMessage());}
return pstmt;
}

public void closePstmt(){
try{pstmt.close();}
catch(SQLException ex){System.err.println(ex.getMessage());}
}

public void closeStmt(){
try{stmt.close();}
catch(SQLException ex){System.err.println(ex.getMessage());}
}

public void closeConn(){
try{conn.close();}
catch(SQLException ex){System.err.println("aq.executeQuery:"+ex.getMessage());}
}
}


----------------解决方案--------------------------------------------------------
建议LZ用用MVC模式!代码可能会多,但是可以提高代码的可重用性,也比较好做测试!先开始用用jsp+servlet+bean!
----------------解决方案--------------------------------------------------------
  相关解决方案