1. Luckydb.properties # - Database --- db.driver.Oracle.jdbc.driver.racleDriverdb.url = jdbc: Oracle: Thin: @ tekt2: 1521: Bakdb.user = readonly db.password = readonlyDB.MAX_CONNECTIONS = 302, establish DBConnectionManage / * * Created on 2004-8-10 * * TODO to change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates * / package com.tek. ALL.DATABASE;
import java.sql.Connection; import java.sql.SQLException; import java.util.Vector; import java.sql.DriverManager; import com.tek.all.database.Configuration; / ** * @author qianzh * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class DBConnectionManage {private final static DBConnectionManage instance = new DBConnectionManage (); private DBConnectionPool pool; public static DBConnectionManage getInstance () { return instance;} public void freeConnection (Connection con) throws SQLException {pool.freeConnection (con);} public Connection getConnection () throws SQLException {return pool.getConnection ();} private DBConnectionManage () {init ();} private void INIT () {configuration cfg = configuration.getInstance (); string db_driver = cfg.getValue ("db.driver"); string db_url = cfg.getValue ("db.url"); string db_user = cfg.getValue ("DB .User "); string db_password = cfg.getValue (" db.password "); int db_ maxConn = Integer.parseInt (cfg.getValue ( "DB.MAX_CONNECTIONS")); try {Class.forName (db_driver);} catch (ClassNotFoundException ex) {System.out.println (ex);} pool = new DBConnectionPool (db_url , db_user, db_password, db_maxConn);} class DBConnectionPool {private Vector freeConnections = new Vector (); private int maxConn; private int connNumb; private String URL; private String password; private String user; public DBConnectionPool (String URL, String user, String password, int mapconn) {this.url = url; this.user = user; this.parsword = password; this.maxconn = maxconn; //system.out.print ("here is OK! ! ! "
URL "___" user password);} public synchronized void freeConnection (Connection con) {freeConnections.addElement (con); connNumb--; notifyAll ();} public synchronized Connection getConnection () throws SQLException {Connection con = null ; If (freeConnections.Size ()> 0) FreeConnections.FirstElement (); freeConnections.removeElementat (0); try {if (con?isclosed ()) {con = getConnection (); // system () .out.print ("3 here is OK !!!");}} catch (sqlexception e) {con = getConnection ();}}} else if (MaxConn == 0 || connumb //System.out.print ("1 here is OK !!! ");} catch (exception e) {system.out.print (" can't read the file ");} finally {try {ix ( Is! = null) {is.close ();}} catch (ooException ex) {}}} / public static configuration getinstance () {return;} public string getValue (String Key) {Return Properties.getProperty (Key) }} 4, establish dbaccess / * * now 2004-8-11 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * / package com.tek.All .database; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.SQLException; / ** * @author qianzh * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class DBAccess {protected Statement statement; protected ResultSet resutlset; protected Connection connection; public DBAccess (Connection connection) {this.connection = connection;} public void close Select () throws SQLException {if (statement = null!) {Statement.close ();}} public ResultSet openSelect (String sql) throws SQLException {statement = connection.createStatement (); ResultSet rs = statement.executeQuery (sql); return rs;} public int runSql (String sql) throws SQLException {statement = connection.createStatement (); int result = statement.executeUpdate (sql); statement.close (); return result;}} 5, following the establishment of a database selected HelpBean data. Package com.tek.all.database; import java.sql.connection; import java.sql.resultset; import java.sql.statement; import com.tek.all.filter.changeiso; import com.tek.all.Database. DbConnectionManage; import com.tek.all.struts.form. *; Import java.sql. *; Import java.util. *; Import com.tek.all.struts.form.manageuserform; import com.tek.all.database .DBAccess; / ** * @author qianzh * * TODO to change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class HelpBean {private DBConnectionManage dbm = DBConnectionManage.getInstance ( ); Connection conn = null; ChangeISO iso = new ChangeISO (); public void savaRegForm (regForm regform, int i) throws Exception {int t = i 1; conn = dbm.getConnection (); String sql = "insert into tek_user " " (U_id, user_name, " " Password, " " Department, " " Work_ID, " " full_name, " " Contact_Mobile, " " Contact_Phone, " " Contact_Email_IN, " " Contact_Email_out, " ""Flag," "REG_DATE)" "VALUES ('" t ",?,?,?,?,?,?,?,?,?,, 1, sysdate, prepaaredState pStmt = conn.preparestatement (SQL); PSTMT.SetString (1, regform.getuser_name ()); pstmt.setstring (2, regform.get dispatch ()); pstmt.setstring (3, iso.trans (regform.getDepartment ()); PSTMT. SetString (4, Regform.getWork_ID ()); PStmt.setString (5, iso.trans (regform.getfull_name ())); pstmt.setstring (6, regform.getContact_Mobile ()); PSTMT.SetString (7, Regform. GetContact_Phone ());