有哪些高手做过将对数据库的所有操作封装到一个JAVABEAN中?提供有用建议的就给分,分可一直加……

win_sharp 2001-11-29 05:50:39
能否将所有对数据库的操作,SELECT、INSERT、UPDATE、DELETE等操作封装到JAVABEAN中?哪位高手做过,能给我提供一个大概的思路吗?
在实现基础操作的基础上,重点是组合查询以及条件的嵌套!
尤其是整个BEAN应该如何组织、为什么?有什么好处?
谢谢了先!我一定会给分的!!!!!!
...全文
209 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
szayang 2002-03-04
  • 打赏
  • 举报
回复
我觉得封装进去确实是件好事,不过难度大了点,如果有足够的能力,有时间的话,就封好了,这样安全可靠,也易维护。如果时间比较紧,就多写些代码吧,一个查询做一个bean.
netyao 2001-12-17
  • 打赏
  • 举报
回复
study
AiNet 2001-12-03
  • 打赏
  • 举报
回复
可以把一些常用的简单SQL进行方法的封装(或者封装成类)
这样你写起来也方便些
例如
select * from tableName where fieldName condition value
-> getResultSet("table name","field name","condition","value")

-> getRecord("table name","field name","condition")
-> getAll("tableName")
...

怎么弄看你

但必须要提供原始SQL语句的方法,因为毕竟不可能设计实现所有的SQL组合
最好是把有可能变化的部分弄出来成为一个被调用的类,这样修改要容易些.
xmvigour 2001-12-03
  • 打赏
  • 举报
回复
win_sharp() :"到底SQL语句的组装有无必要封装到BEAN中????? "
如果这样的话,你肯定要每个表或每一个特定的查询都要对应一个javabean,要不你肯定要传递一大堆的参数来设置,而你的本意是为了什么呢?而且你做出来的javabean让其它使用只要传进来sql就行了,现在都sql的人多的半死,而且这样也可以让调用者很清楚自己的sql要查询数据的结果是什么,到底是sql错还是你的程序有问题!SQL语句的组装若都封装到BEAN中,你是不是要把每一种可能存在的sql查询组合都考虑的,要是超出你预想的范围,那是不是你还要在修改你的程序?
win_sharp 2001-12-03
  • 打赏
  • 举报
回复
继续、继续!
到底SQL语句的组装有无必要封装到BEAN中?????
win_sharp 2001-12-03
  • 打赏
  • 举报
回复
xmvigour(微电):
你说的有道理,我想SQL语言是如此之简单,我想编程者学习我编写的BEAN的时间都足以学会它了,所以似乎没有封装的必要了。
其实我想到在BEAN中组装SQL语句实乃万不得以,因为我的查询条件本来就是由传进来的一大堆参数组成的,如果本来就一条SQL语句,我也没必要把它拆开呀!所以JSP页面上就充满一大堆的代码,其目的为何,就为了组合一条正确的SQL语句。
因此,我觉得 AiNet(人工智能) 的方法不失为一种好办法,鱼和熊掌兼得,何乐不为?你以为呢?
deadrock 2001-11-30
  • 打赏
  • 举报
回复
javabean的封装应以功能分,在jsp中是不能有数据操作的。
mengyou 2001-11-30
  • 打赏
  • 举报
回复
跟不上趟,只能当观众了
figo 2001-11-30
  • 打赏
  • 举报
回复
我有一个,与大家分享:


import java.sql.*;


/**
* Title: 数据库封装
* Description:
* Copyright: Copyright (c) 2001
* Company: MICHEAL
* @author luojia
* @version 1.0
*/

