Database access Layer One

Source: Internet
Author: User

Entity (solid Class):

/**

Serialization of *seralizable

*

/

public class Mas implements serializable{
/**
* Privatization Properties
*/
Private static final long serialversionuid = 1L;
private int id;
Private String loginID;
private String password;
Public Mas () {

}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}

Public String GetLoginId () {
return loginID;
}
public void Setloginid (String loginid) {
This.loginid = loginID;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}

}

Utils (Help Class)

Public class Basedao {
Public Connection getconnection () {
ResultSet rs=null;
PreparedStatement pst=null;
Connection conn=null;
try {
class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "Jdbc:sqlserver://localhost:1433;databasename=epet";
String uname = "sa";
String pwd = "123456";
conn = drivermanager.getconnection (url,uname,pwd);
} catch (ClassNotFoundException ee) {
ee.printstacktrace ();
}catch (SQLException e) {
e.printstacktrace ();
  }
return conn;
 } 

/* * Modify, delete, add */

public void Exectue (String sql,object [] param) {

ResultSet Rs=null;

PreparedStatement Pst=null;

Connection Conn=null;

try {

Conn=getconnection ();

Pst=conn.preparestatement (SQL);

for (int i=0;i<param.length;i++) {

Pst.setobject (i+1, param[i]);

}

int num=pst.executeupdate ();

} catch (Exception ee) {

Ee.printstacktrace ();

}

}

/*
* Close Database connection
* Param Conn Database connection
* param stmt Statement Object
* param RS Result set
*
*/
public void Close (ResultSet RS, PreparedStatement PST, Connection conn) {
try{
if (rs!=null) {
Rs.close ();
}
if (pst!=null) {
Pst.close ();
}
if (conn!=null) {
Conn.close ();
}
}catch (SQLException eee) {
Eee.printstacktrace ();
}
}

}

DAO layer

/*
* Interface
*/

Public interface Masdao {
/*
* Login
* @param m Injection object: mainly ID and password
* @return Master Object
*/
Public Mas Login (mas m);
}

/*
* Inherit helper class to Implement Interface
*/
public class Masdaoimpl extends Basedao implements Masdao {
Public Mas Login (mas m) {
PreparedStatement PST =null;
ResultSet Rs=null;
Connection conn=getconnection ();
Mas Ms=null;
String sql= "SELECT * from Mas where loginid=? and password=? ";
try{
Pst=conn.preparestatement (SQL);
Pst.setstring (1, M.getloginid ());
Pst.setstring (2, M.getpassword ());
Rs=pst.executequery ();
while (Rs.next ()) {
Ms=new Mas ();
Ms.setid (Rs.getint ("id"));
Ms.setloginid (rs.getstring ("loginID"));
Ms.setpassword (rs.getstring ("password"));
}
}catch (SQLException e) {
E.printstacktrace ();
}finally{
Close (Rs,pst,conn);

}
return m;
}

}

Biz Layer

/*
* Interface
*/

Public interface Masbiz {
/*
* Login
*/
Public Mas Login (mas m);
}

/*
* Implementation Interface
*/

public class Masbizlmpl implements masbiz{
Masdao dao=new Masdaoimpl ();
Business layer: Simple business, one SQL
Public Mas Login (mas m) {
Access Layer
Mas Mm=dao.login (m);
return mm;
}
}

Controller (Control layer) <web>

public class Mascontroller {
Page jump, data encapsulation
Public Mas Login (string loginId, string password) {
masbiz MB = new Masbizlmpl ();
Mas m=new Mas ();
M.setloginid (LOGINID);
M.setpassword (password);
Mas Mm=mb.login (m);
return mm;
}

Database access Layer One

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.