JavaBean package with a JDBC operations
On the database in the importance of project development, I think I do not have on this fruitless so on the importance of the database operation, I do not think I would even call! But you thought that such a few problems: in your every item in the total number of sub-projects need to database operations? For the operation of your database design JavaBean will meet the requirements of different component? When users request the use of other database systems, you need to document compiled Class recompiled?
If you are a very diligent programmer, you might say: "indifferent, as long as amend the connection and then on OK to compile!" But you have ever thought about, if we can spend a little time to perfect this JavaBean We even such "modifications" are omit?
The author is not a diligent programmers, and therefore hoped that through a JavaBean to complete most of the database operations at the same time hope that the JavaBean able to meet most of the current mainstream database. On this basis, write a JavaBean, being named LPWDatabaseOperation. JavaBean for the following information:
First, support for Oracle, Sybase, MySQL, SQLServer, DB2, PostgreSQL, Jdbc-Bridge-Odbc database, such as the operation of the DriverManager format;
Second, provide a Tomcate servers as a platform for the operation of data sources (such as other WebLogic, WebSphere, and other large server, usually through entities Bean to access a database, the procedure did not provide both server data source access, but retain the corresponding location, interested readers can add their own conduct.);
Third, the use of only one method of data executeSQL to add, delete, modify, and query operations unified treatment method eliminates the trouble of multiple memory;
4, provides a simple paging display support.
This paper provides a JavaBean the integrity of the source code, source code files are detailed in the Notes, interested readers can be compiled into development documents for ready reference. Source code as follows:
Package lpw.beans;
Import java.sql.DriverManager;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import java.sql.ResultSet;
Import javax.sql.DataSource;
Import javax.naming.Context;
Import javax.naming.InitialContext;
/ **
* <p> Title: </ p>
* <p> Description: </ p>
* <p> Copyright: Copyright
2004 </ p>
* <p> Company: LU Pei Wen </ p>
* @ Author not attributable
* @ Version 1.0
* /
Public class LPWDatabaseOperation
(
/ **
* Use Oracle format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useOracleDriverManager = 0;
/ **
* Sybase format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useSybaseDriverManager = 1;
/ **
* Use MySQL format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useMysqlDriverManager = 2;
/ **
* Use SQLServer format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useSqlserverDriverManager = 3;
/ **
* Use DB2 format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useDb2DriverManager = 4;
/ **
* Informix format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useInformixDriverManager = 5;
/ **
* Use PostgreSQL format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int usePostgresqlDriverManager = 6;
/ **
* Use Jdbc-Odbc-Bridge format DriverManager
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useJdbcOdbcBridge = 7;
/ **
* Use Tomcate format DataSource
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useTomcateDataSource = 8;
/ **
* WebLogic format DataSource
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useWeblogicDataSource = 9;
/ **
* Use the format WebSphere DataSource
* @ See LPWDatabaseOperation # getUseContextType
* /
Public final int useWebsphereDataSource = 10;
////////////////////////////////////////////////// //////////////////////////
Private String [] driverManagerType;
Private int useContextType;
Private int pageSize, pageCount, absolutePage, recordCount;
////////////////////////////////////////////////// //////////////////////////
Private Connection connection;
Private PreparedStatement preparedStatement;
Private ResultSet resultSet;
/ **
* Constructors LPWDatabaseOperation.
* The structure takes choice used by the type of environment,
* LPWDatabaseOperation support multiple drives and mainstream DriverManager DataSource environment,
* Definition of a number of commonly used and DriverManager DataSource and the standard format,
* Including Oracle, Sybase, MySQL, such as SQLServer database DriverManager mainstream format,
* Odbc and Jdbc-Bridge-standard format,
* At the same time also provided Tomcate, WebLogic and WebSphere DataSource under the standard format,
* When using the programmer need only select the appropriate constants can be.
* @ Param useContextType int <br> use of the type of environment
* @ See LPWDatabaseOperation # getUseContextType
* /
Public LPWDatabaseOperation (int useContextType)
(
If (useContextType <0) useContextType = 0;
If (useContextType> 7) useContextType = 7;
This.useContextType = useContextType;
////////////////////////////////////////////////// //////////////////////
This.driverManagerType [this.useOracleDriverManager] = new String ( "oracle.jdbc.driver.OracleDriver");
This.driverManagerType [this.useSybaseDriverManager] = new String ( "com.sybase.jdbc.SybDriver");
This.driverManagerType [this.useMysqlDriverManager] = new String ( "com.mysql.jdbc.Driver");
This.driverManagerType [this.useSqlserverDriverManager] = new String ( "com.microsoft.jdbc.sqlserver.SQLServerDriver");
This.driverManagerType [this.useDb2DriverManager] = new String ( "com.ibm.db2.jdbc.app.DB2Driver");
This.driverManagerType [this.useInformixDriverManager] = new String ( "com.informix.jdbc.IfxDriver");
This.driverManagerType [this.usePostgresqlDriverManager] = new String ( "org.postgresql.Driver");
This.driverManagerType [this.useJdbcOdbcBridge] = new String ( "sun.jdbc.odbc.JdbcOdbcDrive");
////////////////////////////////////////////////// //////////////////////
This.pageSize = 20;
This.pageCount = 0;
This.absolutePage = 0;
This.recordCount = 0;
////////////////////////////////////////////////// //////////////////////
This.connection = null;
This.preparedStatement = null;
This.resultSet = null;
)
/ **
* Open database, the methods need to provide the URL database, the user name and user password.
* After the success of the database will be open executeSQL way through the database to operate.
* @ Param databaseURL String <br> database URL address, if you use the database for DataSource JNDI-Name
* @ Param userName String <br> user name
* @ Param password String <br> user passwords
* @ Throws java.sql.SQLException
* @ Throws java.lang.ClassNotFoundException
* @ Throws javax.naming.NamingException
* /
Public void openDatabase (String databaseURL, String userName, String password)
Throws java.sql.SQLException, java.lang.ClassNotFoundException, javax.naming.NamingException
(
If (this.useContextType> = 0 & & this.useContextType <= this.useJdbcOdbcBridge)
(
Class.forName (this.driverManagerType [this.useContextType]);
This.connection = DriverManager.getConnection (databaseURL, userName, password);
)
Else if (this.useContextType == this.useTomcateDataSource)
(
Context context = new InitialContext ();
DataSource dataSource = (DataSource) context.lookup (databaseURL);
This.connection = dataSource.getConnection (userName, password);
)
Else if (this.useContextType == this.useWeblogicDataSource)
(
)
Else if (this.useContextType == this.useWebsphereDataSource)
(
)
)
/ **
* Executing SQL statements, it could be Select, Insert, Delete, and Update of any one.
* @ Param sql String <br> to the implementation of the SQL statement
* @ Return ResultSet <br> if the query operation, the return to the query operation ResultSet If the other is the implementation of the operation, return null.
* @ Throws java.sql.SQLException
* /
Public ResultSet executeSQL (String sql) throws java.sql.SQLException
(
Sql = sql.trim ();
This.preparedStatement = this.connection.prepareStatement (sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
If (sql.substring (0,1). EqualsIgnoreCase ( "s"))
(
This.resultSet = this.preparedStatement.executeQuery (sql);
Return this.resultSet;
)
Else
(
This.preparedStatement.executeUpdate (sql);
Return null;
)
)
/ **
* Close Database
* @ Throws java.sql.SQLException
* /
Public void closeDatabase () throws java.sql.SQLException
(
If (this.resultSet! = Null)
(
This.resultSet.close ();
)
If (this.preparedStatement! = Null)
(
This.preparedStatement.close ();
)
If (this.connection! = Null)
(
This.connection.close ();
)
)
/ **
* Was currently using the type of environment.
* LPWDatabaseOperation defined by these values: <br>
* UseOracleDriverManager - using Oracle format DriverManager <br>
* UseSybaseDriverManager - use Sybase format DriverManager <br>
* UseMysqlDriverManager - use MySQL format DriverManager <br>
* UseSqlserverDriverManager - use SQLServer format DriverManager <br>
* UseDb2DriverManager - use DB2 format DriverManager <br>
* UseInformixDriverManager - use Informix format DriverManager <br>
* UsePostgresqlDriverManager - use PostgreSQL format DriverManager <br>
* UseJdbcOdbcBridge - Jdbc-Odbc-use format DriverManager <br> Bridge
* UseTomcateDataSource - use Tomcate format DataSource <br>
* UseWeblogicDataSource - use WebLogic format DataSource <br>
* UseWebsphereDataSource - use WebSphere format DataSource
* @ See LPWDatabaseOperation # useOracleDriverManager
* @ See LPWDatabaseOperation # useSybaseDriverManager
* @ See LPWDatabaseOperation # useMysqlDriverManager
* @ See LPWDatabaseOperation # useSqlserverDriverManager
* @ See LPWDatabaseOperation # useDb2DriverManager
* @ See LPWDatabaseOperation # useInformixDriverManager
* @ See LPWDatabaseOperation # usePostgresqlDriverManager
* @ See LPWDatabaseOperation # useJdbcOdbcBridge
* @ See LPWDatabaseOperation # useTomcateDataSource
* @ See LPWDatabaseOperation # useWeblogicDataSource
* @ See LPWDatabaseOperation # useWebsphereDataSource
* @ Return int <br> use of the current type of environment
* /
Public int getUseContextType ()
(
Return useContextType;
)
/ **
* Set up the current page to be displayed per page to display data from several methods of parameter values setPageSize decision
* When the input value is less than 1 page, the first page will be displayed;
* When the input value to the page than the last page of the page, the last page will be displayed.
* It must be noted that, through the result set to setAbsolutePage positioning, and will not make enquiries result set change,
* And concentrate only on the results of the indicators (cursor), the locator.
* So if the real programmers want paging effect, reference should be made to achieve the following procedures for the corresponding function: <br>
* LPWDatabaseOperation database; <br>
* <br>
* ResultSet resultSet = database.executeSQL ( "Select * From Table"); <br>
* <br>
* Database.setPageSize (20); <br>
* Database.setAbsolutePage (2); <br>
* For (int i = 1; i <= database.getPageSize (); i + +) <br>
* (<br>
* <br>
* If (resultSet.isLast ()) <br>
* (<br>
* Break; <br>
*) <br>
* ResultSet.next (); <br>
*)
* @ See LPWDatabaseOperation # setPageSize
* @ Param absolutePage int <br> to show value to the page
* /
Public void setAbsolutePage (int absolutePage)
(
This.getPageCount ();
If (absolutePage <1) absolutePage = 1;
If (absolutePage> this.pageCount) absolutePage = this.pageCount;
This.absolutePage = absolutePage;
)
/ **
* Was revealed that the current value of the page
* @ Return int <br> value currently displayed page
* /
Public int getAbsolutePage ()
(
Return absolutePage;
)
/ **
* Set up each page of the few records show.
* When the input parameter values less than 1:00, a record per page;
* When the input parameter values greater than the total number of records, all data will be displayed;
* The default is 20 per page will be displayed records.
* The programmer can change the current method setAbsolutePage displayed page.
* @ See LPWDatabaseOperation # setAbsolutePage
* @ Param pageSize int <br> Every page shows the number of rows of data
* /
Public void setPageSize (int pageSize)
(
If (pageSize <1) pageSize = 1;
This.pageSize = pageSize;
)
/ **
* Access to the records to be displayed per page of the few
* @ Return int <br> per page of the record number
* /
Public int getPageSize ()
(
Return pageSize;
)
/ **
* The results obtained show that the total set of pages, and its value is: the record / per page of the records of [+1].
* @ Return int <br> Results Set Records show that the total number of pages
* /
Public int getPageCount ()
(
This.getRecordCout ();
This.pageCount = this.recordCount / this.pageSize;
If ((this.recordCount% this.pageSize! = 0)) this.pageCount + +;
Return pageCount;
)
/ **
* Access to the record result set several records
* @ Return int <br> record of the total result set several records
* /
Public int getRecordCout ()
(
Try
(
If (this.resultSet.last ())
(
This.recordCount = this.resultSet.getRow ();
)
Else
(
This.recordCount = 0;
)
)
Catch (java.sql.SQLException e)
(
)
Finally
(
Return this.recordCount;
)
)
)
Tags: package






