RESIN connection pool + database bean

xiaoxiao2021-03-06  84

Author: matrix authorship: fajaven dispatch time: 2003.07.30 10: 51: 48JSP beginners to use a database will encounter problems of database connections BEAN.

And to beginners also recommend RESIN to do server, configuration is simple (development does not need to configure Apache).

So I will send my own database to the baco.

Since Resi itself also provides a connection pool, it is more expensive, and it is enough when the number of visitors is not large.

===================

/ ********************************************************************************** ******************** / package net.asales.mysql;

import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import javax.naming.Context; import javax.naming.InitialContext;

Public class dbconnection {private connection conn = null; private resultset = null; private int resultnum = 0;

/ ** * Constructor * Find the data source and create a connection with this data source * / public dbconnection () {Try {context env = new initialContext (); DataSource pool = (datasource) env.lookup ("Java: comp / env / jdbc / asales "); if (pool == null) Throw new Exception (" JDBC / ASALES IS An Unknown Datasource "); conn = pool.getConnection (); stmt = conn.createstatement ();} catch (Exception E ) {System.out.println ("Naming:" E.getMessage ());}}

/ ** * Execute SQL statement: Query Record * @Param SQL SQL statement * @Return ResultSet Record Set * / Public ResultSet ExecuteQuery (String SQL) {RS = NULL; try {r = stmt.executeQuery (SQL);} catch SQLEXCEPTION SE) {system.out.println ("Query Error:" se.getMessage ());} Return RS;} / ** * Execute SQL statement: Insert and Update Record * @Param SQL SQL Statement * @return Int resultNum updated record number * / public int executeUpdate (String sql) {resultNum = 0; try {resultNum = stmt.executeUpdate (sql);} catch (SQLException se) {System.err.println ( "update error:" se .getMessage ());} Return Resultnum;}

/ ** * Close connection * / public void close () {try {if (rs! = Null) {r.close (); rs = null;} if (stmt! = Null) {stmt.close (); STMT = NULL;} if (conn! = null) {conn.close (); conn = null;}} catch (sqlexception se) {system.out.println ("Close Error:" se.getMessage ());} }

===================

The following is a configuration example of the data source in the RESIN configuration file:

转载请注明原文地址:https://www.9cbs.com/read-106674.html

New Post(0)