PackageCom.chinasoft.julong.dao;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classBasedao {PrivateConnection Conn; PrivateResultSet rst; PrivatePreparedStatement PST; String ClassName= "Oracle.jdbc.OracleDriver"; String Usename= "Oracle"; String Password= "Oracle"; String URL= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"; //Load Database driver PublicBasedao () {Try{class.forname (className); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //establishing a database connection Public voidgetconnection () {Try{conn=drivermanager.getconnection (Url,usename,password); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //function with query result set without parameters (query) PublicResultSet ExecuteQuery (String sql) {returnExecuteQuery (SQL,Newobject[]{}); } //function with a parameter with a query result set (query) PublicResultSet ExecuteQuery (String sql,object[] arry) {getconnection (); Try{PST=conn.preparestatement (SQL); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } for(inti=0;i<arry.length;i++){ Try{pst.setobject (i+1, Arry[i]); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Try{rst=Pst.executequery (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnrst; } //update function without parameters (add, modify, delete) Public intexecuteupdate (String sql) {returnExecuteupdate (SQL,Newobject[]{}); } //update function with parameters (add, modify, delete) Public intexecuteupdate (String sql,object[] arry) {getconnection (); intResult=0; Try{PST=conn.preparestatement (SQL); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } for(inti=0;i<arry.length;i++){ Try{pst.setobject (i+1, Arry[i]); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Try{result=pst.executeupdate (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Closed (); returnresult; } //Close Database Operations Object Public voidClosed () {Try { if(rst!=NULL&&!rst.isclosed ()) {Rst.close (); } if(!pst.isclosed ()) {Pst.close (); } if(!conn.isclosed ()) {Conn.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}View Code
Basedao to manually establish a database connection