ITeye博客

用JDBC处理一对一的关系

jallay · 2008-10-12 15:09 · Markdown

类与类之间存在多种关系:组件关系,继承关系,关联关系。 其中:关联关系有分为:一对一,一对多,多对一,多对多几种。 以下就一对一的关联关系进行说明: 所谓一对一的关联关系就是指一个类的一个对象在另一个类中只有且只有一个与之对应的对象,翻过来也一样。就好比身份证与人一样。一个身份证只能属于一个人,一个人只能拥有一个身份证号一样。 如何将这种关系用JDBC将其反映到数据库中去了: 1)建立连个类:Person1, PersonCard;

(1)person1类:

public class Person1 {
	private Integer id;
	private String name;
	private String address;
	private PersonCard personCard;
	
	public Person1() {
		super();
	}

	public Person1(String name, String address, PersonCard personCard) {
		super();
		this.name = name;
		this.address = address;
		this.personCard = personCard;
	}

	public Integer getId() {
		return id;
	}

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

	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 PersonCard getPersonCard() {
		return personCard;
	}

	public void setPersonCard(PersonCard personCard) {
		this.personCard = personCard;
	}
	
	/*
	 *fucntion:mantain the relation between personCard and person when adding 
	 *         a person,we should also add the person's personCard.
	 *variable:personCard information
	 * */
	public void addPersonCard(PersonCard personCard){
		this.personCard=personCard;
		personCard.addPersonInfo(this);
	}
	
	/*
	 * function: remove the person relation to the personCard.
	 * variable: personCard information
	 * */
	public void removePersonCard(PersonCard personCard){
		this.personCard.remove();
		this.personCard=null;
		
	}
}

PersonCard类:

public class PersonCard {
	private Integer id;
	private String cardNum;
	private int cardSize;
	private Person1 person;
	
	public PersonCard() {
		super();
	}
	
	public PersonCard(String cardNum, int cardSize) {
		super();
		this.cardNum = cardNum;
		this.cardSize = cardSize;
	}

	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getCardNum() {
		return cardNum;
	}
	
	public void setCardNum(String cardNum) {
		this.cardNum = cardNum;
	}
	
	public int getCardSize() {
		return cardSize;
	}
	
	public void setCardSize(int cardSize) {
		this.cardSize = cardSize;
	}

	public Person1 getPerson() {
		return person;
	}

	public void setPerson(Person1 person) {
		this.person = person;
	}
	
	/*
	 *function:keep the relation betwwen this
	 *variable:person information
	 * */
	public void addPersonInfo(Person1 person){
		this.person=person;
	}
	
	/*
	 *fucntion:delete the the person.
	 *variable:null  
	 * */
	public void remove(){
		this.person=null;
	}

}

在数据库中创建与之对应的表。并将类之间的关系正确的反应到数据库表中去:

create sequence person1_sequence;
create sequence personCard_sequence;

create table person1
(
id number(7) primary key,
name varchar2(20) not null,
address varchar2(30) not null
);

create table personCard
(
id number(7) primary key,
cardNum varchar2(20) not null unique,
carSize number(7) not null,
person_id number(7) references person1(id) unique
)

用JDBC实现上述关系:



import java.sql.*;

import com.UtilTool.*;
public class DAO {
	
	/*
	 * function:when you add a new person,you should also add a new personCard
	 * variable:the person object and the personCard object
	 * */
	public void addNewPerson(Person1 person,PersonCard personCard)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		
		try{
			/*get the sequence for the object's id*/
			person.setId(this.getPersonIdSequence());
			conn=ConnectTool.getConnection();
			conn.setAutoCommit(false);
			/*insert into the table person1 for the person information*/
			String insertPersonInfo="insert into person1(id,name,address)" +
					" values(?,?,?)";
			pstm=conn.prepareStatement(insertPersonInfo);
			pstm.setInt(1,person.getId());
			pstm.setString(2, person.getName());
			pstm.setString(3, person.getAddress());
			pstm.execute();
			
			/*this is the same to the upper*/
			personCard.setId(this.getPersonCardSequence());
			personCard.setPerson(person);
			String insertPersonCardInfo=" insert into personCard(id,cardNum," +
					"carSize,person_id) values(?,?,?,?)";
			pstm=conn.prepareStatement(insertPersonCardInfo);
			pstm.setInt(1, personCard.getId());
			pstm.setString(2,personCard.getCardNum());
			pstm.setInt(3, personCard.getId());
			pstm.setInt(4,personCard.getPerson().getId());
			
			conn.commit();
		}finally{
			ConnectTool.releasersc(null, pstm, null);
			
		}
	}

	/*
	 * function: update the person information
	 * */
	public void updatePerson(Person1 person)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		
		try{
			person.setId(this.getPersonIdSequence());
			conn=ConnectTool.getConnection();
			conn.setAutoCommit(false);
			String updatePersonInfo="update person1 " +
					"set name=?,address=? where id=?;";
			pstm=conn.prepareStatement(updatePersonInfo);
			pstm.setString(1, person.getName());
			pstm.setString(2, person.getAddress());
			pstm.setInt(3,person.getId());
			pstm.execute();
		}finally{
			ConnectTool.releasersc(null, pstm, null);
		}
	}

	/*
	 * function:query the person information by the cardNum
	 * */
	public Person1 Query(String cardNum)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		PersonCard personCard=null;
		Person1 person=null;
		
		try{
	        conn=ConnectTool.getConnection();
			String sql="  select pc.id,pc.cardNum,pc.carSize,ps." +
					"id,ps.name,ps.address" +
					" from personCard pc inner join person1 ps " +
					"on(pc.person_id=ps.id)  where cardnum=?";
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, cardNum);
			rs=pstm.executeQuery();
			if(rs.next()){
				person=new Person1();
				personCard=new PersonCard();
				personCard.setId(rs.getInt(1));
				personCard.setCardNum(rs.getString(2));
				personCard.setCardSize(rs.getInt(3));
				person.setId(rs.getInt(4));
				person.setName(rs.getString(5));
				person.setAddress(rs.getString(6));
				personCard.setPerson(person);
				person.setPersonCard(personCard);
			}
			
		}finally{
			ConnectTool.releasersc(rs, pstm, null);
		}
		return person;
		
	}
	
	
	public void deletePerson(PersonCard personCard)throws Exception{
		Connection conn=null;
		PreparedStatement pstm=null;
		
		try{
			conn=ConnectTool.getConnection();
			conn.setAutoCommit(false);
			String sql1="delete from personCard where id=?";
			String sql2="delete from person1 where id=?";
				pstm=conn.prepareStatement(sql1);
				pstm.setInt(1, personCard.getId());
				pstm.execute();
			    pstm=conn.prepareStatement(sql2);
			    pstm.setInt(1, personCard.getPerson().getId());
			    pstm.execute();
			    conn.commit();
				
		}finally{
			ConnectTool.releasersc(null, pstm, null);
		}
	}
	
	private Integer getPersonIdSequence()throws Exception{
		Connection con=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Integer OId=null;
		try{
			con=ConnectTool.getConnection();
			String sql="select person1_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;
	}
	
	private Integer getPersonCardSequence()throws Exception{
		Connection con=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Integer OId=null;
		try{
			con=ConnectTool.getConnection();
			String sql="select personCard_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();
		//Person1 person=new Person1("123","456");
		//PersonCard personCard=new PersonCard("8999",3);
		DAO dao=new DAO();
		//dao.addNewPerson(person, personCard);
	   //System.out.println(dao.Query("8999").getName());
		conn.commit();
	}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。