The DBHelper of JAVA-JDBC

Source: Internet
Author: User

Package Com.myit.util;import Java.lang.reflect.field;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.list;public class DBHelper {private static final Str    ing driver= "COM.MYSQL.JDBC";    private static final String url= "Jdbc:mysql://localhost:3306/dvd";    private static final String user= "root";        private static final String password= "root";        /** * Connect database * @return Linked Database object */public Connection getconnection () {Connection conn=null;        try {class.forname (DRIVER);        } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();        } try {conn=drivermanager.getconnection (URL, USER, PASSWORD); } catch (SQLException e) {//TODO auto-generated catch block e.printstAcktrace ();    } return conn; /** * Release the appropriate resources * @param RS * @param pstmt * @param conn */public void CloseAll (ResultSet r            S,preparedstatement pstmt,connection conn) {try {if (rs!=null) {rs.close ();            } if (Pstmt!=null) {pstmt.close ();            } if (Conn!=null) {conn.close ();        }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }}/** * This method can be done to add and revise all operations * @param SQL * @param params * @return true or false */public        The number of rows affected by the Boolean excuteupdate (String sql,list<object> params) {int res=0;//Connection conn=null;        PreparedStatement pstmt = null;        ResultSet Rs=null;            try {conn=getconnection (); Pstmt=conn.preparestatement (SQL);//Load SQL statement if (params!=null) {//join have? Placeholderbit, before execution?                Placeholder replace for (int i=0;i<params.size (); i++) {Pstmt.setobject (i+1, Params.get (i));                    }} res=pstmt.executeupdate ();        } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();        }finally{CloseAll (RS, PSTMT, conn);    } return res>0?true:false; }/** * encapsulated using generic method and reflection mechanism * @param SQL * @param params * @param CLS * @return */Public &L T T> list<t> executeQuery (String sql,list<object> params,class<t> cls) throws exception{Connect        Ion Conn=null;        PreparedStatement pstmt = null;        ResultSet Rs=null;        List<t> data=new arraylist<t> ();            try {conn=getconnection (); Pstmt=conn.preparestatement (SQL);//Load SQL statement if (params!=null) {//join have? placeholder, put in before executing? Placeholder replace for (int i=0;i<parAms.size (); i++) {Pstmt.setobject (i+1, Params.get (i));            }} rs=pstmt.executequery ();            The query is encapsulated into the corresponding entity class object ResultSetMetaData Rsd=rs.getmetadata ();//Get the Column object, this object can get the structure of the table, including, column name, number of columns, the data type of the column                while (Rs.next ()) {T m=cls.newinstance ();                    for (int i=0;i<rsd.getcolumncount (); i++) {String col_name=rsd.getcolumnname (i+1);//Get Column name                    Object Value=rs.getobject (col_name);//Gets the value of the column corresponding to Field Field=cls.getdeclaredfield (col_name);                 Field.setaccessible (TRUE);//Give the private property an accessible Field.set (m, value);//Assign a value to the object's private property}            Data.add (m); }} catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace        ();        }finally{CloseAll (RS, PSTMT, conn);    } return data; }} 

  

The DBHelper of JAVA-JDBC

Related Article

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.