Implement efficient and Easy-to-use Java operations MySQL Packaging

Source: Internet
Author: User
Tags int size

In order to simplify some operations and to be compatible with other databases, it does not use the form of SqlHelper.

This is just a simple model of the development of the way, not to include data connection pool and other content.

I have seen most of the sqlhelper on the Internet are very problematic in terms of type conversion, and the return result is packaged with ArrayList. The packaging here is mainly to avoid these two problems.

First, the database interface is declared, which represents the operation that a database can perform.

package dao;
import java.sql.SQLException;
public interface Database {
     int ExecuteNoneQuery(String cmdtext, Parameters parms)  throws SQLException;
     <T> T ExecuteObject(Data2Object<T> convertor, String  cmdtext,
             Parameters parms) throws  SQLException;
     Object ExecuteScalar(String cmdtext, Parameters parms)  throws SQLException;
     Parameters CreateFixedParms(int size);
}

The implementation of the interface of the MySQL packaging form, in fact, and SqlHelper almost:

Package DAO;


import java.sql.Connection;


import java.sql.Date;


import Java.sql.DriverManager;


import java.sql.PreparedStatement;


import Java.sql.ResultSet;


Import java.sql.SQLException;


public class Mysqldatabase implements Database {


private Connection Conn;


public mysqldatabase (String connstring) throws SQLException {


conn = drivermanager.getconnection (connstring);


     }


public int Executenonequery (String cmdtext, Parameters parms)


throws SQLException {


PreparedStatement pstmt = null;


try {


pstmt = conn.preparestatement (Cmdtext);


PrepareCommand (pstmt, parms);


return pstmt.executeupdate ();


} catch (Exception ex) {


} finally {


if (pstmt!= null) {


pstmt.clearparameters ();


Pstmt.close ();


}


if (conn!= null)


Conn.close ();


         }


return-1;


     }


public &lt;T&gt; T executeobject (data2object&lt;t&gt; convertor, String Cmdtext,


Parameters parms) throws SQLException {


PreparedStatement pstmt = null;


ResultSet rs = null;


try {


pstmt = conn.preparestatement (Cmdtext);


PrepareCommand (pstmt, parms);


rs = Pstmt.executequery ();


return convertor. Datamap (RS);


} catch (Exception ex) {


} finally {


if (Rs!= null)


Rs.close ();


if (pstmt!= null)


Pstmt.close ();


if (conn!= null)


Conn.close ();


         }


return null;


     }


public Object ExecuteScalar (String cmdtext, Parameters parms)


throws SQLException {


PreparedStatement pstmt = null;


ResultSet rs = null;


try {


pstmt = conn.preparestatement (Cmdtext);


PrepareCommand (pstmt, parms);


rs = Pstmt.executequery ();


if (Rs.next ()) {


return Rs.getobject (1);


} else {


return null;


             }


} catch (Exception e) {


} finally {


if (Rs!= null)


Rs.close ();


if (pstmt!= null)


Pstmt.close ();


if (conn!= null)


Conn.close ();


         }


return null;


     }


private void PrepareCommand (PreparedStatement pstmt, Parameters parms)


throws SQLException {


if (parms!= null &amp;&amp; parms.getlength () &gt; 0) {


for (int i = 0; i &lt; parms.getlength (); i++) {


mysqlparameter parm = Parms.getparameter (i);


String value = Parm.getvalue (). toString ();


switch (Parm.gettype ()) {


Case String:


pstmt.setstring (i + 1, value);


Break


Case Int16:


Pstmt.setshort (i + 1, short.parseshort (value));


break;


Case INT32:


Pstmt.setint (i + 1, integer.parseint (value));


break;


Case Int64:


Pstmt.setlong (i + 1, Long.parselong (value));


break;


Case DateTime:


pstmt.setdate (i + 1, date.valueof (value));


break;


Default:


Pstmt.setobject (i + 1, value);


break;


                 }


             }


         }


     }


Static {


try {


class.forname ("Com.mysql.jdbc.Driver"). newinstance ();


} catch (Exception ex) {


         }


     }


public Parameters createfixedparms (int size) {


return new fixedparameters (size);


     }


}

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.