热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

JDBC优化

一.什么是dao二.dao模式实现三.dao优化定义包的模式如下:TecDaopackagedao;importjava.sql.SQLException;importjava.util.List;importpojo.TecPojo;publicinterfaceTecDao{publicintinsert(TecPojotec)throwsSQLException;publicintdelet

一.什么是dao 二.dao模式实现 三.dao优化 定义包的模式如下: TecDao package dao;import java.sql.SQLException;import java.util.List;import pojo.TecPojo;public interface TecDao {public int insert(TecPojo tec)throws SQLException;public int delet

一.什么是dao

二.dao模式实现

三.dao优化


定义包的模式如下:

TecDao

package dao;

import java.sql.SQLException;
import java.util.List;

import pojo.TecPojo;

public interface TecDao {
	public int insert(TecPojo tec)throws SQLException;
	public int delete(int id)throws SQLException;
	public int update(TecPojo tec)throws SQLException;
	public List query(TecPojo tec)throws SQLException;
}

TecDaoImpl

package dao.impl;

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 dao.TecDao;
import pojo.TecPojo;
import util.SQLUtil;

public class TecDaoImpl implements TecDao{
	

	public int insert(TecPojo tec) throws SQLException {
		Connection cOnn= SQLUtil.getConnection();
		String sql = "insert into tec (name,age,gender,job,createDate)values(?,?,?,?,?)";
		PreparedStatement state = conn.prepareStatement(sql);
		state.setString(1, tec.getName());
		state.setInt(2, tec.getAge());
		state.setString(3, tec.getGender());
		state.setString(4, tec.getJob());
		state.setString(5, tec.getCreateDate());
		int result = state.executeUpdate();
		if(result>0){
			System.out.println("Yes");
		}
		return result;
	}


	public int update(TecPojo tec) throws SQLException {
		Connection cOnn= SQLUtil.getConnection();
		String sql = "update tec set name = ?,age = ? where id = 11";
		PreparedStatement state = conn.prepareStatement(sql);
		state.setString(1, tec.getName());
		state.setInt(2, tec.getAge());
		int result = state.executeUpdate();
		if(result>0){
			System.out.println("Yes");
		}
		return result;
	}

	public List query(TecPojo tec) throws SQLException {
		Connection cOnn= SQLUtil.getConnection();
		String sql = "select * from tec";
		PreparedStatement state = conn.prepareStatement(sql);
		ResultSet resultset = state.executeQuery();
		ArrayList list = new ArrayList();
		TecPojo tp = null;
		while(resultset.next()){
			tp = new TecPojo();
			tp.setId(resultset.getInt("id"));
			tp.setName(resultset.getString("name"));
			tp.setAge(resultset.getInt("age"));
			tp.setGender(resultset.getString("gender"));
			tp.setJob(resultset.getString("job"));
			tp.setCreateDate(resultset.getString("createDate"));
			list.add(tp);
		}
		for(int i = 0;i0){
			System.out.println("Yes");
		}
		return result;
	}
}

TecPojo

package pojo;

public class TecPojo {
	private int id;
	private String name;
	private int age;
	private String gender;
	private String job;
	private String createDate;

	public TecPojo(String name, int age, String gender, String job, String createDate) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.job = job;
		this.createDate = createDate;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String toString() {
		return id+" "+name+" "+age+" "+gender+" "+job+" "+createDate;
	}

	public TecPojo() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public String getCreateDate() {
		return createDate;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

}

SQLUtil

package util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class SQLUtil {
	private static String url;
	private static String user;
	private static String password;
	private static String driver;
	static{
		try {
			Properties pro = new Properties();
			InputStream ins = SQLUtil.class.getResourceAsStream("/sqlconfig.properties");
			pro.load(ins);
			url = pro.getProperty("url");
			user = pro.getProperty("user");
			password = pro.getProperty("password");
			driver = pro.getProperty("driver");
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		Connection cOnn= null;
		try {
			cOnn= DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}

Test

package util;

import java.sql.SQLException;

import dao.impl.TecDaoImpl;
import pojo.TecPojo;

public class Test {
	public static void main(String[] args) {
		SQLUtil util = new SQLUtil();
		TecPojo tp = new TecPojo("bb",22,"man","teacher","2016-4-6");
		TecDaoImpl tdi = new TecDaoImpl();
		try {
//			tdi.insert(tp);
//			tdi.update(tp);
			tdi.query(tp);
//			tdi.delete(12);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

此时发现,TecDaoImpl的方法有多次重复,于是对重复的方法再次进行封装


SQLTemplete

package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SQLTemplete {
	public static int update(String sql,Object...object)throws SQLException{
		Connection cOnn= SQLUtil.getConnection();
		PreparedStatement state = conn.prepareStatement(sql);
		for(int i=0;i


TecDaoImpl

package dao.impl;

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 dao.TecDao;
import pojo.TecPojo;
import util.SQLTemplete;
import util.SQLUtil;

public class TecDaoImpl implements TecDao{
	

	public int insert(TecPojo tec) throws SQLException {
		String sql = "insert into tec (name,age,gender,job,createDate)values(?,?,?,?,?)";
		int result = SQLTemplete.update(sql, tec.getName(),tec.getAge(),tec.getGender(),tec.getJob(),tec.getCreateDate());
		if(result>0){
			System.out.println("Yes");
		}
		return result;
	}


	public int update(TecPojo tec) throws SQLException {
		String sql = "update tec set name = ?,age = ? where id = 11";
		int result = SQLTemplete.update(sql, tec.getName(),tec.getAge());
		if(result>0){
			System.out.println("Yes");
		}
		return result;
	}

	public List query(TecPojo tec) throws SQLException {
		String sql = "select * from tec";
		ResultSet resultset = SQLTemplete.query(sql);
		ArrayList list = new ArrayList();
		TecPojo tp = null;
		while(resultset.next()){
			tp = new TecPojo();
			tp.setId(resultset.getInt("id"));
			tp.setName(resultset.getString("name"));
			tp.setAge(resultset.getInt("age"));
			tp.setGender(resultset.getString("gender"));
			tp.setJob(resultset.getString("job"));
			tp.setCreateDate(resultset.getString("createDate"));
			list.add(tp);
		}
		for(int i = 0;i0){
			System.out.println("Yes");
		}
		return result;
	}
}

这样实现了更彻底的封装,TecDaoImpl类里没有直接和数据库联系,用SQLTemplete类实现对数据库的操作,从而达到业务逻辑和数据访问之间的分离。
推荐阅读
author-avatar
霙昉蘖976
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有