RESIN: Database connection

zhaozj2021-02-16  56

Foreword: In web programming, database connection has always been a problem of headache. The quality of database connections directly affects the speed of access to the entire website.

Currently, in the construction of website, JSP / Java Servlet / Java Bean is used as dynamic page programming has become more and more recognized. As a free web server RESIN, it is one of the selection of small and medium-sized sites. I chose the free database MySQL.

RESIN is now 3.x, which itself provides a connection pool, configured as follows: resin.conf ... jdbc / mysql // jndi-name // mysql drive driver jdbc: mysql: // localhost: 3306 / test // URL root // Database connection Username // Password 8 20 // Maximum number of connections is 20 30s ...

Configure resin.conf, restart the RESIN server. Below we can write the code to connect the database.

Write code with Java Bean: package com.ewe.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;

** * Title: dbconnection.class * Description: Connection mysql * Copyright: Copyright (c) 2004 * Company: www.Ewe.com.cn * @Author bluesunny * /

Public class dbconnection {private constiction con = 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 INITCTX = New InitialContext (); context envctx = (context) ITCTX.lookup ("Java: comp / env "); DataSource DS = (DataSource) Envctx.lookup (" JDBC / MySQL "); con = ds.getConnection ();} catch (exception ex) {throw new Exception (" COULDN '' Open connection to database: " EX.getMessage ());}}

/ ** * Execute SQL statement: Query Record * @Param SQL SQL statement * @Return ResultSet Record Set * / Public ResultSet ExecuteQuery (String SQL) {RS = NULL; try {if (con == null) Con = ds.getConnection (); stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = 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 Update Document * / Public Boolean ExecuteUpdate (String SQL) {Resultnum = 0; try {if (con == null) Con = getConnection (); stmt = con.createstatement (ResultSet.Type_Scroll_Insensitive, ResultSet.concur_read_only); this.Resultnum = stmt.executeUpdate (SQL); return true;

} catch (sqlexception se) {system.err.println ("Update Error:" se.getMessage ()); Return False;}

}

/ ** * Close connection * / public void close () {try {if (rs! = Null) {r.close (); rs = null;} if (stmt! = Null) {stmt.close (); STMT = NULL;} f (inc! = null); con = null;}} catch (sqlexception se) {system.out.println ("Close Error:" se.getMessage ());} }} It's okay, it's here, the big work is warned. Below we can write a JSP page to test it, call this Java Bean class in the JSP page, and the specific code is slightly.

Note: 1. Remember to turn off the database connection in time. Such a database connection can be used as a small and medium-sized website, which is not suitable for large websites.

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

New Post(0)