Custom persistence Layer (DAO) framework

Source: Internet
Author: User
Tags stmt
2. Custom persistence Layer (DAO) framework

Update case: (Insert/update/delete)

1. Load Driver

2. Get connection

3. Create a Pstmt object

4. Implement Sql:pstmt. executeupdate ();

5. Close

Query: (SELECT)

1. Load Driver

2. Get connection

3. Create a Pstmt object

4. Implement Sql:pstmt. ExecuteQuery ();

5. Get the result set object: Encapsulate the object, add it to the collection, and then return

6. Close

Optimization to implement some common methods:
1. Get connection

1. Close the connection

2. Update

3. Query

Meta Data:

Concept: The definition of the database itself, such as database, table, column information

Classification:

1. Database meta data

2. Parameter meta data

3. Result set meta data

JDBC Universal class:

/**

* Handle all operations with the database through a Basedao

* 1. General methods of querying

* 2. Common methods of updating

* @author Administrator

*

*/

Public class Basedao {

Connection objects

Connection con= null;

PreparedStatement pstmt = null;

ResultSet rs = null;

C3P0 Data Source Creation

private static DataSource ds;

Static {

ds = new combopooleddatasource ();

}

/**

* Common methods for implementing queries

* @param<T> Incoming object types, when calling this method, determine the type

* @param sql to query

* @param clazz The Incoming object type, the byte code

* @param The value of the Paramvalue placeholder (query condition)

* @return

*/

Public <T> list<t> query (String sql,object[] paramvalue,class<t> clazz) {

The returned data

list<t> list = new arraylist<t> ();

Try {

1. Get connection

con = ds.getconnection ();

2. Creating stmt Objects

pstmt = con.preparestatement (sql);

Get the number of placeholders by parameter metadata

int count = Pstmt.getparametermetadata (). GetParameterCount ();

for (int i=0; i<count; i++) {

Set parameter values .....

Pstmt.setobject (i+1, paramvalue[i]);

}

3. Get result set

rs = Pstmt.executequery ();

Get result set meta data

ResultSetMetaData RSMD = Rs.getmetadata ();

Get the number of columns from the result set meta data

int columnCount = Rsmd.getcolumncount ();

Traverse result set

while (Rs.next ()) {

Creating objects

T t = clazz.newinstance ();

Iterate through each column of the current row

for (int i=0; i<columncount; i++) {

Column Name "Object Properties"

String columnName = Rsmd.getcolumnname (i+1);

Column Value "Object property, corresponding value"

Object Columnvalue = Rs.getobject (columnName);

Copies the specified value to an object of the specified name by beanutils

Beanutils.copyproperty (t, ColumnName, Columnvalue);

}

Add the Encapsulated object (t) to the collection

List.add (t);

}

Catch (Exception e) {

throw New RuntimeException (e);

finally {

this. CloseAll (Con, pstmt, RS);

}

return list;

}

/**

*

* @param SQL-Updated SQL statement

* @param The value of the Paramvalues SQL statement parameter

*/

public int update (String sql,object[] paramvalues) {

Try {

1. Get connection

con = ds.getconnection ();

2. Creating stmt Objects

pstmt = con.preparestatement (sql);

Get parameter metadata

int count = Pstmt.getparametermetadata (). GetParameterCount ();

for (int i=0; i<count; i++) {

Set parameter values

Pstmt.setobject (i+1, paramvalues[i]);

}

3. Execute SQL, returning the number of affected rows

return pstmt.executeupdate ();

Catch (Exception e) {

throw New RuntimeException (e);

finally {

this. CloseAll (Con, pstmt, null);

}

}

Public void closeall (Connection con, Statement stmt, ResultSet rs) {

Try {

if (RS!= null) {

Rs.close ();

rs = null;

}

if (stmt!= null) {

Stmt.close ();

stmt = null;

}

if (Con!= null) {

Con.close ();

con = null;

}

Catch (SQLException e) {

throw New RuntimeException (e);

}

}

}

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.