Share _java with Java Connection database (using DAO layer operation data) through proxy class

Source: Internet
Author: User
Tags getdate

First, we define the type of structure to be stored in a Java file:

Copy Code code as follows:

Import Java.util.Date;
/**
*
* @author Nero
*/
public class Emp {
private int empno;
Private String ename;
Private String job;
Private Date hiredate;
private float Sal;
public void setempno (int empno) {
This.empno = empno;
}
public void Setename (String ename) {
This.ename = ename;
}
public void Setjob (String job) {
This.job = job;
}
public void Sethiredate (Date hiredate) {
This.hiredate = hiredate;
}
public void Setsal (float sal) {
This.sal = sal;
}
public int Getempno () {
return this.empno;
}
Public String Getename () {
return this.ename;
}
Public String Getjob () {
return this.job;
}
Public Date gethiredate () {
return this.hiredate;
}
public float getsal () {
return this.sal;
}
}

Below we define a database connection class that is responsible for initiating a connection to the database. Java connection database needs to drive the package, we can download it, the test time I used is mysql-connector-java-5.0.5-bin.jar. When running the program eclipse prompts you to load the database driver package, some are similar to the "Org.gjt.mm.mysql.Driver" standard package, in general, we choose the working directory in the driver package.

Copy Code code as follows:

Import java.sql.Connection;
Import Java.sql.DriverManager;
/**
*
* @author Nero
*/
public class DatabaseConnection {
private static final String Dbdriver = "Org.gjt.mm.mysql.Driver";
private static final String Dburl = "JDBC:MYSQL://LOCALHOST:3306/MLDN";
private static final String Dbuser = "root";
private static final String Dbpassword = "root";
Private Connection Conn;
Public DatabaseConnection () throws Exception {
Class.forName (Dbdriver);
This.conn = Drivermanager.getconnection (Dburl,dbuser,dbpassword);
}
Public Connection getconnection () {
return this.conn;
}
public void Close () throws Exception {
if (this.conn!= null) {
try{
This.conn.close ();
}catch (Exception e) {
Throw e;
}
}
}
}

Next we define an interface that can help us implement the Proxy method easily. There are only three methods in the interface: Insert, find all, and search by ID.

Copy Code code as follows:

Import java.util.*;

/**
*
* @author Nero
*/
Public interface Iempdao {
public boolean docreate (EMP emp) throws Exception;
Public list<emp> FindAll (String keyWord) throws Exception;
Public Emp FindByID (int empno) throws Exception;
}

Then, we inherit this interface, the implementation of specific database operations class, are some very basic database operations, there is nothing to say. The main thing to note is the constructor where the parameter uses the Connection object, which is used later to pass in the connection object returned by the function getconnection () in the previously defined database connection class databaseconnection.

Copy Code code as follows:

