Database operations of two DAO in the Java DAO pattern (Basedao) __c#

Source: Internet
Author: User

The

DAO pattern is a Java-oriented design-time pattern. The DAO in the Basedao is nothing more than the data curd (add a check), the following list of two commonly used connections, additions and deletions to change the method.
Condition One:

Mport Java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException;
    public class Basedao {private final String driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
    Private final String url= "Jdbc:sqlserver://localhost:1433;databasename=myjava";
    Private final String USERNAME = "sa";



    Private final String PWD = "sa";
    Private Connection Connection;
    Private PreparedStatement PS;

    protected ResultSet RS;
            public void getconnection () {try {class.forname (DRIVER);
        2 Find the database in the network, create the Connection object connection = Drivermanager.getconnection (URL, username,pwd);
        catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }//return conNection;
            public void CloseAll () {try {//Release resource if (RS!= null) rs.close ();
            if (PS!= null) ps.close ();
        if (connection!= null) connection.close ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }/** * Query * @param SQL * @param params/public void executequery (String sql, OBJEC
            T[] params) {try {getconnection ();
            PS = connection.preparestatement (SQL); if (params!= null) {for (int i = 0; i < params.length i++) {ps.setobject (I+1, para
                Ms[i]);
        rs = Ps.executequery ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }/** * Query * @param SQL *PARAM params */public void ExecuteQuery1 (String sql, Object ... params) {try {getconnection
            ();
            PS = connection.preparestatement (SQL); if (params!= null) {for (int i = 0; i < params.length i++) {ps.setobject (I+1, para
                Ms[i]);
        rs = Ps.executequery ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }/** * Add, delete, change * @param SQL * @param params * @return * * public int executeupdate (S
        Tring SQL, object[] params {int result =-1;
            try {getconnection ();
            PS = connection.preparestatement (SQL); if (params!= null) {for (int i = 0; i < params.length i++) {ps.setobject (I+1, para
                Ms[i]);
   } result= ps.executeupdate ();     catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();

        }finally{This.closeall ();
    return result; /** * Add, delete, change * @param SQL * @param params * @return * * public int executeUpdate1 (Strin
        G sql, Object ... params) {int result =-1;
            try {getconnection ();
            PS = connection.preparestatement (SQL); if (params!= null) {for (int i = 0; i < params.length i++) {ps.setobject (I+1, para
                Ms[i]);
        } result= ps.executeupdate ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();

        }finally{This.closeall ();
    return result;
 }

}

condition Two:

Package DAO;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException;
    public class BaseDao1 {private final String driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
    Private final String url= "Jdbc:sqlserver://localhost:1433;databasename=myjava";
    Private final String USERNAME = "sa";

    Private final String PWD = "sa";
    protected Connection Connection;

    protected PreparedStatement PS;
            Public Connection getconnection () {try {class.forname (DRIVER);
        2 Find the database in the network, create the Connection object connection = Drivermanager.getconnection (URL, username,pwd);
        catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); Return ConnectioN
            } public void CloseAll (ResultSet rs, PreparedStatement PS, Connection conn) {try {//Free resources
            if (Rs!= null) rs.close ();
            if (PS!= null) ps.close ();
        IF (conn!= null) Conn.close ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
        } public ResultSet executequery (String sql, object[] params) {ResultSet rs = null;
            try {getconnection ();
            PS = connection.preparestatement (SQL); if (params!= null) {for (int i = 0; i < params.length i++) {ps.setobject (I+1, para
                Ms[i]);
        rs = Ps.executequery ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
    } return RS; /** * Add, delete, change
     * @param SQL * @param params * @return/public int executeUpdate1 (String sql, Object ... params
        {int result =-1;
            try {getconnection ();
            PS = connection.preparestatement (SQL); if (params!= null) {for (int i = 0; i < params.length i++) {ps.setobject (I+1, para
                Ms[i]);
        } result= ps.executeupdate ();
        catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();

        }finally{This.closeall (null,ps,connection);
    return result;
 }

}

Both of these methods are only used in a personal style. This is just the most basic code. Ok. At least one knowledge point a day to this.

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.