例子:一个部门多个员工
Demo
- dept类
package mult.bean;
import java.util.Set;
public class Dept {
private String name;
private int id;
private Set<Emp> Emp;
public Dept() {
super();
}
public Dept(String name, int id, Set<mult.bean.Emp> emp) {
super();
this.name = name;
this.id = id;
Emp = emp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Emp> getEmp() {
return Emp;
}
public void setEmp(Set<Emp> emp) {
Emp = emp;
}
static int i = 1;
@Override
public String toString() {
System.out.println("dept " + (i++));
return "Dept [name=" + name + ", id=" + id + ", Emp=" + Emp + "]";
}
}
- emp类
package mult.bean;
public class Emp {
private int id;
private String name;
private Dept d;
public Emp() {
super();
}
public Emp(int id, String name, Dept d) {
super();
this.id = id;
this.name = name;
this.d = d;
}
public int getId() {
return id;
}
public void setIdj(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dept getDept() {
return d;
}
public void setDept(Dept d) {
this.d = d;
}
static int i = 1;
@Override
public String toString() {
System.out.println("emp " + (i++));
return "Emp [id=" + id + ", name=" + name + "]";
}
}
- 实现类
package mult.classwork;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import jdbcUtil.jdbcUtils;
import mult.bean.Dept;
import mult.bean.Emp;
import oracle.net.aso.d;
/**
* 多表查询
*
* 思路
* 创建conn
* 创建dept
* 添加dept的数据
* 创建emp
* 添加emp的数据
* 创建集合set
* 把emp添加到set
* 把set添加到dept
* 创建事务,手动提交
* dept
* 准备sql
* 获取ps
* 设置占位符
* 执行sql
* 判断结果
* emp
* 准备sql
* 获取ps
* 设置占位符
* 执行sql
* 判断结果
* 提交事务
* 报错处理回滚
* 关闭资源
*
*/
public class Demo01 {
/**
* 多表添加
*/
@Test
public void demo01() {
// 创建conn
Connection conn = jdbcUtils.getConn();
// 创建dept
Dept d = new Dept();
// 添加dept的数据
d.setId(1);
d.setName("School");
// 创建emp
// 添加emp的数据
Emp e1 = new Emp(01, "Tom", d);
Emp e2 = new Emp(02, "John", d);
Emp e3 = new Emp(03, "Amy", d);
// 创建集合set
Set<Emp> set = new HashSet<Emp>();
// 把emp添加到set
set.add(e1);
set.add(e2);
set.add(e3);
// 把set添加到dept
d.setEmp(set);
PreparedStatement ps = null;
try {
// 创建事务,手动提交
conn.setAutoCommit(false);
// dept
// 准备sql
String d_sql = "insert into t_dept(id,name) values(?,?)";
// 获取ps
ps = conn.prepareStatement(d_sql);
// 设置占位符
ps.setInt(1, 1);
ps.setString(2, "School");
// 执行sql
int update = ps.executeUpdate();
// 判断结果
if (update > 0) {
System.out.println("添加成功");
} else {
throw new Exception();
}
// emp
// 准备sql
String e_sql = "insert into t_emp(id,name,dept) values(?,?,?)";
// 获取ps
ps = conn.prepareStatement(e_sql);
for (Emp emp : set) {
// 设置占位符
ps.setInt(1, emp.getId());
ps.setString(2, emp.getName());
ps.setInt(3, d.getId());
// 执行sql
int update2 = ps.executeUpdate();
// 判断结果
if (update2 > 0) {
System.out.println("添加成功");
} else {
throw new Exception();
}
}
// 提交事务
conn.commit();
} catch (Exception e) {
e.printStackTrace();
// 报错处理回滚
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e4) {
e4.printStackTrace();
}
}
} finally {
// 关闭资源
jdbcUtils.closeAll(ps, conn);
}
}
/**
* 多表查询
*/
@Test
public void demo02() {
Dept d = new Dept();
d.setId(1);
// 创建conn
Connection conn = jdbcUtils.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
// dept
// 准备sql
String d_sql = "select * from t_dept where id = ?";
try {
// 获取ps
ps = conn.prepareStatement(d_sql);
// 设置占位符
ps.setInt(1, d.getId());
// 执行sql
rs = ps.executeQuery();
// 判断结果
if (rs.next()) {
String name = rs.getString("name");
d.setName(name);
//System.out.println(d);
}
// emp
// 准备sql
String e_sql = "select * from t_emp where dept = ?";
// 获取ps
ps = conn.prepareStatement(e_sql);
// 设置占位符
ps.setInt(1, d.getId());
// 执行sql
rs = ps.executeQuery();
// 判断结果
Set<Emp> set = new HashSet<Emp>();
while (rs.next()) {
int id = rs.getInt("id");
//System.out.println(id);
String name = rs.getString("name");
//System.out.println(name);
Emp emp = new Emp(id, name, d);
set.add(emp);
}
d.setEmp(set);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
jdbcUtils.closeAll(rs, ps, conn);
}
System.out.println(d);
}
/**
* 删除
*/
@Test
public void demo03() {
Dept d = new Dept();
d.setId(1);
// 创建conn
Connection conn = jdbcUtils.getConn();
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
String e_sql = "delete from t_emp where dept = ?";
ps = conn.prepareStatement(e_sql);
ps.setInt(1, d.getId());
int update = ps.executeUpdate();
if (update > 0) {
System.out.println("删除成功");
} else {
throw new SQLException();
}
String d_sql = "delete from t_dept where id = ?";
ps = conn.prepareStatement(d_sql);
ps.setInt(1, d.getId());
int update2 = ps.executeUpdate();
if (update2 > 0) {
System.out.println("删除成功");
} else {
throw new SQLException();
}
} catch (SQLException e) {
e.printStackTrace();
if (conn != null) {
System.out.println("回滚");
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
} finally {
jdbcUtils.closeAll(ps, conn);
}
}
}
递归问题
因为类重写toString()的重写,会在打印的时候出现递归问题
原因是:
- dept类的toString()重写
@Override
public String toString() {
System.out.println("dept " + (i++));
return "Dept [name=" + name + ", id=" + id + ", Emp=" + Emp + "]";
}
- emp类的toString()重写
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", d=" + d + "]";
}
当打印dept时,会调用Emp,而Emp也会调用dept,从而形成递归
解决方法
需要把emp类的toString()中的d删除