What DAO mode?
Stands for Data Access Object, Data Access Objects.
What is the role model is the DAO?
The project is divided into three layers: a data layer, a service layer, an interaction layer, such that the business logic becomes clear.
DAO mode components:
interface class , implement the interface class , the entity class , the database tools
Let the following step by step:
1. interface class
add an interface class to the project: IDAO.java
package main;
import java.util.Map;
/**
* @author szymou
* @DAO接口类
*/
public interface IDAO {
public void insert(Student student) throws Exception;//增
public void delete(Student student) throws Exception;//删
public void query(Map<String,String> condition) throws Exception;//查
public void update(Student student) throws Exception;//改
}
2. The class implements the interface
to add implementation class file: Use_DaoImpl.java
package main;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
/**
* @author szymou 接口实现类
*/
public class Use_DaoImpl implements IDAO {
@Override
public void insert(Student student) throws Exception {
// TODO 自动生成的方法存根
Connection connection = Con_db.getConnection();
String insertsql = "insert into STUDENTINFO (STUDENTID,STUDENTNAME,SEX,DEPARTMENTCODE)values(?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertsql);// 创建执行sql语句对象
preparedStatement.setString(1, student.getStudentid());// 对应上面语句的第1个问号
preparedStatement.setString(2, student.getStudentname());// 对应上面语句的第2个问号
preparedStatement.setString(3, student.getSex());// 对应上面语句的第3个问号
preparedStatement.setString(4, student.getDepartmentcode());// 对应上面语句的第4个问号
preparedStatement.executeQuery();// 执行sql语句,插入数据
System.out.println("已插入学生ID为" + student.getStudentid() + "的数据");
Con_db.closeALL(connection, preparedStatement, null);// 关闭数据库
System.out.println("已关闭数据库");
}
@Override
public void delete(Student student) throws Exception {
// TODO 自动生成的方法存根
Connection connection = Con_db.getConnection();
String deletesql = "delete STUDENTINFO where STUDENTID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deletesql);// 创建执行sql语句对象
preparedStatement.setString(1, student.getStudentid());// 对应上面语句的第1个问号
preparedStatement.executeQuery();// 执行sql语句,删除数据
System.out.println("已删除学生ID为" + student.getStudentid() + "的数据");
Con_db.closeALL(connection, preparedStatement, null);// 关闭数据库
System.out.println("已关闭数据库");
}
@Override
public void update(Student student) throws Exception {
// TODO 自动生成的方法存根
Connection connection = Con_db.getConnection();
String updatesql = "update STUDENTINFO set STUDENTNAME = ? where STUDENTID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updatesql);// 创建执行sql语句对象
preparedStatement.setString(1, student.getStudentname());// 对应上面语句的第1个问号
preparedStatement.setString(2, student.getStudentid());// 对应上面语句的第2个问号
preparedStatement.executeQuery();// 执行sql语句,更新数据
System.out.println("已更新学生ID为" + student.getStudentid() + "的姓名为" + student.getStudentname());
Con_db.closeALL(connection, preparedStatement, null);// 关闭数据库
System.out.println("已关闭数据库");
}
@Override
public void query(Map<String, String> condition) throws Exception {
// TODO 自动生成的方法存根
Connection connection = Con_db.getConnection();
String updatesql = "select * from STUDENTINFO where STUDENTID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updatesql);
preparedStatement.setString(1, condition.get("s"));
// preparedStatement.executeUpdate();
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
// 打印的就是总记录数。把检索结果看成只有一跳记录一个字段的表
System.out.println("查询结果:");
System.out.println("学生ID:" + rs.getString("STUDENTID") + "\n" + "学生姓名:" + rs.getString("STUDENTNAME") + "\n" + "性别:" + rs.getString("SEX") + "\n" + "学生编码:" + rs.getString("DEPARTMENTCODE"));
}
Con_db.closeALL(connection, preparedStatement, null);
}
}
3. entity classes
to add class files in the project: Student.java (carefully designed to do the project)
How to quickly generate entity classes? (Click to view)
package main;
/**
* @author szymou
* @学生信息实体类
*/
//一般与数据库某表的字段对应
public class Student {
private String studentid;
private String studentname;
private String sex;
private String departmentcode;
private String address;
private String tel;
private String fax;
public String getStudentid() {
return studentid;
}
public void setStudentid(String studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDepartmentcode() {
return departmentcode;
}
public void setDepartmentcode(String departmentcode) {
this.departmentcode = departmentcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
}
4. Database tools
add the class file in the project: Con_db.java (for connecting to the database)
package main;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author szymou
* @数据库工具类
*/
public class Con_db {
// 连接数据库
static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("数据库驱动加载成功");
// 数据库用户名为nue,密码为1;你们的自己改
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Szymou", "nue", "1");
System.out.println("已连接数据库");
connection.setAutoCommit(true);// 自动提交
return connection;
}
// 关闭数据库
static void closeALL(Connection connection, Statement createStatement, ResultSet executeQuery) throws SQLException {
createStatement.close();
connection.close();
}
}
5. Perform
a new project in a Test.java , for executing the entire program.
package main;
import java.util.HashMap;
import java.util.Map;
/**
* @author szymou
* @执行
*/
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
//用于增、删、改。不同操作,改不同数据,这个不多说
Student student = new Student();
student.setStudentid("010");
student.setStudentname("6cc");
student.setSex("m");
student.setDepartmentcode("201");
student.setAddress("he is a goo66666");
student.setTel("020-45443441");
student.setFax("020-99976562");
//用于查询语句的查询条件
Map<String, String> conditon = new HashMap<String, String>();
conditon.put("s", student.getStudentid());
//增删查改都在这里直接调用
IDAO dao = new Use_DaoImpl();
// dao.insert(student);
// dao.delete(student);
// dao.update(student);
dao.query(conditon);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
This article Demo: DAO_TEST.zip