Import java.sql.*;
/**
*
* @author Nero
*/
public class Empdaoimpl implements iempdao{
PRIVATE Connection conn = null;
Private PreparedStatement pstmt = null;
Public Empdaoimpl (Connection conn)
{
This.conn = conn;
}
public boolean docreate (EMP emp) throws exception{
Boolean flag = false;
String sql = "INSERT into emp (empno,ename,job,hiredate,sal) VALUES (?,?,?,?,?)";
this.pstmt = this.conn.prepareStatement (sql);
This.pstmt.setInt (1, Emp.getempno ());
This.pstmt.setString (2,emp.getename ());
This.pstmt.setString (3,emp.getjob ());
This.pstmt.setDate (4,new java.sql.Date (Emp.gethiredate (). GetTime ()));
This.pstmt.setFloat (5,emp.getsal ());
if (this.pstmt.executeUpdate () > 0)
{
Flag = true;
}
This.pstmt.close ();
return flag;
}
Public list<emp> FindAll (String keyWord) throws exception{
list<emp> all = new arraylist<emp> ();
String sql = "Select Empno,ename,job,hiredate,sal from emp WHERE ename like?" Or job like? ";
this.pstmt = this.conn.prepareStatement (sql);
This.pstmt.setString (1, "%" +keyword+ "%"); Escape character
This.pstmt.setString (2, "%" +keyword+ "%");
ResultSet rs = this.pstmt.executeQuery (SQL);
EMP emp = NULL;
while (Rs.next ())
{
EMP = new EMP ();
Emp.setempno (Rs.getint (1));
Emp.setename (rs.getstring (2));
Emp.setjob (Rs.getstring (3));
Emp.sethiredate (Rs.getdate (4));
Emp.setsal (Rs.getfloat (5));
All.add (EMP);
}
This.pstmt.close ();
return all;
}
Public Emp FindByID (int empno) throws exception{
EMP emp = NULL;
String sql = "Select Empno,ename,job,hiredate,sal from emp WHERE empno=?";
this.pstmt = this.conn.prepareStatement (sql);
This.pstmt.setInt (1,empno);
ResultSet rs = This.pstmt.executeQuery ();
if (Rs.next ()) {
EMP = new EMP ();
Emp.setempno (Rs.getint (1));
Emp.setename (rs.getstring (2));
Emp.setjob (Rs.getstring (3));
Emp.sethiredate (Rs.getdate (4));
Emp.setsal (Rs.getfloat (5));
}
This.pstmt.close ();
return EMP;
}
}

Below we look at the implementation of the proxy class, the individual feel here is more interesting. In this class, we declare a database connection Class DatabaseConnection object, a database application Class Empdaoimpl object, Initialize Empdaoimpl object with DatabaseConnection object, The Empdaoimpl object is then used in each function of the proxy class to invoke a method inherited from the same interface, so that the specific implementation method is hidden to some extent.

Copy Code code as follows:

Import java.sql.*;
/**
*
* @author Nero
*/
public class Empdaoproxy implements iempdao{
Private databaseconnection DBC = null;
Private Empdaoimpl DAO = null;
Public Empdaoproxy () throws exception{
THIS.DBC = new DatabaseConnection ();
This.dao = new Empdaoimpl (This.dbc.getConnection ());
}
public boolean docreate (EMP emp) throws exception{
Boolean flag = false;
try{
if (This.dao.findById (Emp.getempno ()) = = null)
{
Flag = this.dao.doCreate (EMP);

}
}catch (Exception e)
{
Throw e;
}finally{
This.dbc.close ();
}
return flag;
}
Public list<emp> FindAll (String keyWord) throws exception{
List<emp> all = null;
try{
all = This.dao.findAll (KeyWord);
}catch (Exception e) {
Throw e;
}finally{
This.dbc.close ();
}
return all;
}
Public Emp FindByID (int empno) throws exception{
EMP emp = NULL;
try{
EMP = This.dao.findById (empno);
}catch (Exception e) {
Throw e;
}finally{
This.dbc.close ();
}
return EMP;
}
}

This is not all, we can add a factory class to implement the Factory mode:

Copy Code code as follows:

/**
*
* @author Nero
*/
public class Daofactory {
public static Iempdao Getiempdaoinstance () throws exception{
return new Empdaoproxy ();
}
}

What is the use of this factory class? Finally, we call in the main class file to see what the factory class does:

Copy Code code as follows:

/**
*
* @author Nero
*/
public class Testdaoinsert {
public static void Main (String args[]) throws exception{
EMP emp = NULL;
for (int x=0;x<5;x++) {
EMP = new EMP ();
Emp.setempno (1000 + x);
Emp.setename ("Chinese Display test-" + x);
Emp.setjob ("Programmer-" + x);
Emp.sethiredate (New Java.util.Date ());
Emp.setsal (* x);
Daofactory.getiempdaoinstance (). docreate (EMP);
}
}
}

It can be seen that the specific implementation methods are better hidden, through the factory class call get method to obtain the proxy class, the proxy class calls the specific method, and then by the proxy class specific operation object to invoke the concrete operation method.

In fact, these things seem to feel very simple, but they often forget the design. Mainly because it is not used much, at first always forced yourself to think up to do, and then slowly become a habit.

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.