Basedao to manually establish a database connection

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.