-
dao (date access object)
数据库访问对象,主要功能就是进行数据库的操作(CURD),属于原子性的操作。
-
Dao的组成
实际上Dao的整个操作是以接口操作为标准的
1.VO:值对象,每个VO对应一个表,VO(由属性和数据库表中的属性相对应,setter,getter 方法组成,如果有需要还可以重写ToString方法和equls()以及构造方法)
2.dao接口:主要定义各种操作的接口,这时只有方法的定义没有方法的实现
3.dao接口实现类:实现dao接口中的方法,也就是完成具体的数据库操作。
4.dao工厂:通过工厂可以获得Dao的实例化对象(创建dao对象) -
在Eclipse中的具体实现的代码:
1.
package Jdbc.dao;
public class Account {
int accountid;
String name;
int remain;
public int getAccountid() {
return accountid;
}
public void setAccountid(int accountid) {
this.accountid = accountid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRemain() {
return remain;
}
public void setRemain(int remain) {
this.remain = remain;
}
@Override
public String toString() {
return "AccountDao [accountid=" + accountid + ", name=" + name + ", remain=" + remain + "]";
}
public Account(String name, int remain) {
super();
this.name = name;
this.remain = remain;
}
public Account() {
super();
}
//其中必须写一个无参的构造方法,因为一定有了有参的构造方法,就不存在默认的构造方法了
}
package Jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import jdbcTest.JdbcUtil;
public class AccountDaoImpl implements AccountDao {
// 定义实现AccountDao类
String sql_inset = "insert into account values(null,?,?)";
String sql_delete = "delete from account where accountid=?";
String sql_update = "update account set name=?,remain=? where accountid=?";
@Override
public void insert(Account account) {
try (Connection conn = JdbcUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql_inset);) {
ps.setString(1, account.getName());
ps.setInt(2, account.getRemain());
ps.execute();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void delete(int accountid) {
try (Connection conn = JdbcUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql_delete);) {
ps.setInt(1,accountid);
ps.execute();
System.out.println(" end success");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean Update(Account account) {
int count=0;
try(Connection conn=JdbcUtil.getConnection();
PreparedStatement ps=conn.prepareStatement(sql_update); ){
ps.setString(1, account.getName());
ps.setInt(2, account.getRemain());
ps.setInt(3, account.getAccountid());
count=ps.executeUpdate();
System.out.println("更新的条数为:"+count);
System.out.println("dddd success");
//在Java中,所有的对象都是继承自Object,自然继承了toString方法,
//在当使用System.out.println()里面为一个对象的引用时,
//自动调用toString方法将对象打印出来。如果重写了tostring方法则调用重写的toString 方法。
System.out.println(account);
}catch (Exception e) {
e.printStackTrace();
}
return count>0?true:false;
}
}
2.对原有代码的优化
public class AccountDaoImpl implements AccountDao {
// 定义实现AccountDao类
String sql_inset = "insert into account values(null,?,?)";
String sql_delete = "delete from account where accountid=?";
String sql_update = "update account set name=?,remain=? where accountid=?";
@Override
public void insert(Account account) {
JdbcUtil.executeSql(sql_inset, account.getName(),account.getRemain());
}
@Override
public void delete(int accountid) {
JdbcUtil.executeSql(sql_delete, accountid);
}
@Override
public boolean Update(Account account) {
return JdbcUtil.executeSql(sql_update, account.getName(),account.getRemain(),account.getAccountid())>0?true:false;
}
}
在JdbcUtil中对execute的封装,
public static int executeSql(String sql, Object...args) {
int count=0;
try(Connection conn=getConnection();
PreparedStatement ps=conn.prepareStatement(sql)){
for(int i=0;i<args.length;i++) {
ps.setObject(i+1,args[i]);
}
count=ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
return count;
}