[Repost] JNDI connection database model

xiaoxiao2021-03-06  42

Package dbutil;

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, create a connection with this data source * / public dbconnection () {Try {context ctx = new initialContext (); if (ctx == null) Throw new Exception ("no context") DataSource DS = (Datasource) CTX.lookup ("Java: Comp / Env / JDBC / Oracle"); if (DS == NULL) Throw new Exception ("JDBC / ORACLE IS An Unknown Datasource"); conn = DS. 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 Update Document * / Public Int ExecuteUpdate (String SQL) {Resultnum = 0; try {results (SQL) );} Catch (sqlexception se) {system.err.println ("Update error:" se.getMessage ());} Return Resultnum;} / ** * Close connection * / public void close () {Try {i (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 ());}}}

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

New Post(0)