public class Mssql {

private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;

private String dbdriver = "org.gjt.mm.mysql.Driver";
private String dburl = "jdbc:mysql://127.0.0.1/stock";

/**
* 构造数据库的连接和访问类
*/
public Mssql() throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
stmt = conn.createStatement();
}
public Mssql(String sql) throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
this.prepareStatement(sql);
}
public Mssql(int resultSetType, int resultSetConcurrency) throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
/**
* 设置对应值
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException {
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException {
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException {
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException {
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
prepstmt.setFloat(index,value);
}


public void clearParameters()
throws SQLException
{
prepstmt.clearParameters();
}
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 关闭连接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
win_sharp 2001-11-30
  • 打赏
  • 举报
回复
哦?是么!
支持 w_zy(草上飞)观点的人多么?
请大家发表自己的看法,回答的就有分!
w_zy 2001-11-30
  • 打赏
  • 举报
回复
还是自己写吧,要自动拼起来很不爽,也容易出错
win_sharp 2001-11-30
  • 打赏
  • 举报
回复
当然是用SQL语句咯!不过我觉得不应该是整个SQL语句一起传入,而应该在程序里构造SQL语句(用传入的参数),因为很多时候的是根据传入的参数操作数据库,如果在BEAN外面组织SQL语句,简单的没问题,如果一涉及逻辑复杂的组合查询等,不是很容易出错吗?而且程序的可读性也不好!
xmvigour 2001-11-30
  • 打赏
  • 举报
回复
对一个查询,我觉得不用sql那你准备怎么进行查询?sql作为差数传进来,不是很省事吗?而且你可以把sql在你数据库先试一下,看看sql有没写错!
不过你能说一下,你构想之中,应该如何做了?
win_sharp 2001-11-30
  • 打赏
  • 举报
回复
楼上的例子非常好呀!谢谢了,我一定会给分的。不过,对SQL语句的组合大家都认为只能传入SQL语句吗?
还有吗?
mengyou 2001-11-29
  • 打赏
  • 举报
回复
我有一个书上的例子,对我这个初学者来说还可以。

package xbook.common ;

import java.lang.* ;
import java.sql.* ;
import javax.servlet.* ;
import xbook.common.* ;

public class SQLBridge {
private ConnPool connPool ;
private Connection conn ;
private ResultSet rs ;
private ResultSetMetaData rsmd ;
private Statement stmt ;
private String driverName ;
private String jdbcURL ;
private String username ;
private String password ;

// -------------------------------------- Constructor --------------------------------------
public SQLBridge() {
connPool=null ;
conn=null ;
rs=null ;
rsmd=null ;
stmt=null ;
}

// -----------------------------------------------------------------------------------------
private void clearResult() throws SQLException {
if( rs!=null ) rs.close() ;
rs=null ;
if( stmt!=null ) stmt.close() ;
stmt=null ;
rsmd=null ;
}


public void closeDB() throws SQLException {
clearResult() ;
if( connPool!=null ) {
connPool.returnConnection() ;
connPool=null ;
}
else {
if( conn==null )
throw new SQLException( "This connection has been closed already." ) ;
if( conn.isClosed() )
throw new SQLException( "This connection has been closed." ) ;
conn.close() ;
}
conn=null ;
}


public int execSQL( String sqlStmt )
throws SQLException {
if( conn==null || conn.isClosed() )
throw new SQLException( "This connection has not been established yet." ) ;
if( sqlStmt==null )
throw new SQLException( "SQL-statement is null." ) ;
clearResult() ;
conn.setAutoCommit( true ) ;
stmt=conn.createStatement() ;
if( sqlStmt.toUpperCase().startsWith( "SELECT" ) ) {
rs=stmt.executeQuery( sqlStmt ) ;
rsmd=rs.getMetaData() ;
return -1 ;
}
else {
int numRow=stmt.executeUpdate( sqlStmt ) ;
clearResult() ;
return numRow ;
}
}


public void execUpdate( String[] sqlStmts )
throws SQLException {
if( conn==null || conn.isClosed() )
throw new SQLException( "The connection has not been established yet." ) ;
if( sqlStmts==null || sqlStmts.length==0 )
throw new SQLException( "SQL-statement is null." ) ;
clearResult() ;
conn.setAutoCommit( false ) ;
try {
for( int i=0 ; i<sqlStmts.length ; i++ ) {
stmt=conn.createStatement() ;
stmt.executeUpdate( sqlStmts[i] ) ;
stmt.close() ;
}
conn.commit() ;
}
catch( SQLException ex ) {
conn.rollback() ;
throw ex ;
}
}


public int getColumnCount()
throws SQLException {
if( rsmd==null )
throw new SQLException( "ResultSet is null." ) ;
return rsmd.getColumnCount() ;
}


public String[] getColumnNames()
throws SQLException {
if( rsmd==null )
throw new SQLException( "ResultSet is null." ) ;
String[] columnNames=new String[ getColumnCount() ] ;
for( int i=1 ; i<=columnNames.length ; i++ )
columnNames[i-1]=rsmd.getColumnName( i ) ;
return columnNames ;
}


protected Object getField( int column, boolean convertToString )
throws SQLException {
if( rs==null || rsmd==null )
throw new SQLException( "ResultSet is null." ) ;

switch( rsmd.getColumnType( column ) ) {
case Types.BIGINT :
if( convertToString )
return String.valueOf( rs.getLong(column) ) ;
else
return new Long( rs.getLong(column) ) ;

case Types.BINARY :
if( convertToString )
return Byte.toString( rs.getByte(column) ) ;
else
return new Byte( rs.getByte(column) ) ;

case Types.BIT :
if( convertToString )
return String.valueOf( rs.getBoolean(column) ) ;
else
return new Boolean( rs.getBoolean(column) ) ;

case Types.CHAR :
return rs.getString(column) ;

case Types.DATE :
if( convertToString )
return ( rs.getDate(column) ).toString() ;
else
return rs.getDate(column) ;

case Types.DECIMAL :
if( convertToString )
return ( rs.getBigDecimal( column, rsmd.getScale(column) ) ).toString() ;
else
return rs.getBigDecimal( column, rsmd.getScale(column) );

case Types.DOUBLE :
if( convertToString )
return String.valueOf( rs.getDouble(column) ) ;
else
return new Double( rs.getDouble(column) ) ;

case Types.FLOAT :
if( convertToString )
return String.valueOf( rs.getDouble(column) ) ;
else
return new Float( rs.getDouble(column) ) ;

case Types.INTEGER :
if( convertToString )
return String.valueOf( rs.getInt(column) ) ;
else
return new Integer( rs.getInt(column) ) ;

case Types.LONGVARBINARY :
if( convertToString )
return ( rs.getBinaryStream(column) ).toString() ;
else
return rs.getBinaryStream(column) ;

case Types.LONGVARCHAR :
return rs.getString(column) ;

case Types.NULL :
if( convertToString )
return "NULL" ;
else
return null ;

case Types.NUMERIC :
if( convertToString )
return ( rs.getBigDecimal( column, rsmd.getScale(column) ) ).toString() ;
else
return rs.getBigDecimal( column, rsmd.getScale(column) ) ;

case Types.REAL :
if( convertToString )
return String.valueOf( rs.getFloat(column) ) ;
else
return new Float( rs.getFloat(column) ) ;

case Types.SMALLINT :
if( convertToString )
return String.valueOf( rs.getShort(column) ) ;
else
return new Short( rs.getShort(column) ) ;

case Types.TIME :
if( convertToString )
return ( rs.getTime(column) ).toString() ;
else
return rs.getTime(column) ;

case Types.TIMESTAMP :
if( convertToString )
return ( rs.getTimestamp(column) ).toString() ;
else
return rs.getTimestamp(column) ;

case Types.TINYINT :
if( convertToString )
return String.valueOf( rs.getByte(column) ) ;
else
return new Byte( rs.getByte(column) ) ;

case Types.VARBINARY :
if( convertToString )
return ( rs.getBytes(column) ).toString() ;
else
return rs.getBytes(column) ;

case Types.VARCHAR :
return rs.getString(column) ;

default :
if( convertToString )
return ( rs.getObject(column) ).toString() ;
else
return rs.getObject(column) ;
}
}


public Object getField( int column )
throws SQLException {
return getField( column, false ) ;
}


public Object getField( String fieldName )
throws SQLException {
return getField( rs.findColumn( fieldName ) ,false ) ;
}


public String getFieldString( int column )
throws SQLException {
return (String)getField( column, true ) ;
}


public String getFieldString( String fieldName )
throws SQLException {
return (String)getField( rs.findColumn( fieldName ) ,true ) ;
}


public boolean nextRow()
throws SQLException {
if( rs==null )
throw new SQLException( "ResultSet is null." ) ;
return rs.next() ;
}


public void openDB( String drvName ,String url ,
String uname ,String passwd )
throws SQLException {
if( conn!=null && !conn.isClosed() )
throw new SQLException( "The connection has been established already." ) ;
clearResult() ;
try {
Class.forName( drvName ) ;
}
catch( ClassNotFoundException ex ) {
throw new SQLException( ex.toString() ) ;
}
conn=DriverManager.getConnection( url ,uname ,passwd ) ;
}


public void openDB( ConnPool pool )
throws SQLException {
if( conn!=null && !conn.isClosed() )
throw new SQLException( "The connection has been established already." ) ;
if( pool==null )
throw new SQLException( "The connection pool cannot be found." ) ;
clearResult() ;
connPool=pool ;
conn=connPool.getConnection() ;
}


public void setConnectionSwitch( String on_off )
throws ServletException {
try {
if( on_off.equalsIgnoreCase( "ON" ) ) {
if( connPool==null )
openDB( driverName, jdbcURL, username, password ) ;
else
openDB( connPool ) ;
}
else if( on_off.equalsIgnoreCase( "OFF" ) )
closeDB() ;
}
catch( SQLException ex ) {
throw new ServletException( ex.toString() ) ;
}
}


public void setConnPool( ConnPool pool ) {
connPool=pool ;
}


public void setDriverName( String drvName ) {
driverName=drvName ;
}


public void setJdbcURL( String url ) {
jdbcURL=url ;
}


public void setUserName( String uname ) {
username=uname ;
}


public void setPassword( String passwd ) {
password=passwd ;
}
}
dearxiaobao 2001-11-29
  • 打赏
  • 举报
回复
我就写过一个,不过感觉不是很好,不知哪位有比较好的处理方法吗?
win_sharp 2001-11-29
  • 打赏
  • 举报
回复
真的么?没人这么做?
但我以前见过用ASP+COM的开发把SQL语句组合起来的!
win_sharp 2001-11-29
  • 打赏
  • 举报
回复
以SQL语句做参数似乎不是个好方法吧!不但程序逻辑性较差,而且构造SQL语句的代码就够你写,尤其是组合查询!条件可都是外面来的参数哦!
其他人还有吗?
scud 2001-11-29
  • 打赏
  • 举报
回复
可以呀

不过我觉得最好不要让他自动组合sql,简单的也还可以

复杂的sql语句:如果组合的话,往往效率非常低
xmvigour 2001-11-29
  • 打赏
  • 举报
回复
我写过一个bean,SELECT、INSERT、UPDATE、DELETE
这样做我觉得我自己对数据库的操作是,只要给它个sql就可以了,省得还要每次都写一大堆的代码。主要是我写程序比较懒,能省就省。哈哈……
当然你可以建个数据库连接池,这样会减少连接数据库的时间。

81,122

社区成员

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

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