The basic way of the Java Operations database in 3

Source: Internet
Author: User
Tags stmt

JDBC-Extracted tool classes

Package utils;

Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;

Import com.mysql.jdbc.Statement;

public class Jdbcutils {
private static final String Driverclass;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
static{
Properties P = new properties ();
try {
P.load (New FileInputStream ("Src/jdbc.properties"));
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
Driverclass = P.getproperty ("Driverclass");
url = p.getproperty ("url");
USERNAME = P.getproperty ("USERNAME");
PASSWORD = P.getproperty ("PASSWORD");
}
/**

  • Load Driver
    */
    public static void Loaddriver () {
    try {
    Load Driver
    Class.forName (Driverclass);
    } catch (ClassNotFoundException e) {
    E.printstacktrace ();
    }
    }
    /**
  • Get connection, return connection
  • @return
    */
    public static Connection getconnection () {
    Calling a load-driven method
    Loaddriver ();
    Connection conn = null;
    try {
    Get connections
    conn = Drivermanager.getconnection (URL, USERNAME, PASSWORD);
    } catch (SQLException e) {
    E.printstacktrace ();
    }
    Return conn;
    }
    /**
  • Freeing resources
  • @param stmt
  • @param Conn
    */
    public static void Release (Statement stmt,connection conn) {
    if (stmt! = null) {
    try {
    Stmt.close ();
    } catch (SQLException e) {
    E.printstacktrace ();
    }
    }
    IF (conn! = null) {
    try {
    Conn.close ();
    } catch (SQLException e) {
    E.printstacktrace ();
    }
    }
    }

    /**

  • Freeing resources
  • @param stmt
  • @param Conn
    */
    public static void Release (ResultSet rs,statement stmt,connection conn) {
    if (rs! = null) {
    try {
    Rs.close ();
    } catch (SQLException e) {
    E.printstacktrace ();
    }
    }
    if (stmt! = null) {
    try {
    Stmt.close ();
    } catch (SQLException e) {
    E.printstacktrace ();
    }
    }
    IF (conn! = null) {
    try {
    Conn.close ();
    } catch (SQLException e) {
    E.printstacktrace ();
    }
    }
    }
    }

Link pool and JDBC federated extracted tool classes
Package utils;

Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;

Import Javax.sql.DataSource;

Import Org.apache.commons.dbcp.BasicDataSourceFactory;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;
Import com.mysql.jdbc.Statement;

public class Jdbcutils {

/** * Get connection, return connection is Apache * @return */public static Connection getdbcpconnection () {Connection Connection = null;        try {Properties Pro = new Properties ();        Pro.load (New FileInputStream ("Src\\dbcpconfig.properties"));        DataSource ds = Basicdatasourcefactory.createdatasource (pro);    Connection = Ds.getconnection ();//Enhanced Connetion} catch (Exception e) {e.printstacktrace (); } return connection;} /** * Get connection, return connection not Apache * configuration file name is fixed c3p0-config.xml and placed in SRC directory *    Combopooleddatasource will automatically go to the SRC directory to find the C3p0-config.xml configuration file * @return */public static Connection getc3p0connection () {    Connection Connection = null; try {DataSource ds =new Combopooleddatasource ();//The parameter of this place is matching the configuration file, if not written, the default connection = Ds.getconnection ()    ;//Enhanced Connetion} catch (Exception e) {e.printstacktrace (); } return connection;} /** * Release Resources * @param stmt * @param conn enhanced */public static void release (Statement stmt,connection conn) {if (stmt! = Null) {try {stmt.close ();        } catch (SQLException e) {e.printstacktrace ();        }} if (conn! = null) {try {//has become returned ... conn.close ();        } catch (SQLException e) {e.printstacktrace (); }}}/** * Frees resources * @param stmt * @param conn enhanced */public static void release (ResultSet rs,statement stmt,connection con        N) {if (rs! = null) {try {rs.close ();        } catch (SQLException e) {e.printstacktrace ();        }} if (stmt! = null) {try {stmt.close ();        } catch (SQLException e) {e.printstacktrace ();            }} if (conn! = null) {try {//has modified the close (), it was destroyed, and now the method becomes the return connection.        Conn.close ();        } catch (SQLException e) {e.printstacktrace (); }    }}

}

Dbutis using the Import package

Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.util.Date;

Import Javax.sql.DataSource;

Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.ResultSetHandler;
Import Org.junit.Test;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;
/**

  • Because: Before Dbutis is used
  • (1) Database link object, SQL statement action object, encapsulate result set object, these three large objects will be defined repeatedly
    (2) Code duplication of encapsulated data, complex operation and large code size
    (3) Code duplication for releasing resources
    Results: (1) When the programmer is developing, there is a lot of repetitive work. (2) long cycle of development, low efficiency
    So Apache developed a set of tool classes that encapsulate JDBC that Dbutis provides programmers with
  • @author Wangshuang
  • */
    public class Test {
    DataSource ds = new Combopooleddatasource ();br/> @Test

    /**

      1. Create the Queryrunner class first
      1. Call the update () method to complete the redaction operation
        /
        Queryrunner qr = new Queryrunner (DS);
        try {
        Add to
        Qr.update ("INSERT into product values (null,?,?,?)", "pear", 3.5,new Date ());
        } catch (SQLException e) {br/>e.printstacktrace ();
        }
        }
        @Test

        Queryrunner qr = new Queryrunner ();
        try {
        Qr.query ("Select
        from product where pid =?", new Myresultsethandler (), 1);
        } catch (SQLException e) {
        E.printstacktrace ();
        }
        }
        }
        /**
  • Similar Beanhandler
  • @author Wangshuang
  • */
    Class Myresultsethandler implements Resultsethandler<product>{br/> @Override

    Product P = new product ();
    P.setpid (Arg0.getint (1));
    P.setpname (arg0.getstring (2));
    P.setprice (Arg0.getdouble (3));
    P.setpdate (Arg0.getdate (4));
    return p;
    }
    }

The basic way of the Java Operations database in 3

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.