您好,欢迎访问一九零五行业门户网

JDBC优化

一.什么是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;i0){ 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类实现对数据库的操作,从而达到业务逻辑和数据访问之间的分离。
其它类似信息

推荐信息