用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);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java代码可以通过使用JDBC API来实现session与数据库信息的同步。可以使用JDBC获取数据库中的信息,然后将其存储在session中。之后可以使用JDBC更新session中的信息,并将其同步数据库中。 ### 回答2: 要在Java代码中实现session与数据库信息的同步,可以按照以下步骤进行操作: 1. 首先,需要在Java代码中建立数据库连接。可以使用JDBC(Java Database Connectivity)技术来连接数据库。通过加载数据库驱动程序,建立数据库连接,创建一个Connection对象。 2. 在建立数据库连接之后,可以通过执行SQL查询语句来获取数据库中的信息。使用Statement或PreparedStatement对象执行查询语句,并将结果存储在ResultSet对象中。 3. 当获取到数据库信息后,可以将其存储在Java的HttpSession对象中。HttpSession对象是用于在服务器端存储用户相关信息的一种机制。可以通过HttpServletRequest对象的getSession()方法获取HttpSession对象。 4. 将数据库信息存储在HttpSession对象中的某个属性上,可以使用setAttribute()方法。可以根据需要将不同的数据库信息存储在不同的属性上。 5. 当需要在不同的页面或不同的请求之间访问数据库信息时,可以通过获取HttpSession对象并读取相应的属性值来实现同步。 6. 如果需要对数据库信息进行更新,可以在Java代码中执行相应的SQL更新语句,将更新后的数据写入数据库。更新后的数据会被同步到HttpSession对象中。 7. 当会话结束或需要销毁HttpSession时,可以调用invalidate()方法来销毁当前会话中的HttpSession对象。 总结起来,通过建立数据库连接、执行查询、将结果存储在HttpSession对象中,并在需要时更新数据,可以实现session与数据库信息的同步。这样,在不同的页面或不同的请求中,可以方便地获取和更新数据库中的信息,并保证其与用户的会话信息同步。 ### 回答3: 要实现Java代码中的session与数据库信息同步,首先需要确保数据库连接在session中可用。以下是实现的步骤: 1. 在Java代码中,可以使用Servlet API提供的HttpSession接口来处理session。可以通过HttpServletRequest对象获取当前会话的session。例如:HttpSession session = request.getSession(); 2. 在数据库中创建一个表来存储session信息。表中的字段可以包括session ID、创建时间、最后访问时间以及其他自定义的属性。 3. 在每次请求中,都需要更新session的最后访问时间,以确保session不会过期。可以在过滤器或拦截器中实现刷新最后访问时间的逻辑。 4. 当session属性发生变化时,需要在数据库中更新对应的字段。可以使用监听器来监听session属性的变化,并在变化发生时将信息同步数据库。 5. 在用户登录或注销时,需要在数据库中添加或删除对应的session信息。可以在登录或注销的逻辑中执行数据库操作。 6. 定时清理过期的session信息。可以使用定时任务来实现定期清理数据库中过期的session记录实现session与数据库信息同步需要根据具体的框架和需求来定制,上述步骤仅提供了一个基本的实现思路。可以根据项目情况进行相应的调整和扩展,以满足实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值