首先实体类Person
package com.henu.dao;
import java.io.Serializable;
public class Person implements Serializable{
private Integer id;
private String name;
private Integer sex;
private Integer age;
private String mobile;
private String address;
public Person() {
super();
}
public Person(Integer id, String name, Integer sex, Integer age, String mobile, String address) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.mobile = mobile;
this.address = address;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", mobile=" + mobile
+ ", address=" + address + "]";
}
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 Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
DAO模型设计:PersonDAO
package com.henu.dao;
/**
* 捕获异常快捷键:ALT+SHIFT+Z
*ps.executeUpdate();发送DML sql语句 ,代表当前操作影响的数据库的数据行数
*ps.executeQuery();发送DQL语句,查询结果会被放在返回值ResultSet对象
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Utils.JDBCUtil3;
public class PersonDAO {
/**
* 1.将Person对象插入到T_PERSON表中:insert
* 功能:将接收的person参数中的数据,插入到T_PERSON表中
* 参数:被插入到数据库中的person
* 返回值:void
*/
public void insert(Person person){
Connection conn = null;
PreparedStatement ps = null;
//1.获得连接
conn = JDBCUtil3.getConnection();
//2.创建发送sql的工具
try {
ps = conn.prepareStatement(
"insert into T_PERSON values(person_sep.nextval,?,?,?,?,?)");
//3.绑定参数
ps.setString(1, person.getName());//person对象的name属性值---insert语句的name位置
ps.setInt(2, person.getSex());
ps.setInt(3, person.getAge());
ps.setString(4, person.getMobile());
ps.setString(5, person.getAddress());
//4.发送参数,执行sql
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("添加数据异常!",e);
}
//5.释放资源
JDBCUtil3.close(null, ps, conn);
}
//2.删除数据库中的T_PERSON表中的数据
/**
* 参数:id
*/
public void delete(Integer id){
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接
conn = JDBCUtil3.getConnection();
try {
//2.创建ps
ps = conn.prepareStatement("delete from T_PERSON where id=?");
//3.绑定参数
ps.setInt(1, id);
//4.发送参数,执行sql
ps.executeUpdate();
System.out.println("id为"+id+"的行被删除");
//5.释放资源
JDBCUtil3.close(null, ps, conn);
} catch (SQLException e) {
throw new RuntimeException("删除异常!",e);
}
}
//3.修改表中的一条数据:调用给person对象,修改到表
//思考:根据id将其他属性值全部修改
public void update(Person person){
Connection conn = null;
PreparedStatement ps = null;
conn = JDBCUtil3.getConnection();
try {
ps = conn.prepareStatement("update T_PERSON set name=?,sex=?,age=?,mobile=?,address=? where id=?");
ps.setString(1, person.getName());//person对象的name属性值---update语句的name位置
ps.setInt(2, person.getSex());
ps.setInt(3, person.getAge());
ps.setString(4, person.getMobile());
ps.setString(5, person.getAddress());
ps.setInt(6, person.getId());
int i = ps.executeUpdate();
if (i != 0) {
System.out.println("更新成功!");
}else {
System.out.println("更新失败!");
}
} catch (SQLException e) {
throw new RuntimeException("修改错误!",e);
}
}
/**
* 4.根据id,查询数据库中的1条并封装成一个Entity对象,select * from T_PEROSN where id = ?
* 功能:根据ID查询数据库T_PERSON表中的一条数据
* 参数:Integer id, 接受要查询的id
* 返回值:Person对象,封装id对应的一行数据
*/
public Person serlectById(Integer id){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.获得连接
conn = JDBCUtil3.getConnection();
try {
//2.创建ps
ps = conn.prepareStatement(
"select id,name,sex,age,mobile,address from T_PERSON where id=?");
//3.绑定参数
ps.setInt(1, id);
//4.发送sql参数
rs = ps.executeQuery();
//5.处理结果集[ResultSet->Person]
if (rs.next()) {
//获得每一个rs值
Integer ids = rs.getInt("id");
String name = rs.getString("name");
int sex = rs.getInt(3);
int age = rs.getInt(4);
String mobile = rs.getString(5);
String address = rs.getString(6);
//将每一个值封装到在Person对象中
Person person = new Person(ids,name,sex,age,mobile,address);
//返回
return person;
}
//6.释放资源
} catch (SQLException e) {
throw new RuntimeException("查询异常!",e);
}
return null ;
}
/**
* 查询全部
*/
public List<Person> selectAllPerson(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Person> list = new ArrayList<Person>();
conn = JDBCUtil3.getConnection();
try {
ps = conn.prepareStatement("select * from T_PERSON");
rs = ps.executeQuery();
while(rs.next()){
//获得每一个rs值
Integer ids = rs.getInt("id");
String name = rs.getString("name");
int sex = rs.getInt(3);
int age = rs.getInt(4);
String mobile = rs.getString(5);
String address = rs.getString(6);
//将每一个值封装到在Person对象中
Person person = new Person(ids,name,sex,age,mobile,address);
/*
* Person person = new Person(rs.getInt("id"),rs.getString("name"),
* rs.getInt(3),rs.getInt(4),rs.getString(5),rs.getString(5));
*/
//将对象放入集合
list.add(person);
}
} catch (SQLException e) {
throw new RuntimeException("查询全部异常!",e);
}finally {
JDBCUtil3.close(rs, ps, conn);
}
return list;
}
}
测试类:TestDao
package com.henu.dao;
import java.util.List;
public class TestDao {
public static void main(String[] args) {
// /**
// * 准备一个java对象
// */
// Person person = new Person(5,"李孟冬",1,18,"13949586036","河南");
// //将对象插入到数据库
// //1.创建PersonDao对象 insert
// PersonDAO personDAO = new PersonDAO();
// //2.调用insert
// personDAO.insert(person);
// /**
// * 测试PersonDAO对象中根据id查询person信息的方法
// */
// Integer id = 1;
// //1.创建PersonDAO对象
// PersonDAO personDAO = new PersonDAO();
// //2.调用selectById方法
// Person person = personDAO.serlectById(id);
// System.out.println(person);
// /**
// * 删除
// */
// Integer id = 1;
// PersonDAO personDAO = new PersonDAO();
// personDAO.delete(id);
// /**
// * 查询全部
// */
// PersonDAO personDAO = new PersonDAO();
// List<Person> list = personDAO.selectAllPerson();
// System.out.println(list);
/**
* 更新数据
*/
Person person = new Person(5,"董康",0,18,"13949586036","河南");
PersonDAO personDAO = new PersonDAO();
personDAO.update(person);
}
}
补充JDBCUtil3代码:
package Utils;
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 JDBCUtil3 {
private static final Properties props = new Properties();
//配置文件加载 执行一次
static{
//1.输入流
InputStream is = null;
//获得类加载的输入流getResourceAsStream 读取src目录下的文件
is = JDBCUtil3.class.getResourceAsStream("/Utils/jdbc.properties");
try {
props.load(is);
} catch (IOException e) {
throw new RuntimeException("配置文件加载异常!",e);
}finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取连接
*/
public static Connection getConnection(){
Connection conn = null;
try {
//1.加载驱动
// Class.forName("oracle.jdbc.OracleDriver");
Class.forName(props.getProperty("driverClassName"));
//2.获取conn
// conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","123456");
conn = DriverManager.getConnection(props.getProperty("url"),
props.getProperty("user"),props.getProperty("password"));
} catch (Exception e) {
throw new RuntimeException("获取数据库连接异常!");
}
return conn;
}
/**
* 释放资源:关闭 rs ps conn
*/
public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException("rs资源释放异常!");
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException("ps资源释放异常!");
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException("conn资源释放异常!");
}
}
}
}
jdbc.properties文件内容
driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
user=hr
password=123456
keep moving!!!