mvc设计模式

来源:互联网 发布:怎么样才能做好淘宝店 编辑:程序博客网 时间:2024/04/27 14:52
//1jar包的导入 我的jar包为:ojdbc6.jar//配置文件的编写,在这里我的配置文件为jdbc. properties//entity层:根据数据库的表格创建实体类//dao层:根据实体类创建的实体类的抽象类//dao层—impl:为实体类抽象类的子类,直接操作数据库的子类// utils层:为工具类,为了降低代码直接的耦合性随之产生的工具类下面果断copy我的代码:

entity层:Temp.java(根据数据库的信息创建的实体类)

import java.io.Serializable;import java.util.Date;/** * @author ljk * 创建Temp实体类 */public class Temp implements Serializable {private int empno;private String ename;private String job;private int mgr;private Date hiredate;private double sal;private double comm;private int deptno;public Temp(int empno, String ename, String job, int mgr, Date hiredate,double sal, double comm, int deptno) {this.empno = empno;this.ename = ename;this.job = job;this.mgr = mgr;this.hiredate = hiredate;this.sal = sal;this.comm = comm;this.deptno = deptno;}public Temp() {}public int getEmpno() {return empno;}public void setEmpno(int empno) {this.empno = empno;}public String getEname() {return ename;}public void setEname(String ename) {this.ename = ename;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public int getMgr() {return mgr;}public void setMgr(int mgr) {this.mgr = mgr;}public Date getHiredate() {return hiredate;}public void setHiredate(Date hiredate) {this.hiredate = hiredate;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public double getComm() {return comm;}public void setComm(double comm) {this.comm = comm;}public int getDeptno() {return deptno;}public void setDeptno(int deptno) {this.deptno = deptno;}@Overridepublic String toString() {return "Temp [empno=" + empno + ", ename=" + ename + ", job=" + job+ ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal+ ", comm=" + comm + ", deptno=" + deptno + "]";}@Overridepublic int hashCode() {final int prime = 31;int result = 1;long temp;temp = Double.doubleToLongBits(comm);result = prime * result + (int) (temp ^ (temp >>> 32));result = prime * result + deptno;result = prime * result + empno;result = prime * result + ((ename == null) ? 0 : ename.hashCode());result = prime * result+ ((hiredate == null) ? 0 : hiredate.hashCode());result = prime * result + ((job == null) ? 0 : job.hashCode());result = prime * result + mgr;temp = Double.doubleToLongBits(sal);result = prime * result + (int) (temp ^ (temp >>> 32));return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Temp other = (Temp) obj;if (Double.doubleToLongBits(comm) != Double.doubleToLongBits(other.comm))return false;if (deptno != other.deptno)return false;if (empno != other.empno)return false;if (ename == null) {if (other.ename != null)return false;} else if (!ename.equals(other.ename))return false;if (hiredate == null) {if (other.hiredate != null)return false;} else if (!hiredate.equals(other.hiredate))return false;if (job == null) {if (other.job != null)return false;} else if (!job.equals(other.job))return false;if (mgr != other.mgr)return false;if (Double.doubleToLongBits(sal) != Double.doubleToLongBits(other.sal))return false;return true;}}

utils层:SxtJDBCUtil.java(为了降低代码直接的耦合性)

import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;/** * @author ljk * @JDBC工具类 */public class BjsxtUtils {private static String url;private static String dirver;private static String uname;private static String pwd;static{//创建properties对象Properties pro= new Properties();try {//将jdbc.properties文件数据加载到当前类中pro.load(BjsxtUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));dirver=pro.getProperty("dirver");url=pro.getProperty("url");uname=pro.getProperty("uname");pwd=pro.getProperty("pwd");Class.forName(dirver);System.out.println(dirver+"======"+url+"=========="+uname+"========="+pwd);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}/** * @建立连接 * @return */public static Connection getcoConnection(){Connection conn=null;//2加载驱动try {conn= DriverManager.getConnection(url,uname,pwd);conn.setAutoCommit(false);}catch (SQLException e) {e.printStackTrace();}//3创建连接return conn;}/** * @param conn * @param sql * @return * @获取命令发送器 */public static Statement getStatement(Connection conn){Statement state=null;try {state=conn.createStatement();} catch (SQLException e) {e.printStackTrace();}return state;}public static PreparedStatement getpPreparedStatement(Connection conn,String sql){PreparedStatement pstate=null;try {pstate=conn.prepareStatement(sql);} catch (SQLException e) {e.printStackTrace();}return pstate;}public static ResultSet getrResultSet(){return null;}public static void closeAll(ResultSet resul,Statement state,Connection conn){if(resul!=null){try {resul.close();} catch (SQLException e) {e.printStackTrace();}}if(state!=null){try {state.close();} catch (SQLException e) { e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}

dao层:ITempDao(根据实体类创建的抽象类)

import java.util.List;import com.bjsxt.entity.Temp;public interface ITempDao {//查询所有员工信息public List<Temp> findAll();}
dao层—impl:TempDaoimpl(实现了实体类的抽象类,可以直接对数据库信息进行操作)
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.bjsxt.dao.ITempDao;import com.bjsxt.entity.Temp;import com.bjsxt.utils.SxtJDBCUtil;public class TempDaoimpl implements ITempDao {public static void main(String[] args) {List<Temp> temps=new TempDaoimpl().findAll();System.out.println(temps.size());}@Overridepublic List<Temp> findAll() {//创建接受集合List<Temp> lists=new ArrayList<>();//声明连接Connection conn=null;PreparedStatement pstat=null;ResultSet rs=null;try {//创建连接conn=SxtJDBCUtil.getConnection();//创建命令发送器String sql="SELECT * FROM TEMP";pstat=SxtJDBCUtil.getPreparedStatement(conn, sql);//执行sql语句获取结果集rs=pstat.executeQuery();while(rs.next()){Temp temp=new Temp();temp.setEmpno(rs.getInt("empno"));temp.setEname(rs.getString("ename"));temp.setJob(rs.getString("job"));temp.setMgr(rs.getInt("mgr"));temp.setHiredate(rs.getDate("hiredate"));temp.setSal(rs.getDouble("sal"));temp.setComm(rs.getDouble("comm"));temp.setDeptno(rs.getInt("deptno"));lists.add(temp);}if(lists.size() >0){return lists;}} catch (SQLException e) {e.printStackTrace();}//关闭资源finally{SxtJDBCUtil.closeAll(rs, pstat, conn);}return null;}}
0 0
原创粉丝点击