JDBC Four (Web Foundation learning Note 10)

Source: Internet
Author: User

First, add, modify, delete, query

Integrate functionality into a class

 Packagepb.base;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportPb.until.ConfigManager; Public classBase { PublicConnection Conn;  PublicPreparedStatement pstmt;  PublicStatement stmt;  PublicResultSet rs; //Establish a connection     Public Booleangetconnection () {//get driver,url, user name passwordString Driver = Configmanager.getintance (). GetValue ("Jdbc.driver"); String URL= Configmanager.getintance (). GetValue ("Jdbc.url"); String uname= Configmanager.getintance (). GetValue ("Jdbc.uname"); String pwd= Configmanager.getintance (). GetValue ("Jdbc.upwd"); //(1) Load driver with Class.forName ()        Try{class.forname (driver); //(2) drivermanager.getconnection (URL, user name, password) Get the connection to the database connection//Oracle Connection Mode Jdbc:oracle:thin: @localhost: 1521:orclconn =drivermanager.getconnection (URL, uname, pwd); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }        return true; }    //Add, modify, delete     Public intexecuteupdate (String sql,object [] pararm) {intUpdaterow=0;        Getconnection (); Try{pstmt=conn.preparestatement (SQL); //iterate through an array to set each value             for(inti=0;i<pararm.length;i++) {Pstmt.setobject (i+1, Pararm[i]); } UpdateRow=pstmt.executeupdate (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnUpdateRow; }          //Enquiry     PublicResultSet ExecuteSQL (String sql,object [] pararm) {getconnection (); Try{pstmt=conn.preparestatement (SQL); //iterate through an array to set each value             for(inti=0;i<pararm.length;i++) {Pstmt.setobject (i+1, Pararm[i]); } RS=Pstmt.executequery (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnrs; }            //To close a database connection     Public BooleanCloseConnection () {Try {                if(rs!=NULL) {rs.close (); }            } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }            Try {            if(stmt!=NULL) {stmt.close (); }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }        Try {            if(pstmt!=NULL) {pstmt.close (); }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }        Try {            if(conn!=NULL) {conn.close (); }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }    return true; }}

JDBC Four (Web Foundation learning Note 10)

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.