Java JDBC Database connection encapsulation and instance code __ database

Source: Internet
Author: User
Tags getmessage create database

Java JDBC Feature code encapsulation:

Package com.common.common.util.mysql;  
Import java.sql.CallableStatement;  
Import java.sql.Connection;  
Import Java.sql.DriverManager;  
Import java.sql.PreparedStatement;  
Import Java.sql.ResultSet;  
Import Java.sql.ResultSetMetaData;  
Import java.sql.SQLException;  
Import java.util.ArrayList;  
Import Java.util.HashMap;  
Import java.util.List;  

Import Java.util.Map; public class Connectiondb {/** * Database driver class name/private static final String DRIVER = "Com.mysql.jdbc.Driv  
  
    ER "; /** * Connection string/private static final String urlstr = "Jdbc:mysql://*****?useunicode=true&characterenc  
  
    Oding=utf8 ";  
  
    /** * User name */private static final String USERNAME = "* * *";  
  
    /** * Password/private static final String UserPassword = "* * *";  
  
    /** * CREATE database Connection object/private Connection connnection = null; /** * Create PreparedStatement object/Private PreparedStatement PREparedstatement = null;  
  
    /** * Create CallableStatement object/private CallableStatement callablestatement = null;  
  
    /** * Create result set object/private ResultSet ResultSet = null;  
        static {try {//Load Database driver Class.forName (DRIVER);  
            catch (ClassNotFoundException e) {System.out.println ("Load driver error");  
        System.out.println (E.getmessage ());  
        /** * Establish database connection * @return database connection/public Connection getconnection () {  
                    try {//Get connection connnection = Drivermanager.getconnection (Urlstr, USERNAME,  
        UserPassword);  
        catch (SQLException e) {System.out.println (E.getmessage ());  
    return connnection; 
     /** * INSERT Update Delete SQL statement uniform method * @param SQL SQL statement * @param params parameter array, null if no arguments * @return Number of rows affected/public int executeupdate (String sql, object[] params) {//affected number of rows int Affectedl  
          
        ine = 0;  
            try {//Get connection connnection = This.getconnection ();  
              
            Call SQL PreparedStatement = connnection.preparestatement (sql);  
                    Parameter assignment if (params!= null) {for (int i = 0; i < params.length; i++) {  
                Preparedstatement.setobject (i + 1, params[i]);  
  
        }//Execute Affectedline = Preparedstatement.executeupdate ();  
        catch (SQLException e) {System.out.println (E.getmessage ());  
        finally {//release resource CloseAll ();  
    return affectedline; /** * SQL query puts query results directly into resultset * @param SQL SQL statement * @param params parameter array, null if no argument Return Result set */private ResultSet executequeryrs (String sql, object[] params) {try {//Get connection  
              
            Connnection = This.getconnection ();  
              
            Call SQL PreparedStatement = connnection.preparestatement (sql);  
                    Parameter assignment if (params!= null) {for (int i = 0; i < params.length; i++) {  
                Preparedstatement.setobject (i + 1, params[i]);  
  
        }//Execute ResultSet = Preparedstatement.executequery ();  
        catch (SQLException e) {System.out.println (E.getmessage ());  
    return resultSet;                        /** * Gets the result set and places the result in the list * * @param SQL * SQL statement * @return List * Result set */public list<object> excutequery (String sql, object[] params) {/ /Execute SQL Get result set ResultSet rs = executequeryrs (sql, params);  
          
        Create ResultSetMetaData object ResultSetMetaData RSMD = null;  
        Result set column number int columnCount = 0;  
              
            try {rsmd = Rs.getmetadata ();  
        Get the result set column number ColumnCount = Rsmd.getcolumncount ();  
        catch (SQLException E1) {System.out.println (E1.getmessage ());  
  
        //Create list list<object> list = new arraylist<object> ();  try {//Saves the resultset result to the list while (Rs.next ()) {map<string, object>  
                Map = new hashmap<string, object> ();  
                for (int i = 1; I <= columnCount i++) {map.put (Rsmd.getcolumnlabel (i), rs.getobject (i));  
            } list.add (map); } catch (SQLException e) {System.out.printlN (e.getmessage ());  
        Finally {//Close all resources closeall ();  
    } return list;  /** * Stored procedure method with one output parameter * @param SQL stored Procedure statement * @param params parameter array * @param outparampos  Output parameter position * @param sqltype output Parameter type * @return The value of the output parameter/public Object excutequery (String sql, object[]  
        Params,int outparampos, int sqltype) {object = null;  
        Connnection = This.getconnection ();  
              
            try {//call stored procedure callablestatement = connnection.preparecall (sql);  
                    Assign a value to a parameter if (params!= null) {for (int i = 0; i < params.length; i++) {  
                Callablestatement.setobject (i + 1, params[i]);  }//Register output parameter Callablestatement.registeroutparameter (Outparampos,  
              
            SqlType);  
       Perform     Callablestatement.execute ();  
              
        Get output Parameter object = Callablestatement.getobject (Outparampos);  
        catch (SQLException e) {System.out.println (E.getmessage ());  
        finally {//release resource CloseAll ();  
    return object; /** * Close all resources */private void CloseAll () {//Close result set object if (ResultSet!= nul  
            L) {try {resultset.close ();  
            catch (SQLException e) {System.out.println (E.getmessage ());  
                }///Close PreparedStatement object if (PreparedStatement!= null) {try {  
            Preparedstatement.close ();  
            catch (SQLException e) {System.out.println (E.getmessage ()); }///Close CallableStatement Object if(CallableStatement!= null)  
            {try {callablestatement.close ();  
            catch (SQLException e) {System.out.println (E.getmessage ()); }///Close Connection object if (connnection!= null) {try {C  
            Onnnection.close ();  
            catch (SQLException e) {System.out.println (E.getmessage ());
 }  
        }     
    }  

}

MySQL Instance code calls:

Package com.common.common.util.mysql;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import Com.smart.entity.HomeDevice;
Import Com.smart.entity.HomeDeviceAlarm;
Import Com.smart.entity.HomeDeviceAttrStatu;
Import Com.smart.entity.HomeDeviceCommand;

Import Com.smart.entity.HomeDeviceLog; public class Connectiondbutil {public list

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.