Package com.tzsw.hjyweb.admin;
Import java.io. *; import java.sql. *; import javax.sql. *; import com.tzsw.hjyweb.db. *;
/ ** *
Data transfer, from the business system to the website business system p> * @version 1.0 * /
Public class datamove {string logfile = ""; static datasource datasource; string oprtable = ""; // boolean skip = false;
Public DataMove () {Try {DataSource = TopcheerDataSource.getDataSource ();} catch (excetion e) {system.out.println (E.getMessage ());}}
Public void writelog (String Writestr) {Try {randomaccessfile out = new file (logfile), "rw"); out.seek (out.Length ()); out.write (Writestr.getbytes ()); OUT .write ("/ n" .GetBytes ()); out.close ();} catch (filenotfoundexception e) {system.out.println (E.GetMessage ());} catch (ioexception e) {system.out. PRINTLN (E.getMessage ());}}
public String readLog () {StringBuffer sb = new StringBuffer (); try {BufferedReader in = new BufferedReader (new FileReader (new File (logfile))); while (in.ready ()) {sb.append (in.readLine ( )); //Sb.append ("/n "); sb.append ("
");} in.close ();} catch (filenotfoundexception e) {system.out.println (E.GetMessage ))); Return "The log file has not been established or has been destroyed!";} Catch (ieException e) {system.out.println (E.GetMessage ()); Return "error in the process of reading the log file!";} Return sb.toString ();
Public void setlogfile (string logfile) {this.logfile = logfile;
public void movePersonalIn () {try {Connection conn = dataSource.getConnection (); Statement statement = conn.createStatement (); conn.setAutoCommit (false); try {int labour = 0, seekjob = 0, resume = 0, temp_labour = 0, TEMP_SEEKJOB = 0, TEMP_RESUME = 0, Web_Users = 0, Seekjobadvert = 0, Photo = 0;
labour = statement.executeUpdate (labourIn ()); // wzq add 2003-09-25 statement.executeUpdate (seekjobIn_Del ()); seekjob = statement.executeUpdate (seekjobIn ()); resume = statement.executeUpdate (resumeIn ()) Photo = statement.executeUpdate (Photo); conn.commit (); stringbuffer sb = new stringbuffer (); sb.append ("personal user table is imported:"); sb.append (new java.util. Date (). TOSTRING (); sb.append ("/ n"); sb.append ("/ TLABOUR Table successfully imported); sb.append (labour); sb.append (" record ./n "); Sb.append (" / tseek.com "); sb.append (seekjob); sb.append (" strip record ./n "); sb.append (" / TResume table successfully imported ") Sb.append (resume); sb.append ("/ tphoto table successfully imported"); sb.append (photo); sb.append ("record ./n"); writelog (sb.toString ()) (SQLEXCEPTION E) {conn.rollback (); stringbuffer sb = new stringbuffer (); sb.append ("The personal user imports are as follows:"); sb.append (new java.util.date (). Tostring ()) Sb.append ("/ n"); sb.append ("/ t in the Import Table"); sb.append (oppend (oppended); error occurred! "); Sb.append (new java.util.date (). ToString ()); sb.append (" / n "); Writelog (sb.tostring ()); E.PrintStackTrace ();} Statement.close (); Conn.close (); Catch (SQLException E) {system.out.println (E.getMessage ()); E.PrintStackTrace ();}}
public void moveCompanyIn () {try {Connection conn = dataSource.getConnection (); Statement statement = conn.createStatement (); conn.setAutoCommit (false); try {int company = 0, employer = 0, want = 0, temp_company = 0, TEMP_EMPLOYER = 0, TEMP_WANT = 0, Web_company_user = 0, CompanyAdvert = 0;
company = statement.executeUpdate (companyIn ()); // wzq add 2003-09-25 statement.executeUpdate (employerIn_Del ()); employer = statement.executeUpdate (employerIn ()); want = statement.executeUpdate (wantIn ()) Conn.commit (); StringBuffer SB = new stringbuffer (); sb.append ("unit user table import (new java.util.date (). Tostring ()); SB. Append ("/ n"); sb.append ("/ tcompany table is successfully imported); sb.append (company); sb.append (" record ./n "); sb.append (" / temployer table Successfully imported "); sb.append (employer); sb.append (" record ./n "); sb.append (" / twant table is successfully imported); sb.append (want); sb.append ("Record ./N"); WRITELOG (SB.Tostring ());} catch (sqlexception e) {conn.rollback (); stringbuffer sb = new stringbuffer (); sb.append ("unit user import : "); Sb.append (new java.util.date (). ToString ()); sb.append (" / n "); sb.append (" / t in the Import Table "); Sb.Append (Oprtable ); Sb.append ("error occurred!"); S B.Append (new java.util.date (). toString ()); sb.append ("/ n"); Writelog (sb.tostring ()); E.PrintStackTrace (); // system.out.println (E.GetMessage ();} statement.close (); conn.close ();} catch (sqlexception e) {E.PrintStackTrace ();}}
private String labourIn () {oprTable = "Labour"; String sql = "insert into labour" "(select * from labour @ usremlmnew" "where labidno in" "(select labidno from labour @ usremlmnew" "minus select Labidno from labour) "; return SQL;} private string seekjobin_del () {oprtable =" seekjob "; string sql =" delete from seekjob where sjbflag = 'y' "; return sql;}
private String seekjobIn () {oprTable = "Seekjob"; String sql = "insert into seekjob (sjbno, sjbidno, sjbname, sjbpostreqcd1," "sjbpostreqcd2, sjbpostreqcd3, sjbemptpreq1, sjbemptpreq2," "sjbemptpreq3, sjbminsalreq, sjbaddreq, sjbturnreq1, sjbturnreq2, sjbturnreq3, sjbvalid, sjbnote, sjbiscopy, " " sjbstate, sjbrectime, sjboprdt, sjboprcd, insertdt, insertuserid, insertdeptid, " " qnote0, qnote1, qnote2, mnote0, mnote1, mnote2, dnote0, dnote1, dnote2, sjbflag) " " (select sjbno, sjbidno, sjbname, sjbpostreqcd1, sjbpostreqcd2, sjbpostreqcd3, sjbemptpreq1, sjbemptpreq2, " " sjbemptpreq3, sjbminsalreq, sjbaddreq, sjbturnreq1, sjbturnreq2, sjbturnreq3, sjbvalid, sjbnote, sjbiscopy, " " sjbstate, sjbrectime, sjboprdt , sjboprcd, sysdate, insertuserid, insertdeptid, " " qnote0, qnote1, qnote2, mnote0, mnote1, mnote2, dnote0, dnote1, dnote2, 'Y' " " from seekjob @ usremlmnew where sjbno in (select sjbno from seekjob @ usremlmnew " " Where sjbstate = 'wt' and sjbweb = 'y' and sjbvalid 1> sysdate minus select sjbno from seekjob) "; Return SQL;}
private String resumeIn () {oprTable = "Resume"; String sql = "insert into resume" "(select * from resume @ usremlmnew" "where rsmno in" "(select rsmno from resume @ usremlmnew" "minus select RSMNO from resume) "; return sql;}
private String companyIn () {oprTable = "company"; String sql = "insert into company (" "cmpcmpcd, cmpname, cmptype, cmpectype, cmpusccd," "cmpclass, cmptrade, cmpmother, cmprange, cmpregion," "cmpscale , cmpinvarea, cmpinvtl, cmplglname, cmptel, " " cmplnkman, cmplnkmanadd, cmppstcd, cmpregadd, cmpregrmb, " " cmpregfc, cmpfctype, cmpflag, cmpismember, cmpcredit, " " cmpifcons, cmpbank, cmpacctno, cmpstartdt, cmpenddt, " "cmpinittown, cmpinitfarm, cmpcmpregdt, cmpregcd," "cmpregdt, cmpoprcd, cmpoprdt, cmplicence, cmpnote)" "(select" "cmpcmpcd, cmpname, cmptype, cmpectype, cmpusccd," "cmpclass, cmptrade, cmpmother, cmprange , cmpregion, " " cmpscale, cmpinvarea, cmpinvtl, cmplglname, cmptel, " " cmplnkman, cmplnkmanadd, cmppstcd, cmpregadd, cmpregrmb, " " cmpregfc, cmpfctype, cmpflag, cmpismember, cmpcredit, " " cmpifcons, cmpbank, cmpacctno , cmpstartdt, cmpenddt, " "Cmpinittown, cmpinitfarm, cmpcmpregdt, cmpregcd," "cmpregdt, cmpoprcd, cmpoprdt, cmplicence, cmpnote" "from company @ usremlmnew" "where cmpcmpcd in" "(select cmpcmpcd from company @ usremlmnew" "minus select cmpcmpcd From companies) "; return sql;}
private String employerIn_Del () {oprTable = "employer"; String sql = "delete from employer where empflag = 'Y'"; return sql;} private String employerIn () {oprTable = "employer"; String sql = "insert into employer (empno, empauthor, empcmpcd, empcmpname, emplink, emptel, emplnkpc, emplnkadd, emplnkway, empwkadd, " " empwkway, empemptp, emptype, emptimddt, emppitchadd, empobj, empregion, empwkplc, " " empappdt, empappenddt, empaddt, empadenddt , empisrush, empisnormal, empnote, empstatus, " " emptype1, empplacard, empoprdt, empoprcd, insertdt, insertuserid, insertdeptid, " " qnote0, qnote1, qnote2, mnote0, mnote1, mnote2, dnote0, dnote1, dnote2, empflag) " "(select empno, empauthor, empcmpcd, empcmpname, emplink, emptel, emplnkpc, emplnkadd, emplnkway, empwkadd," "empwkway, empemptp, emptype, emptimddt, emppitchadd, empobj, empregion, empwkplc," "empappdt, empappenddt, empaddt, empadenddt, empisrush, empisnormal, empnote, empstatus, " " emptype1, empplacard, empoprdt, empoprcd, sysdate, insertuserid, insertdeptid, " " qnote0, qnote1, qnote2, mnote0, mnote1, mnote2, dn ote0, dnote1, dnote2, 'Y' " " from employer @ usremlmnew where empno in (select empno from employer @ usremlmnew " " where empappenddt 1> sysdate and empweb = 'Y' and (empstatus = 'UU' or empstatus = 'US') AND Empweb = 'y' " " Minus Select Empno from Employer) "; Return SQL;}
private String wantIn () {oprTable = "want"; String sql = "insert into want (select * from want @ usremlmnew where wntempno in" "(select empno from employer where empflag = 'Y'))"; return sql; } private String photoIn () {oprTable = "photo"; String sql = "INSERT INTO photo (SELECT * fROM photo @ usremlmnew where phoidno in" "(select phoidno from photo @ usremlmnew minus select phoidno from photo))";
Return Sql;} public static void main (string [] args) {datamove app = new datamove (); app.movePersonalin (); // app.movecompanyin (); app.setlogfile ("fdsaf"); app.writelog "China"); app.writelog ("new2"); system.out.println (app.readlog ());}} ===================== ===========================================================Pumage com.tzsw.hjyweb. Db; import java.sql.connection; import java.sql.driver; import java.util.properties; / * *
Title: p> *
description: p> *
Copyright: Copyright (c) 2003 p> *
Company: p> * @Author not attributable * @version 1.0 * /
. Public class DbConnect {public DbConnect () {} public static Connection getConnect () {try {Driver driver = (Driver) Class.forName ( "oracle.jdbc.driver.OracleDriver") newInstance (); String url = "jdbc: Oracle: Thin: @ 192.2.3.210: 1521: hzjy "; Properties P = New Properties (); P.SetProperty (" User "," Hjyweb "); P.SetProperty (" Password "," Oracle "); Connection CONN = Driver.connect (URL, P); Return Conn;} catch (excetion e) {system.out.println (E.getMessage ());} return null;} public static void main (string [] args) THROWS EXCEPTION {Dbconnect dbtest = new dbconnect (); system.out.println ("**" dbtest);}} ========================= ====================================================== Package com.tzsw .hjyweb.db;
Import com.tzsw.hjyweb.db.topcheerdynamicupdate; import com.tzsw.hjyweb.db.topcheerQuery;
Public class dosqloperate {
Protected Boolean Updatestatus = false; protected string exceptionMessage = ""; public dosqloperate () {}
public boolean hasSame (String sql) {boolean result = false; TopcheerQuery topcheerQuery = new TopcheerQuery (); result = topcheerQuery.hasRecord (sql); topcheerQuery.close (); return result;}
public boolean insert (String sql) {TopcheerDynamicUpdate dynamicUpdate = new TopcheerDynamicUpdate (); try {dynamicUpdate.doUpdate (sql); updateStatus = true;} catch (Exception e) {updateStatus = false; exceptionMessage = e.getMessage ();} dynamicUpdate .close (); System.out.println (updateStatus); return updateStatus;} public boolean insert (String sql, String [] para) {TopcheerDynamicUpdate dynamicUpdate = new TopcheerDynamicUpdate (); try {dynamicUpdate.doUpdate (sql, para); Updatestatus = true;} catch (exception e) {updatestatus = false; exceptionMessage = E.getMessage ();} DynamicUpdate.close (); Return Updatestatus;
public boolean update (String sql) {TopcheerDynamicUpdate dynamicUpdate = new TopcheerDynamicUpdate (); try {int count = dynamicUpdate.doUpdate (sql); if (count> 0) updateStatus = true; else {updateStatus = false; exceptionMessage = "to be updated Record does not exist. ";}} Catch (Exception E) {updatestatus = false; ExceptionMessage = E.GetMessage ();} DynamicUpdate.close (); Return Updatestatus;
public boolean update (String sql, String [] para) {TopcheerDynamicUpdate dynamicUpdate = new TopcheerDynamicUpdate (); try {int count = dynamicUpdate.doUpdate (sql, para); if (count> 0) updateStatus = true; else {updateStatus = false ; exceptionMessage = "record to be updated does not exist.";}} catch (Exception e) {updateStatus = false; exceptionMessage = e.getMessage ();} dynamicUpdate.close (); return updateStatus;} public boolean delete (String sql ) {TopcheerDynamicUpdate dynamicUpdate = new TopcheerDynamicUpdate (); try {int count = dynamicUpdate.doUpdate (sql); if (count> 0) updateStatus = true; else {updateStatus = false; exceptionMessage = "record to be deleted does not exist."; }}} CatCH (Exception E) {updatestatus = false; ExceptionMessage = E.getMessage ();} DynamicUpdate.close (); Return Updatestatus;
public boolean delete (String sql, String [] para) {TopcheerDynamicUpdate dynamicUpdate = new TopcheerDynamicUpdate (); try {int count = dynamicUpdate.doUpdate (sql, para); if (count> 0) updateStatus = true; else {updateStatus = false ExceptionMessage = "Records to be deleted do not exist.";}} Catch (Exception E) {updatestatus = false; ExceptionMessage = E.GetMessage ();} DynamicUpdate.close (); Return Updatestatus;
Public String getExceptionMessage () {return exceptionMessage;
Public void setExceptionMessage (String ExceptionMessage) {this.exceptionMessage = exceptionMessage;}} ================================== =================================================================================================== .Properties; / ** *
Title: p> *
description: p> *
Copyright: Copyright (c) 2004 p> *
Company: P > * @Author NOT Attributable * @version 1.0 * / public class sqlconnect {public sqlconnect () {
} Public static Connection getConnect () {try {Driver driver = (Driver) Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver") newInstance (); String url = "jdbc:. Microsoft: sqlserver: //192.2 .3.122: 1433; DatabaseName = SZLDBZ "; Properties P = New Properties (); P.SetProperty (" User "," SA "); P.SETPROPERTY (" Password "," 1 "); connection conn = driver.connect (URL, P); // Connection conn = java.sql.driverManager.getConnection (URL, P); // system.out.println (conn "##"); return conn;} catch (Exception E) {system .out.println (E.GetMessage ()); E.PrintStackTrace ();} return null;} public static void main (string [] args) throws exception {// sqlConnect dbtest = new sqlconnect (); system.out. PRINTLN (SqlConnect.getConnect () "**");}
} ==================================================== =========== Package com.tzsw.hjyweb.db; public interface sqloperate {boolean hasame (String SQL); Boolean INSERT (String SQL); Boolean Insert (String SQL, String [] Para); Boolean Update (String SQL); Boolean Update (String SQL, String [] Para); Boolean Delete (String SQL); Boolean Delete (String SQL, String [] Para); Info Query (String SQL); String getExceptionMessage (); Void setExceptionMessage (String ExceptionMessage);} ========================================== = package com.tzsw.hjyweb.db;
Import java.sql.connection; import java.sql.sqlexception; import java.sql.driver; import java.sql.databaseMetadata; import java.sql.sqlwarning; import java.util.properties;
public class TopcheerConnection {final int TRANSACTION_NONE = Connection.TRANSACTION_NONE; final int TRANSACTION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED; final int TRANSACTION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED; final int TRANSACTION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ; final int TRANSACTION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
PRIVATE CONNECTION Connection;
Public TopcheerConnection () {}
public void setConnection () throws SQLException, Exception {//connection=TopcheerDataSource.getDataSource () getConnection ();. connection = DbConnect.getConnect ();} public void setConnection (final String sdriver, final String url, final String user, final String password {try {driver = (driver) class.forname (sdriver) .newinstance (); Properties P = new Properties (); p.PUT ("User", user); p.PUT ("Password ", password); connection = driver.connect (URL, P);} catch (classnotfoundexception e) {throw e;} catch ({throw e;} catch (sqlexception e) {throw e;} catch (Exception e) {throw e;}}
Public connection getConnection () {return
Public void setAutocommit (Boolean b) throws sqlexception {connection.setautocommit (b);}
Public void commit () throws sqlexception {connection.commit ();
Public void rollback () THROWS SQLEXCEPTION {connection.rollback ();}
Public void releaseconnection () throws sqlexception {ix (connection! = null&&! connection.isclosed ()) connection.close ();}
public String getSQLWarning () throws SQLException {SQLWarning sqlwarnings = connection.getWarnings (); StringBuffer sb_warnings = new StringBuffer (); while (sqlwarnings = null!) {sb_warnings.append ( "error happened./n"); sb_warnings.append ( "Message IS" SQLWARNINGS.GETMESSAGE () "/ n"); SB_WARNINGS.APpend ("SqlState IS" SQLWARNINGS.GETSQLSTATE () "/ n"); SB_WARNINGS.APpend ("ErrorCode IS" SQLWarnings.GeterrorCode () "/ n"); sb_warnings.append ( "Continuing ...."); sqlwarnings = sqlwarnings.getNextWarning ();} return sb_warnings.toString ();} public void setTransactionIsolation (int level) throws SQLException {DatabaseMetaData DBMetadata = connection.getMetadata (); if (DBMetadata.supportstransactioniSolationledLevel (Level)) Connection.SetTransactioniSolation (Level);}
} ==================================================== ================ Package com.tzsw.hjyweb.db;
Import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception; import javax.sql.connectionpoolDataSource;
Import com.tzsw.hjyweb.pub.jndinames;
Public class topcheerconnectionPoolDataSource {
private static ConnectionPoolDataSource pooldataSource = null; private static Context context = null; public TopcheerConnectionPoolDataSource () {} public final static ConnectionPoolDataSource getConnectionPoolDataSource () throws Exception {try {if (pooldataSource == null) {pooldataSource = (ConnectionPoolDataSource) getInitContext () lookup. (JNDINames.HJYWEB_POOL_DATASOURCE);}} catch (NamingException e) {throw e;} catch (Exception e) {throw e;} return pooldataSource;} public final static context getInitContext () throws Exception {try {if (context == null ) {Context = new initialContext ();}} catch (namingexception e) {throw e;} catch (exception e) {throw e;} return context;}} ============== ====================================================== Package com.tzsw .hjyweb.db; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception; import javax.sql.datasource;
Import com.tzsw.hjyweb.pub.jndinames;
Public class topcheerdataserce {
private static DataSource dataSource = null; private static Context context = null; public TopcheerDataSource () {} public final static DataSource getDataSource () throws Exception {try {if (dataSource == null) {dataSource = (DataSource) getInitContext () lookup. (JNDINames.HJYWEB_POOL_DATASOURCE);}} catch (NamingException e) {throw e;} return dataSource;} public final static context getInitContext () throws Exception {try {if (context == null) {context = new InitialContext ();} } catch (namingexception e) {throw e;} catch (exception e) {throw e;} return context;}} ======================== ============================================ package com.tzsw.hjyweb.db; import java.sql.resultset Import java.sql.sqlexception; import com.tzsw.hjyweb.exception. *; import com.tzsw.hjyweb.pub.constants;
Public class topcheerdynamicquery {private boolean debug = false;
Private TopcheerConnection; Private TopcheerPreparedStatement TopcheerPreparedStatement
public TopcheerDynamicQuery () {try {topcheerConnection = new TopcheerConnection (); topcheerPreparedStatement = new TopcheerPreparedStatement (); setCurrentConnection ();} catch (Exception e) {e.printStackTrace ();}}
// Take more close, otherwise the cursor does not release public resultSet doquery (String SQL) {RSULTSET RS = NULL; try {= Operate (SQL, NULL);} catch (connectfailedExcection E) {system.out.println (e) );} catch (Exception e) {System.out.println (e);} return rs;} public ResultSet doQuery (String sql, String [] pstatementParameters) {ResultSet rs = null; try {return operate (sql, pstatementParameters) } Catch (connectfailedException e) {system.out.println (e);} catch (exception e) {system.out.println (e);} return}
Public String DoQueryone (String SQL) {String Result = ""; Try {ResultSet RS = Operate (SQL, NULL); if (RS == Null ||! rs.next ()) Return Result; Result = rs.getstring 1);} catch (connectfailedException e) {system.out.println (e);} catch (exception e) {system.out.println (e);} return result;}
public String doQueryOne (String sql, String [] pstatementParameters) {String result = ""; try {ResultSet rs = operate (sql, pstatementParameters); if (! rs == null || rs.next ()) return result; return Rs.getstring (1);} catch (connectfailedExcection e) {system.out.println (e);} catch (exception e) {system.out.println (e);} returnrate;}
Public Boolean Hasrecord (String SQL) {Boolean Found = False; Try {ResultSet RS = Operate (SQL, NULL); if (RS == Null ||! rs.next ()) Return Found; Found = true;} catch ConnectFailedException E) {system.out.println (e);} catch (exception e) {system.out.println (e);} returnif;
public boolean hasRecord (String sql, String [] pstatementParameters) {boolean found = false; try {ResultSet rs = operate (sql, pstatementParameters); if (! rs == null || rs.next ()) return found; found = true;} catch (ConnectFailedException e) {System.out.println (e);} catch (Exception e) {System.out.println (e);} return found;} public void close () {try {topcheerPreparedStatement.releasePreparedStatement (); Topcheerconnection.releaseConnection ();} catch (exception e) {E.PrintStackTrace ();}}
private void setCurrentConnection () throws Exception {try {if (debug) topcheerConnection.setConnection (Constants.DRIVER, Constants.URL, Constants.USER, Constants.PASSWORD); else topcheerConnection.setConnection ();} catch (SQLException e) {e .printStackTrace (); TopcheerPreparedStatement.releasePreparedStatement (); TopcheerConnection.releaseConnection (); throw e;}}
private void releaseResultSet (ResultSet rs) throws SQLException {if (! rs = null) rs.close ();} private ResultSet operate (String sql, String [] parameters) throws ConnectFailedException, Exception {ResultSet rs = null; try {if ( topcheerConnection.getConnection () == null) {throw new ConnectFailedException ();} if (topcheerConnection.getConnection () isClosed ()) setCurrentConnection.. (); if (topcheerConnection.getSQLWarning () length ()> 0) System.out .println (topcheerConnection.getSQLWarning ()); topcheerConnection.setTransactionIsolation (topcheerConnection.TRANSACTION_READ_COMMITTED); topcheerPreparedStatement.setPreparedStatement (topcheerConnection, sql, TopcheerPreparedStatement.TYPE_FORWARD_ONLY, TopcheerPreparedStatement.CONCUR_READ_ONLY); if (parameters = null!) {topcheerPreparedStatement.clearParameters (); TopcheerPreparedStatement.setstring (parameters);}} r = TopcheerPreparedStatement.executequer y ();} catch (SQLException e) {System.out.println (sql); releaseResultSet (rs); topcheerPreparedStatement.releasePreparedStatement (); topcheerConnection.releaseConnection (); throw e;} catch (Exception e) {releaseResultSet (rs ); topcheerPreparedStatement.releasePreparedStatement (); topcheerConnection.releaseConnection (); throw e;} return rs;} public static void main (String [] args) {TopcheerDynamicQuery app = new TopcheerDynamicQuery (); String sql = "select * from web_news" String SQL2 = "SELECT * WEB_NEWS WHERE WNNEWSID>? And Wnnewsid "; String SQL3 = "SELECT * from Web_News Where 1> 2";
String Para [] = new string [2]; para [0] = "6"; para [1] = "5"; ResultSet RS = app.doQuery (SQL); try {if (rs! = Null) {while (rs.next ()) {system.out.println (rs.getstring (1));}}} catch (sqlexception e) {E.PrintStackTrace ();} ResultSet RS2 = app.doQuery (SQL2, PARA); Try {IF (rs2! = null) {while (rs2.next ()) {system.out.println (rs2.getstring (1));}}} catch ();}} catch SQLEXCEPTION E) {E.PrintStackTrace ();} system.out.println; system.out.println (app.doQueryone (sql2, para); system.Out.println (app.doQueryone) (SQL3)); system.out.println; system.out.println (app.hasrecord (sql2, para); system.out.println (app.hasrecord (sql3)); app .Close ();}} ========================================================================================================================================================================================================== =================================== package com.tzsw.hjyweb.db;
Import java.sql.sqlexception; import java.sql.batchupdateException; import com.tzsw.hjyweb.exception. *; Import com.tzsw.hjyweb.pub.constants;
Public class topcheerdynamicupdate {
PRIVATE BOOLEAN Debug = False;
Private TopcheerConnection; Private TopcheerPreparedStatement TopcheerPreparedStatement
public TopcheerDynamicUpdate () {try {topcheerConnection = new TopcheerConnection (); topcheerPreparedStatement = new TopcheerPreparedStatement (); setCurrentConnection ();} catch (Exception e) {e.printStackTrace ();}} / * public TopcheerDynamicUpdate (boolean debug) {this .debug = debug; try {topcheerConnection = new TopcheerConnection (); topcheerPreparedStatement = new topcheerPreparedStatement (); setCurrentConnection ();} catch (Exception e) {e.printStackTrace ();}} * /
public int doUpdate (String sql) throws Exception {int updateCount = 0; try {updateCount = operate (sql, null);} catch (Exception e) {//System.out.println(e); throw e;} return updateCount }
public int doUpdate (String sql, String [] pstatementParameters) throws Exception {int updateCount = 0; try {updateCount = operate (sql, pstatementParameters);} catch (Exception e) {//System.out.println(e); throw e;} return updatecount;
// Batch Update // Parameters 2D array update number Each parameter array public int [] dobatchupdate (string sql, string [] parameters) throws exception {int updateCount [] = null; try {updateCount = BatchoPERATE (SQL , parameters;} catch (exception e) {//system.out.println (E); throw e;} return updatecount;}
private void setCurrentConnection () throws Exception {try {if (debug) topcheerConnection.setConnection (Constants.DRIVER, Constants.URL, Constants.USER, Constants.PASSWORD); else topcheerConnection.setConnection ();} catch (SQLException e) {e .printstacktrace (); TopcheerConnection.releaseConnection (); throw e;}}
private int operate (String sql, String [] parameters) throws Exception {int updateCount = 0; try {if (topcheerConnection.getConnection () == null) {throw new ConnectFailedException ();.} if (topcheerConnection.getConnection () isClosed ()) setCurrentConnection (); if (topcheerConnection.getSQLWarning () length ()> 0) System.out.println (topcheerConnection.getSQLWarning ());. topcheerConnection.setTransactionIsolation (topcheerConnection.TRANSACTION_READ_COMMITTED); topcheerConnection.setAutoCommit (false); topcheerPreparedStatement.setPreparedStatement (topcheerConnection, sql); if (parameters = null!) {topcheerPreparedStatement.clearParameters (); topcheerPreparedStatement.setString (parameters);} updateCount = topcheerPreparedStatement.executeUpdate (); topcheerConnection.commit (); topcheerConnection.setAutoCommit (true );} Catch (sqlexception e) {E.PrintStackTrace (); system.out.println ("Updat ! E error / n " sql); topcheerConnection.rollback (); topcheerPreparedStatement.releasePreparedStatement (); topcheerConnection.releaseConnection (); throw e;} catch (Exception e) {topcheerConnection.rollback (); topcheerPreparedStatement.releasePreparedStatement (); Topcheerconnection.releaseConnection (); throw e;} return updatecount;}
private int [] batchOperate (String sql, String [] [] parameters) throws Exception {int [] updateCounts = null; try {if (topcheerConnection.getConnection () == null) {throw new ConnectFailedException ();} if (topcheerConnection . .getConnection () isClosed ()) setCurrentConnection (); if (topcheerConnection.getSQLWarning (). length ()> 0) System.out.println (topcheerConnection.getSQLWarning ()); topcheerConnection.setTransactionIsolation (topcheerConnection.TRANSACTION_READ_COMMITTED); topcheerConnection .setAutoCommit (false); topcheerPreparedStatement.setPreparedStatement (topcheerConnection, sql); topcheerPreparedStatement.clearBatch (); for (int i = 0; i SB_WARNINGS.APpend ("Continuing ...."); SQLE = SQLE.GetNextexception ();} system.out.println (sb_warnings.tostring ());} catch (sqlexception e) {system.out.println ("Update ! error / n " sql); topcheerConnection.rollback (); topcheerPreparedStatement.releasePreparedStatement (); topcheerConnection.releaseConnection (); System.out.println (e);} catch (Exception e) {topcheerConnection.rollback (); topcheerPreparedStatement .releasePreparedStatement (); topcheerConnection.releaseConnection (); System.out.println (e);} return updateCounts;} public void close () {try {topcheerPreparedStatement.releasePreparedStatement (); topcheerConnection.releaseConnection ();} catch (Exception e ) {E.PrintStackTrace ();}} Public String PrintUpdateCount (int updatecount) {string message = "Update" UpdateCount "RECORD (s)!"; Return Message; // - 2 public String printUpdateCount (int [] updateCount) {if (updateCount == null) return ""; StringBuffer sb_result = new StringBuffer (); sb_result.append ( "update records are:"); for (int i = 0; i public static void main (String [] args) {TopcheerDynamicUpdate app = new TopcheerDynamicUpdate (); String sql = "insert into web_news (wnnewsid, wnnewstitle) values ( '6', 'ss')"; String sql2 = "insert into web_news (wnnewsid, wnnewstitle) VALUES (?,?) "; string para [] = new string [2]; para [0] =" 7 "; para [1] =" ddd "; string para2 [] [] = new String [3] [2]; Para2 [0] [0] = "8"; Para2 [0] [1] = "SAFS"; Para2 [1] [0] = "9"; Para2 [1] [1 ] = "SAFS"; Para2 [2] [0] = "10"; Para2 [2] [1] = "Safs"; try {// int status = app.doupdate (SQL); //system.out. Println (App.PrintUpdateCount (status); // int status2 = app.doupdate (SQL2, PARA); //system.out.println (app.printupdatecount) ;st (STATUS2)); int [] status3 = app.dobatchupdate (SQL2, Para2); system.out.println (app.printUpdatecount (status3));} catch (exception e) {E.PrintStackTrace ();} app.close ();} } ==================================================== ====== Package com.tzsw.hjyweb.db; Import javax.sql.pooledConnection; import java.sql.connection; import java.sql.databaseMetadata; import java.sql.sqlexception; import java.sql.sqlwarning; public class TopcheerPoolConnection {final int TRANSACTION_NONE = Connection.TRANSACTION_NONE; final int TRANSACTION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED; final int TRANSACTION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED; final int TRANSACTION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ; final int TRANSACTION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE; private PooledConnection poolconnection; private Connection PUBLIC TOPCHEERPOOLCONNECTION () {} public void setConnection () throws SQLException, Exception {connection = poolconnection.getConnection ();} public void setPoolConnection () throws Exception {poolconnection = TopcheerConnectionPoolDataSource.getConnectionPoolDataSource () getPooledConnection ();.} Public connection getConnection () {return Public void setAutocommit (Boolean b) throws sqlexception {connection.setautocommit (b);} Public void commit () throws sqlexception {connection.commit (); Public void rollback () THROWS SQLEXCEPTION {connection.rollback ();} Public void releaseconnection () throws sqlexception {ix (connection! = null&&! connection.isclosed ()) connection.close ();} public void closConnection () throws SQLException {if (poolconnection = null!) poolconnection.close ();} public String getSQLWarning () throws SQLException {SQLWarning sqlwarnings = connection.getWarnings (); StringBuffer sb_warnings = new StringBuffer (); while (sqlwarnings ! = null) {sb_warnings.Append ("ERROR HAPPENED./N"); SB_WARNINGS.APpend ("Message IS" SQLWARNINGS.GETMESSAGE () "/ n"); SB_WARNINGS.APpend ("SqlState IS" Sqlwarnings. GetSqlState () "/ n"); SB_WARNINGS.APpend ("ErrorCode IS" SQLWARNINGS.GETERRORCODE () "/ n"); SB_WARNINGS.APpend ("Continuing ...."); sqlwarnings = sqlwarnings.getNextWarning ( );} return sb_warnings.toString ();} public void setTransactionIsolation (int level) throws SQLException {DatabaseMetaData dbMetaData = connection.getMetaData (); if (dbMetaData.supportsTransactionIsolationLevel (level)) connection.setTransactionIsolation (level);} } ============================================================================================================================================================================================================= ====== Package com.tzsw.hjyweb.db; Import java.sql.preparedStatement; import java.sql.resultset; import java.sql.batchupdateException; import java.sql.sqlexception; public class TopcheerPreparedStatement {final static int CONCUR_READ_ONLY = ResultSet.CONCUR_READ_ONLY; final static int CONCUR_UPDATABLE = ResultSet.CONCUR_UPDATABLE; final static int FETCH_FORWARD = ResultSet.FETCH_FORWARD; final static int FETCH_REVERSE = ResultSet.FETCH_REVERSE; final static int FETCH_UNKNOWN = ResultSet.FETCH_UNKNOWN; final static int TYPE_FORWARD_ONLY = ResultSet.TYPE_FORWARD_ONLY; final static int TYPE_SCROLL_INSENSITIVE = ResultSet.TYPE_SCROLL_INSENSITIVE; final static int TYPE_SCROLL_SENSITIVE = ResultSet.TYPE_SCROLL_SENSITIVE; private PreparedStatement pstatement; public TopcheerPreparedStatement () throws SQLException {} Public void setpreparedStatement (TopcheerConnection TopcheerConnection, String SQL) THROWS SQLEXCEPTION {PStatement = TopcheerConnection.getConnection (). PrepareStatement (SQL); public void setPreparedStatement (TopcheerConnection topcheerConnection, String sql, int resultSetType, int resultSetConcurrency) throws SQLException {pstatement = topcheerConnection.getConnection () prepareStatement (sql, resultSetType, resultSetConcurrency);.} Public preparedStatement GetPreparedStatement () throws sqlexception {return pstate; Public void setString (String [] pStatementParameters) throws sqlexception {for (int i = 0; i Public void clearbatch () throws sqlexception {pstatement.clearbatch (); Public void cleanparameters () throws sqlexception {pstatement.clearparameters (); Public void addbatch () THROWS SQLEXCEPTION {PStatement.Addbatch (); Public boolean execute () throws sqlexcetion {return pStatement.execute (); public boolean Execute (String SQL) throws sqlexception {return pStatement.execute (SQL) Public int [] executebatch () throws batchupdateException, SQLEXCEPTION {Return PStatement.executebatch (); Public ResultSet ExecuteQuery () THROWS SQLEXCEPTION {Return PStatement.executeQuery (); Public ResultSet ExecuteQuery (STRING SQL) THROWS SQLEXCEPTION {Return PStatement.executeQuery (SQL); Public int executeUpdate () throws sqlexception {return pStatement.executeUpdate (); Public int executeUpdate (String SQL) THROWS SQLEXCEPTION {Return PStatement.executeUpdate (SQL); Public void releasepreparedStatement () THROWS SQLEXCEPTION {if (pStatement! = null) pStatement.close ();}} ============================ ========================================================== Package com.tzsw.hjyweb.db; Import java.sql.resultset; import java.sql.sqlexception; import com.tzsw.hjyweb.exception. *; import com.tzsw.hjyweb.pub.constants; Public class topcheerQuery {private boolean debug = false; Private TopcheerConnection TopcheerConnection; Private Topcheerstate Topcheerstate; public TopcheerQuery () {try {topcheerConnection = new TopcheerConnection (); topcheerStatement = new TopcheerStatement (); setCurrentConnection (); setCurrentStatement ();} catch (Exception e) {e.printStackTrace ();}} Public ResultSet DoQuery (String SQL) {ResultSet RS = NULL; Try {RS = Operate (SQL);} catch (connectfailedException e) {system.out.println (e);} catch (exception e) {system.out.println (e);} return rs;} public string doqueryone (String SQL) {String Result = ""; try {resultset = Operate (SQL); if (rs == null ||! rs.next ()) Return Result Result = rs.getstring (1);} catch (connectfailedException e) {system.out.println (e);} catch (exception e) {system.out.println (e);} returnrate;} Public Boolean Hasrecord (String SQL) {Boolean Found = False; Try {ResultSet RS = Operate (SQL); if (RS == Null ||! rs.next ()) Return Found; Found = true;} catch (ConnectFaileDexce) ) {System.out.println (e);} catch (exception e) {system.out.println (e);} returnif; Public void close () {Try {Topcheerstatement.releaseStatement (); TopcheerConnection.releaseConnection ();} catch (exception e) {E.PrintStackTrace ();}} private void setCurrentConnection () throws Exception {try {if (debug) topcheerConnection.setConnection (Constants.DRIVER, Constants.URL, Constants.USER, Constants.PASSWORD); else topcheerConnection.setConnection (); topcheerConnection.setTransactionIsolation (topcheerConnection.TRANSACTION_READ_COMMITTED) ;} Catch (sqlexception e) {E.PrintStackTrace (); Topcheerstatement.releaseStatement (); TopcheerConnection.releaseConnection (); throw e;}} Private void setCurrentStatement () throws exception.setStatement (TopcheerConnection, Topcheerstatement.Type_forward_only, topcheerstatement.concur_read_only);} private void releaseResultSet (ResultSet rs) throws SQLException {if (! rs = null) rs.close ();} private ResultSet operate (String sql) throws ConnectFailedException, Exception {ResultSet rs = null; try {if (topcheerConnection.getConnection () == null) {throw new ConnectFailedException ();}. if (topcheerConnection.getConnection () isClosed ()) {setCurrentConnection (); setCurrentStatement ();} if (topcheerConnection.getSQLWarning (). length ()> 0) System. out.println (topcheerConnection.getSQLWarning ()); //topcheerConnection.setTransactionIsolation(topcheerConnection.TRANSACTION_READ_COMMITTED); //topcheerStatement.setStatement(topcheerConnection,topcheerStatement.TYPE_FORWARD_ONLY,topcheerStatement.CONCUR_READ_ONLY); rs = topcheerStatement.executeQuery (sql);} catch (SQLEXCEPTION E) {system.out.println (SQL); E.PrintStackTrace (); ReleaseResultSet (RS); Topcheerstatement.ReleaseStatement (); topcheerConnection.releaseConnection (); throw e;} catch (Exception e) {releaseResultSet (rs); topcheerStatement.releaseStatement (); topcheerConnection.releaseConnection (); throw e;} return rs;} public static void main (String [] args ) {TOPCHEERQUERY App = New TopcheerQuery (); String SQL = "SELECT * WHER WEB_NEWS"; string SQL2 = "Select * from Web_News Where 1> 2"; ResultSet RS = App.doQuery (SQL); Try {IF (RS! = NULL) {while (rs.next ()) {system.out.println (}}}}}}}}}}}}}}} catch (sqlexception e) {E.PrintStackTrace (); } System.out.println (app.doQueryone (sql)); system.out.println; system.out.println; system.out.println (app) .hasrecord (SQL2)); app.close ();}} =================================== =================================== package com.tzsw.hjyweb.db; import java.sql.statement; import java.sql.resultset; import; import; import; import; import; import; import; import; import; import; import Java.sql.batchupdateException; import java.sql.sqlexception; public class TopcheerStatement {final static int CONCUR_READ_ONLY = ResultSet.CONCUR_READ_ONLY; final static int CONCUR_UPDATABLE = ResultSet.CONCUR_UPDATABLE; final static int FETCH_FORWARD = ResultSet.FETCH_FORWARD; final static int FETCH_REVERSE = ResultSet.FETCH_REVERSE; final static int FETCH_UNKNOWN = ResultSet.FETCH_UNKNOWN; final static int TYPE_FORWARD_ONLY = ResultSet.TYPE_FORWARD_ONLY; final static int TYPE_SCROLL_INSENSITIVE = ResultSet.TYPE_SCROLL_INSENSITIVE; final static int TYPE_SCROLL_SENSITIVE = ResultSet.TYPE_SCROLL_SENSITIVE; Private statement; public Topcheerstate () throws Sqlexception {} Public void setStatement (TopcheerConnection TopcheerConnection) THROWS SQLEXCEERCONNECTION.GETCONNECTION (). Createment (); public void setStatement (TopcheerConnection topcheerConnection, int resultSetType, int resultSetConcurrency) throws SQLException {statement = topcheerConnection.getConnection () createStatement (resultSetType, resultSetConcurrency);.} Public statement getStatement () THROWS SQLEXCEPTION; PUBLIC VOID Addbatch (String SQL) THROWS SQLEXCEPTION {Statement.Addbatch (SQL); Public void clearbatch () throws sqlexception {statement.clearbatch (); Public Boolean Execute (String SQL) THROWS SQLEXCEPTION {Return Statement.execute (SQL) Public int [] executebatch () throws batchupdateException, sqlexception {return statement.executebatch (); Public ResultSet ExecuteQuery (String SQL) THROWS SQLEXCEPTION {Return Statement.executeQuery (SQL); public int executeUpdate (String sql) throws SQLException {return statement.executeUpdate (sql);} public void releaseStatement () throws SQLException {if (! statement = null) statement.close ();} } ==================================================== =============== Package com.tzsw.hjyweb.db; Import java.sql.sqlexception; import java.sql.batchupdateException; import com.tzsw.hjyweb.exception. *; Import com.tzsw.hjyweb.pub.constants; Public class topcheerupdate { PRIVATE BOOLEAN Debug = False; private TopcheerConnection topcheerConnection; private TopcheerStatement topcheerStatement; public TopcheerUpdate () {try {topcheerConnection = new TopcheerConnection (); topcheerStatement = new TopcheerStatement (); setCurrentConnection (); setCurrentStatement ();} catch (Exception e) {e.printStackTrace (); } / * Public TopcheerUpdate (boolean debug) {this.debug = debug; try {topcheerConnection = new TopcheerConnection (); topcheerStatement = new TopcheerStatement (); setCurrentConnection ();} catch (Exception e) {e.printStackTrace ();}} * / public int Doupdate (String SQL) throws exception {int updateCount = 0; try {updateCount = Operate (SQL);} catch (exception e) {system.out.println (e);} return UpdateCount;} // Batch update public int [] doBatchUpdate (String [] sqls) throws Exception {int updateCount [] = null; try {updateCount = batchOperate (sqls);} catch (Exception e) {System.out.println (e);} return updateCount } private void setCurrentConnection () throws Exception {try {if (debug) topcheerConnection.setConnection (Constants.DRIVER, Constants.URL, Constants.USER, Constants.PASSWORD); else topcheerConnection.setConnection (); topcheerConnection.setTransactionIsolation (topcheerConnection.TRANSACTION_READ_COMMITTED) ;} Catch (sqlexception e) {E.PrintStackTrace (); TopcheerConnection.releaseConnection (); throw e;}} Private void setcurrentStatement () throws exception {Topcheerstatement.SetStatement (TopcheerConnection); private int operate (String sql) throws Exception {int updateCount = 0; try {if (topcheerConnection.getConnection () == null) {throw new ConnectFailedException ();} if (. topcheerConnection.getConnection () isClosed ()) {setCurrentConnection (); setCurrentStatement ();} if (topcheerConnection.getSQLWarning () length ()> 0.) System.out.println (topcheerConnection.getSQLWarning ()); //topcheerConnection.setTransactionIsolation(topcheerConnection.TRANSACTION_READ_COMMITTED); topcheerConnection.setAutoCommit ( false); //topcheerStatement.setStatement(topcheerConnection); updateCount = topcheerStatement.executeUpdate (sql); topcheerConnection.commit (); topcheerConnection.setAutoCommit (true);} catch (SQLException e) {System.out.println ( "update error ! / n " SQL); TopcheerConnection.Rollback (); Topcheerstatement.releaseStatement (); TopcheerConnection.releaseConnection (); throw e;} catc H (Exception E) {TopcheerConnection.Rollback (); Topcheerstatement.releaseStatement (); TopcheerConnection.releaseConnection (); throw e;} return updatecount; private int [] batchOperate (String [] sqls) throws Exception {int [] updateCounts = null; try {if (topcheerConnection.getConnection () == null) {throw new ConnectFailedException ();} if (topcheerConnection.getConnection (). isClosed ()) {setCurrentConnection (); setCurrentStatement ();} if (topcheerConnection.getSQLWarning (). length ()> 0) System.out.println (topcheerConnection.getSQLWarning ()); //topcheerConnection.setTransactionIsolation(topcheerConnection.TRANSACTION_READ_COMMITTED ); topcheerConnection.setAutoCommit (false); //topcheerStatement.setStatement(topcheerConnection); topcheerStatement.clearBatch (); for (int i = 0; i } System.out.println (sb_warnings.toString ());} catch (SQLException e) {System.out.println ( "update error / n!"); TopcheerConnection.rollback (); topcheerStatement.releaseStatement (); topcheerConnection. releaseConnection (); System.out.println (e);} catch (Exception e) {topcheerConnection.rollback (); topcheerStatement.releaseStatement (); topcheerConnection.releaseConnection (); System.out.println (e);} return updateCounts ;} public void close () {try {topcheerStatement.releaseStatement (); topcheerConnection.releaseConnection ();} catch (Exception e) {e.printStackTrace ();}} public String printUpdateCount (int updateCount) {String message = "update " UpdateCount " RECORD (S)! "; Return Message;