1.创建封装Emp表和Dept表的pojo类。
封装Emp表的pojo类:
class="java">package com.lxd.pojo;
import java.io.Serializable;
public class Emp implements Serializable {
private static final long serialVersionUID = 1L;
private Integer empno;
private String ename;
private Double sal;
private Integer deptno;
public Emp() {
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
}
封装Dept表的pojo类:
package com.lxd.pojo;
import java.io.Serializable;
public class Dept implements Serializable {
private static final long serialVersionUID = 1L;
private Integer deptno;
private String dname;
private String loc;
public Dept() {
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
2.创建数据库工具类DBUtil,来获得数据库连接与关闭数据库连接。
package com.lxd.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
private static Properties props;
static {
try {
props = new Properties();
// 读取 jdbc 配置文件到输入流(jdbc.properties)
InputStream in = DBUtil.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
// 从输入流中加载 jdbc 配置文件
props.load(in);
Class.forName(props.getProperty("driverName"));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
String url = props.getProperty("url");
String username = props.getProperty("username");
String password = props.getProperty("password");
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// public static void main(String[] args) {
// System.out.println(getConnection());
// }
}
3.创建dao类,来封装Emp表和Dept表的操作(增删改查CRUD)。
package com.lxd.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.lxd.pojo.Dept;
import com.lxd.pojo.Emp;
/**
* 事务操作(一次连接多次执行SQL语句 - 增删改)
*
* @author Xiangdong
*
*/
public class TransDao {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
/**
* 保存部门的同时还保存雇员
*
* @param dept
* @param emp
* @return
*/
public boolean trans(Dept dept, Emp emp) {
boolean flag = true;
try {
conn = DBUtil.getConnection();
// A.设置手动提交事务(取消自动提交事务)
conn.setAutoCommit(false);
// 1.保存部门
String sql = "insert into dept(deptno,dname,loc)"
+ " values(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, dept.getDeptno());
ps.setString(2, dept.getDname());
ps.setString(3, dept.getLoc());
ps.executeUpdate();
// 2.保存雇员
sql = "insert into emp(empno,ename,sal,deptno) ";
sql += " values(?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, emp.getEmpno());
ps.setString(2, emp.getEname());
ps.setDouble(3, emp.getSal());
ps.setInt(4, emp.getDeptno());
ps.executeUpdate();
// B.提交事务
conn.commit();
} catch (SQLException e) {
try {
// C.当出错时回滚事务
conn.rollback();
} catch (SQLException e1) {
flag = false;
e1.printStackTrace();
}
flag = false;
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return flag;
}
}
4.测试dao类,进行事务的操作。
package com.lxd.test;
import com.lxd.dao.TransDao;
import com.lxd.pojo.Dept;
import com.lxd.pojo.Emp;
public class TransDaoTest {
public static void main(String[] args) {
Dept dept = new Dept();
dept.setDeptno(50);
dept.setDname("it");
dept.setLoc("cs");
Emp emp = new Emp();
emp.setEmpno(9999);
emp.setEname("Tom");
emp.setSal(99d);
emp.setDeptno(10);
TransDao dao = new TransDao();
boolean flag = dao.trans(dept, emp);
if (flag) {
System.out.println("success");
} else {
System.out.println("error");
}
}
}
- jdbc.rar (299 Bytes)
- 下载次数: 0