JSP beginners use the database to meet the problem of database connection beans. 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 the number of visits is not large enough.
/ ********************************************************************************** ********************* / 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 Statement stmt = null; private ResultSet rs = 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 * / Public Int ExecuteUpdate (String SQL) {Resultnum = 0; try {resultnum = stmt.executeUpdate (SQL);
} catch ("Update Error:" se.getMessage ());} Return ResultNum;} / ** * Close connection * / public void close () {i ix ! = null) {rclose (); rs = null;} if (stmt! = null) {stmt.close (); stmt = null;} if (conn! = null) {conn.close (); conn = NULL;}} catch (sysXception se) {system.out.println ("Close Error:" se.getMessage ());}}} The following is a configuration example of the data source in the RESIN configuration file: