我写过一个bean,SELECT、INSERT、UPDATE、DELETE
这样做我觉得我自己对数据库的操作是,只要给它个sql就可以了,省得还要每次都写一大堆的代码。主要是我写程序比较懒,能省就省。哈哈……
当然你可以建个数据库连接池,这样会减少连接数据库的时间。
这样做我觉得我自己对数据库的操作是,只要给它个sql就可以了,省得还要每次都写一大堆的代码。主要是我写程序比较懒,能省就省。哈哈……
当然你可以建个数据库连接池,这样会减少连接数据库的时间。
其他人还有吗?
但我以前见过用ASP+COM的开发把SQL语句组合起来的!
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 ;
}
}
还有吗?
不过你能说一下,你构想之中,应该如何做了?
支持 w_zy(草上飞)观点的人多么?
请大家发表自己的看法,回答的就有分!
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;
}
}
到底SQL语句的组装有无必要封装到BEAN中?????
如果这样的话,你肯定要每个表或每一个特定的查询都要对应一个javabean,要不你肯定要传递一大堆的参数来设置,而你的本意是为了什么呢?而且你做出来的javabean让其它使用只要传进来sql就行了,现在都sql的人多的半死,而且这样也可以让调用者很清楚自己的sql要查询数据的结果是什么,到底是sql错还是你的程序有问题!SQL语句的组装若都封装到BEAN中,你是不是要把每一种可能存在的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组合
最好是把有可能变化的部分弄出来成为一个被调用的类,这样修改要容易些.
你说的有道理,我想SQL语言是如此之简单,我想编程者学习我编写的BEAN的时间都足以学会它了,所以似乎没有封装的必要了。
其实我想到在BEAN中组装SQL语句实乃万不得以,因为我的查询条件本来就是由传进来的一大堆参数组成的,如果本来就一条SQL语句,我也没必要把它拆开呀!所以JSP页面上就充满一大堆的代码,其目的为何,就为了组合一条正确的SQL语句。
因此,我觉得 AiNet(人工智能) 的方法不失为一种好办法,鱼和熊掌兼得,何乐不为?你以为呢?