用JDBC处理一对一的关系

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

  (1)person1类:

Java代码
  1. public class Person1 {   
  2.     private Integer id;   
  3.     private String name;   
  4.     private String address;   
  5.     private PersonCard personCard;   
  6.        
  7.     public Person1() {   
  8.         super();   
  9.     }   
  10.   
  11.     public Person1(String name, String address, PersonCard personCard) {   
  12.         super();   
  13.         this.name = name;   
  14.         this.address = address;   
  15.         this.personCard = personCard;   
  16.     }   
  17.   
  18.     public Integer getId() {   
  19.         return id;   
  20.     }   
  21.   
  22.     public void setId(Integer id) {   
  23.         this.id = id;   
  24.     }   
  25.   
  26.     public String getName() {   
  27.         return name;   
  28.     }   
  29.   
  30.     public void setName(String name) {   
  31.         this.name = name;   
  32.     }   
  33.   
  34.     public String getAddress() {   
  35.         return address;   
  36.     }   
  37.   
  38.     public void setAddress(String address) {   
  39.         this.address = address;   
  40.     }   
  41.   
  42.     public PersonCard getPersonCard() {   
  43.         return personCard;   
  44.     }   
  45.   
  46.     public void setPersonCard(PersonCard personCard) {   
  47.         this.personCard = personCard;   
  48.     }   
  49.        
  50.     /*  
  51.      *fucntion:mantain the relation between personCard and person when adding   
  52.      *         a person,we should also add the person's personCard.  
  53.      *variable:personCard information  
  54.      * */  
  55.     public void addPersonCard(PersonCard personCard){   
  56.         this.personCard=personCard;   
  57.         personCard.addPersonInfo(this);   
  58.     }   
  59.        
  60.     /*  
  61.      * function: remove the person relation to the personCard.  
  62.      * variable: personCard information  
  63.      * */  
  64.     public void removePersonCard(PersonCard personCard){   
  65.         this.personCard.remove();   
  66.         this.personCard=null;   
  67.            
  68.     }   
  69. }  
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类:

Java代码
  1. public class PersonCard {   
  2.     private Integer id;   
  3.     private String cardNum;   
  4.     private int cardSize;   
  5.     private Person1 person;   
  6.        
  7.     public PersonCard() {   
  8.         super();   
  9.     }   
  10.        
  11.     public PersonCard(String cardNum, int cardSize) {   
  12.         super();   
  13.         this.cardNum = cardNum;   
  14.         this.cardSize = cardSize;   
  15.     }   
  16.   
  17.     public Integer getId() {   
  18.         return id;   
  19.     }   
  20.        
  21.     public void setId(Integer id) {   
  22.         this.id = id;   
  23.     }   
  24.        
  25.     public String getCardNum() {   
  26.         return cardNum;   
  27.     }   
  28.        
  29.     public void setCardNum(String cardNum) {   
  30.         this.cardNum = cardNum;   
  31.     }   
  32.        
  33.     public int getCardSize() {   
  34.         return cardSize;   
  35.     }   
  36.        
  37.     public void setCardSize(int cardSize) {   
  38.         this.cardSize = cardSize;   
  39.     }   
  40.   
  41.     public Person1 getPerson() {   
  42.         return person;   
  43.     }   
  44.   
  45.     public void setPerson(Person1 person) {   
  46.         this.person = person;   
  47.     }   
  48.        
  49.     /*  
  50.      *function:keep the relation betwwen this  
  51.      *variable:person information  
  52.      * */  
  53.     public void addPersonInfo(Person1 person){   
  54.         this.person=person;   
  55.     }   
  56.        
  57.     /*  
  58.      *fucntion:delete the the person.  
  59.      *variable:null    
  60.      * */  
  61.     public void remove(){   
  62.         this.person=null;   
  63.     }   
  64.   
  65. }  
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;
	}

}



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

