用jdbc实现对象与数据库表中记录的同步

java主要是面向对象的语言,因此,我们大多数操作的都是对象。我们如何将对象的信息存入到数据库中去了,其主要分为三步:
1):写出实体类的JavaBean
2):在数据库中建立与之对应的表记录
3): 根据需求写出dao层
(注:一般的,每一个对象都有唯一的一个OID(object ID),与数据库中表的ID一一对应。对象之间的关系在表之间中主要也反映为主外键的关系(假如对象在内存中没有OID,对象中表的跟新与查询变得相当的困难)。
即:一旦一个对象在数据库表中有相应的记录,就会有相应的OID,这样的一个对象也就是一个持久化的对象,此时,如果跟新内存中该对象的值,提交之后就可以反映到数据库中去。如果一个对象在表中没有相应的记录,此时,该对象为一个临时对象,当该对象在很长一段时间没有被使用时就会被GC清理掉。)
以下是一个用JDBC实现对象存储的简单列子:

1:person的javabean


public class Person {

private Integer id;//对象OID
private String name;
private String address;
private String personId;

public Person() {
super();
}

public Person(String name, String address, String personId) {
super();
this.name = name;
this.address = address;
this.personId = personId;
}

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPersonId() {
return personId;
}

public void setPersonId(String personId) {
this.personId = personId;
}

}


2:创建与之对应的表:
create sequence jdbc_person_sequence;//id通过该序列产生
create table person
(
id number(7) primary key,
name varchar2(20) not null,
address varchar2(30) not null,
personId varchar2(21) not null
);

3:用JDBC实现dao层:

import java.sql.*;
import com.UtilTool.*;
public class Dao_Person {

/*
* function: insert a new personInfo into the person table
* variable:insert a new personInfo
* */
public void addPersonInfo(Person person)throws Exception{
Connection connection=null;
PreparedStatement pstm=null;
try{
person.setId(this.getIdSequence());//set the object ID from the method;
connection=ConnectTool.getConnection();//get current connection
connection.setAutoCommit(false);//manual control the transaction commit
String sql="insert into person(id,name,address,personId) values(?,?,?,?)";
pstm=connection.prepareStatement(sql);
pstm.setInt(1, person.getId());
pstm.setString(2, person.getName());
pstm.setString(3, person.getAddress());
pstm.setString(4, person.getPersonId());
pstm.execute();
connection.commit();
}finally{
ConnectTool.releasersc(null, pstm, null);//colse the Result and the pstm
}
}

/*
* function:delete a person's information from table 'person'
* variable:the person you will delete
* */
public void deletePersonInfo(Person person)throws Exception{
Connection connection=null;
PreparedStatement pstm=null;
try{
connection=ConnectTool.getConnection();
connection.setAutoCommit(false);
String sql="delete from person where id=?";
pstm=connection.prepareStatement(sql);
pstm.setInt(1, person.getId());
pstm.executeUpdate();
connection.commit();
}finally{
ConnectTool.releasersc(null, pstm, null);
}

}

/*
* function:modify the person's information in the table 'person'
* variable: person information you will update.
* */
public void updatePersonInfo(Person person)throws Exception{
Connection connection=null;
PreparedStatement pstm=null;
try{
connection=ConnectTool.getConnection();
connection.setAutoCommit(false);
String sql="update person set name=?,address=?,personId=? where id=?";
pstm=connection.prepareStatement(sql);
pstm.setString(1, person.getName());
pstm.setString(2, person.getAddress());
pstm.setString(3, person.getPersonId());
pstm.setInt(4, person.getId());
pstm.execute();
connection.commit();
}finally{
ConnectTool.releasersc(null, pstm, null);
}


}
/*
* function:get a person information by the persons's OId
* variable:person'OId you want to query.
* */
public Person queryPersonInfo(Integer OId)throws Exception{
Connection connection=null;
PreparedStatement pstm=null;
ResultSet rs=null;
Person person=null;
try{
connection=ConnectTool.getConnection();
String sql="select * from person where id=?";
pstm=connection.prepareStatement(sql);
pstm.setInt(1, OId);
rs=pstm.executeQuery();
if(rs.next()){
person=new Person();
person.setId(rs.getInt(1));
person.setName(rs.getString(2));
person.setAddress(rs.getString(3));
person.setPersonId(rs.getString(4));
}
}finally{
ConnectTool.releasersc(rs, pstm, null);
}
return person;
}

/*
* get the OId from the sequence jdbc_person_sequence
* */
private Integer getIdSequence()throws Exception{
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
Integer OId=null;
try{
con=ConnectTool.getConnection();
String sql="select jdbc_person_sequence.nextVal from dual";
pstm=con.prepareStatement(sql);
rs=pstm.executeQuery();
rs.next();
OId=rs.getInt(1);


}finally{
ConnectTool.releasersc(rs, pstm, null);
}
return OId;
}

}

以下是对上述代码的测试:

import java.sql.Connection;
import com.UtilTool.*;
public class Test {
public static void main(String args[])throws Exception{
Connection conn=ConnectTool.getConnection();
Dao_Person dao=new Dao_Person();
Person person=dao.queryPersonInfo(6);

//dao.addPersonInfo(person);
//dao.deletePersonInfo(person);
person.setName("good morning");
dao.updatePersonInfo(person);
ConnectTool.releasersc(null, null, conn);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值