Sql代码
  1. create sequence person1_sequence;   
  2. create sequence personCard_sequence;   
  3.   
  4. create table person1   
  5. (   
  6. id number(7) primary key,   
  7. name varchar2(20) not null,   
  8. address varchar2(30) not null  
  9. );   
  10.   
  11. create table personCard   
  12. (   
  13. id number(7) primary key,   
  14. cardNum varchar2(20) not null unique,   
  15. carSize number(7) not null,   
  16. person_id number(7) references person1(id) unique  
  17. )  
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实现上述关系:

Java代码
  1.   
  2. import java.sql.*;   
  3.   
  4. import com.UtilTool.*;   
  5. public class DAO {   
  6.        
  7.     /*  
  8.      * function:when you add a new person,you should also add a new personCard  
  9.      * variable:the person object and the personCard object  
  10.      * */  
  11.     public void addNewPerson(Person1 person,PersonCard personCard)throws Exception{   
  12.         Connection conn=null;   
  13.         PreparedStatement pstm=null;   
  14.            
  15.         try{   
  16.             /*get the sequence for the object's id*/  
  17.             person.setId(this.getPersonIdSequence());   
  18.             conn=ConnectTool.getConnection();   
  19.             conn.setAutoCommit(false);   
  20.             /*insert into the table person1 for the person information*/  
  21.             String insertPersonInfo="insert into person1(id,name,address)" +   
  22.                     " values(?,?,?)";   
  23.             pstm=conn.prepareStatement(insertPersonInfo);   
  24.             pstm.setInt(1,person.getId());   
  25.             pstm.setString(2, person.getName());   
  26.             pstm.setString(3, person.getAddress());   
  27.             pstm.execute();   
  28.                
  29.             /*this is the same to the upper*/  
  30.             personCard.setId(this.getPersonCardSequence());   
  31.             personCard.setPerson(person);   
  32.             String insertPersonCardInfo=" insert into personCard(id,cardNum," +   
  33.                     "carSize,person_id) values(?,?,?,?)";   
  34.             pstm=conn.prepareStatement(insertPersonCardInfo);   
  35.             pstm.setInt(1, personCard.getId());   
  36.             pstm.setString(2,personCard.getCardNum());   
  37.             pstm.setInt(3, personCard.getId());   
  38.             pstm.setInt(4,personCard.getPerson().getId());   
  39.                
  40.             conn.commit();   
  41.         }finally{   
  42.             ConnectTool.releasersc(null, pstm, null);   
  43.                
  44.         }   
  45.     }   
  46.   
  47.     /*  
  48.      * function: update the person information  
  49.      * */  
  50.     public void updatePerson(Person1 person)throws Exception{   
  51.         Connection conn=null;   
  52.         PreparedStatement pstm=null;   
  53.            
  54.         try{   
  55.             person.setId(this.getPersonIdSequence());   
  56.             conn=ConnectTool.getConnection();   
  57.             conn.setAutoCommit(false);   
  58.             String updatePersonInfo="update person1 " +   
  59.                     "set name=?,address=? where id=?;";   
  60.             pstm=conn.prepareStatement(updatePersonInfo);   
  61.             pstm.setString(1, person.getName());   
  62.             pstm.setString(2, person.getAddress());   
  63.             pstm.setInt(3,person.getId());   
  64.             pstm.execute();   
  65.         }finally{   
  66.             ConnectTool.releasersc(null, pstm, null);   
  67.         }   
  68.     }   
  69.   
  70.     /*  
  71.      * function:query the person information by the cardNum  
  72.      * */  
  73.     public Person1 Query(String cardNum)throws Exception{   
  74.         Connection conn=null;   
  75.         PreparedStatement pstm=null;   
  76.         ResultSet rs=null;   
  77.         PersonCard personCard=null;   
  78.         Person1 person=null;   
  79.            
  80.         try{   
  81.             conn=ConnectTool.getConnection();   
  82.             String sql="  select pc.id,pc.cardNum,pc.carSize,ps." +   
  83.                     "id,ps.name,ps.address" +   
  84.                     " from personCard pc inner join person1 ps " +   
  85.                     "on(pc.person_id=ps.id)  where cardnum=?";   
  86.             pstm=conn.prepareStatement(sql);   
  87.             pstm.setString(1, cardNum);   
  88.             rs=pstm.executeQuery();   
  89.             if(rs.next()){   
  90.                 person=new Person1();   
  91.                 personCard=new PersonCard();   
  92.                 personCard.setId(rs.getInt(1));   
  93.                 personCard.setCardNum(rs.getString(2));   
  94.                 personCard.setCardSize(rs.getInt(3));   
  95.                 person.setId(rs.getInt(4));   
  96.                 person.setName(rs.getString(5));   
  97.                 person.setAddress(rs.getString(6));   
  98.                 personCard.setPerson(person);   
  99.                 person.setPersonCard(personCard);   
  100.             }   
  101.                
  102.         }finally{   
  103.             ConnectTool.releasersc(rs, pstm, null);   
  104.         }   
  105.         return person;   
  106.            
  107.     }   
  108.        
  109.        
  110.     public void deletePerson(PersonCard personCard)throws Exception{   
  111.         Connection conn=null;   
  112.         PreparedStatement pstm=null;   
  113.            
  114.         try{   
  115.             conn=ConnectTool.getConnection();   
  116.             conn.setAutoCommit(false);   
  117.             String sql1="delete from personCard where id=?";   
  118.             String sql2="delete from person1 where id=?";   
  119.                 pstm=conn.prepareStatement(sql1);   
  120.                 pstm.setInt(1, personCard.getId());   
  121.                 pstm.execute();   
  122.                 pstm=conn.prepareStatement(sql2);   
  123.                 pstm.setInt(1, personCard.getPerson().getId());   
  124.                 pstm.execute();   
  125.                 conn.commit();   
  126.                    
  127.         }finally{   
  128.             ConnectTool.releasersc(null, pstm, null);   
  129.         }   
  130.     }   
  131.        
  132.     private Integer getPersonIdSequence()throws Exception{   
  133.         Connection con=null;   
  134.         PreparedStatement pstm=null;   
  135.         ResultSet rs=null;   
  136.         Integer OId=null;   
  137.         try{   
  138.             con=ConnectTool.getConnection();   
  139.             String sql="select person1_sequence.nextVal from dual";   
  140.             pstm=con.prepareStatement(sql);   
  141.             rs=pstm.executeQuery();   
  142.             rs.next();   
  143.             OId=rs.getInt(1);   
  144.                
  145.                
  146.         }finally{   
  147.             ConnectTool.releasersc(rs, pstm, null);   
  148.         }   
  149.         return OId;   
  150.     }   
  151.        
  152.     private Integer getPersonCardSequence()throws Exception{   
  153.         Connection con=null;   
  154.         PreparedStatement pstm=null;   
  155.         ResultSet rs=null;   
  156.         Integer OId=null;   
  157.         try{   
  158.             con=ConnectTool.getConnection();   
  159.             String sql="select personCard_sequence.nextVal from dual";   
  160.             pstm=con.prepareStatement(sql);   
  161.             rs=pstm.executeQuery();   
  162.             rs.next();   
  163.             OId=rs.getInt(1);   
  164.                
  165.                
  166.         }finally{   
  167.             ConnectTool.releasersc(rs, pstm, null);   
  168.         }   
  169.         return OId;   
  170.     }   
  171.   
  172. }  
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;
	}

}



以下是测试上述代码的列子:

Java代码
  1. import java.sql.Connection;   
  2.   
  3. import com.UtilTool.*;   
  4. public class Test {   
  5.     public static void main(String[] args)throws Exception{   
  6.         Connection conn=ConnectTool.getConnection();   
  7.         //Person1 person=new Person1("123","456");   
  8.         //PersonCard personCard=new PersonCard("8999",3);   
  9.         DAO dao=new DAO();   
  10.         //dao.addNewPerson(person, personCard);   
  11.        //System.out.println(dao.Query("8999").getName());   
  12.         conn.commit();   
  13.     }   
  14.   
  15. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值