DAO数据库操作和业务操作分离
- entity就是实体类
- DAO就是操作数据库的工具类
通俗讲:就是把操作数据库和保存查询出来的零散数据都封装起来,使用起来更方便,更系统。不用在test类敲数据库的业务。
DAO层 专门操作数据库增删查改的类,和测试类分离
package admin;
import admin.DButils.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/*
增删改查 查多个
* */
public class PersonDaoImpl {
// 增
public int insert(Person person) throws SQLException {
Connection connection=null;
PreparedStatement ps=null;
try {
String sql="insert into admin(username,PASSWORD,phone,address) values(?,?,?,?) ";
connection = DButil.gerConnection();
ps = connection.prepareStatement(sql);
ps.setString(1,person.getUsername());
ps.setString(2,person.getPASSWORD());
ps.setString(3,person.getPhone());
ps.setString(4,person.getAddress());
int i = ps.executeUpdate(); // 受影响行数
return i;
}catch (SQLException e){
e.printStackTrace();
}finally {
DButil.toclose(connection,ps,null);
}
return 0;
}
// 删
public int delect(int id) throws SQLException {
Connection connection = null;
PreparedStatement ps=null;
String sql="DELETE FROM `admin` WHERE `id`=?;";
try {
connection=DButil.gerConnection();
ps = connection.prepareStatement(sql);
ps.setInt(1,id);
int i = ps.executeUpdate();
return i;
}catch (SQLException e){
e.printStackTrace();
}finally {
DButil.toclose(connection,ps,null);
}
return 0;
}
// 改
public int updata(Person person) throws SQLException {
Connection connection=null;
PreparedStatement ps=null;
String sql = "update `admin` set username=?,PASSWORD=?,phone=?,address=? where id = ?";
try {
connection = DButil.gerConnection();
ps = connection.prepareStatement(sql);
ps.setString(1,person.getUsername());
ps.setString(2,person.getPASSWORD());
ps.setString(3,person.getPhone());
ps.setString(4,person.getAddress());
ps.setInt(5,person.getId());
int i = ps.executeUpdate();
return i;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DButil.toclose(connection,ps,null);
}
return 0;
}
// 查单个
public Person getdata(int id) throws SQLException {
Connection connection=null;
PreparedStatement ps=null;
ResultSet resultSet=null;
String sql="SELECT * FROM `admin` where `id`=?;";
try {
connection=DButil.gerConnection();
ps=connection.prepareStatement(sql);
ps.setInt(1,id);
resultSet=ps.executeQuery();
Person person=null;
if (resultSet.next()){
int id1 = resultSet.getInt("id");
String username = resultSet.getString("username");
String password = resultSet.getString("PASSWORD");
String phone = resultSet.getString("phone");
String address = resultSet.getString("address");
person=new Person(id1,username,password,phone,address);
}
return person;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DButil.toclose(connection,ps,resultSet);
}
return null;
}
// 查所有
public List<Person> getAlldata() throws SQLException {
Connection connection=null;
PreparedStatement ps=null;
ResultSet resultSet=null;
Person person1=null;
List<Person> personList=new ArrayList<>();
String sql="SELECT * FROM `admin` ;";
try {
connection = DButil.gerConnection();
ps=connection.prepareStatement(sql);
resultSet = ps.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String username = resultSet.getString("username");
String PASSWORD = resultSet.getString("PASSWORD");
String phone = resultSet.getString("phone");
String address = resultSet.getString("address");
person1 = new Person(id, username, PASSWORD, phone, address);
personList.add(person1);
}
return personList;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DButil.toclose(connection,ps,resultSet);
}
return null;
}
}
实体类 用于保存零散数据的载体
package admin;
public class Person {
private String username;
private int id;
private String PASSWORD;
private String phone;
private String address;
public Person(int id) {
this.id = id;
}
public Person(int id, String username, String PASSWORD, String phone, String address) {
this.username = username;
this.id = id;
this.PASSWORD = PASSWORD;
this.phone = phone;
this.address = address;
}
public Person(String username, String PASSWORD, String phone, String address) {
this.username = username;
this.PASSWORD = PASSWORD;
this.phone = phone;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPASSWORD() {
return PASSWORD;
}
public void setPASSWORD(String PASSWORD) {
this.PASSWORD = PASSWORD;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"username='" + username + '\'' +
", PASSWORD='" + PASSWORD + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
}
DButil工具类 用于连接 数据库 和获取,Connection,释放资源
package admin.DButils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DButil {
private static final Properties PROPERTIES=new Properties();
static {
InputStream is=DButil.class.getResourceAsStream("/db.properties");
try {
PROPERTIES.load(is);
Class.forName(PROPERTIES.getProperty("driver"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection gerConnection() throws SQLException {
Connection connection=null;
return connection= DriverManager.getConnection(PROPERTIES.getProperty("url"),PROPERTIES.getProperty("username"),PROPERTIES.getProperty("password"));
}
public static void toclose(Connection connection, PreparedStatement ps, ResultSet resultSet ) throws SQLException {
if (connection!=null) {
connection.close();
}
if (ps!=null) {
ps.close();
}
if (resultSet!=null) {
resultSet.close();
}
}
}
查多个
package admin.DButils;
import admin.Person;
import admin.PersonDaoImpl;
import java.sql.SQLException;
import java.util.List;
public class selectPerson {
public static void main(String[] args) throws SQLException {
// System.out.println("-----------查单个-----------");
// PersonDaoImpl personDao=new PersonDaoImpl();
// Person getdata = personDao.getdata(5);
// if (getdata!=null){
// System.out.println(getdata);
// }else {
// System.out.println("NO");
// }
System.out.println("-----------查多个-----------");
PersonDaoImpl personDao=new PersonDaoImpl();
List<Person> alldata = personDao.getAlldata();
for (Person Allperson : alldata) {
System.out.println(Allperson);
}
}
}
增。删。改
package admin;
import java.sql.SQLException;
public class testPerson {
public static void main(String[] args) throws SQLException {
// -------------------------------------------------增
// PersonDaoImpl personDao = new PersonDaoImpl();
// Person person=new Person("wkl","1234","1234567","广东省");
// int insert = personDao.insert(person);
// System.out.println(" -------------------------------------------------增");
//
// if (insert==1){
// System.out.println("OK "+insert);
// }else {
// System.out.println("ON");
// }
// -------------------------------------------------改
// Person person1=new Person(5,"wkl999","1571681","18171273","广东省");
// PersonDaoImpl personDao1=new PersonDaoImpl();
// int updata = personDao1.updata(person1);
// System.out.println(" -------------------------------------------------改");
// if (updata==1){
// System.out.println("OK "+updata);
// }else {
// System.out.println("ON");
// }
// -------------------------------------------------删
Person person2=new Person(7);
PersonDaoImpl personDao2=new PersonDaoImpl();
int delect = personDao2.delect(person2.getId());
if (delect!=0){
System.out.println("OK "+delect);
}else {
System.out.println("NO");
}
}
}
查询的数据打印(多个)
-------------------------删
Person person2=new Person(7);
PersonDaoImpl personDao2=new PersonDaoImpl();
int delect = personDao2.delect(person2.getId());
if (delect!=0){
System.out.println("OK "+delect);
}else {
System.out.println(“NO”);
}
}
}
打印结果(多个)
-----------查多个-----------
Person{username='wkl', PASSWORD='123456', phone='136456534', address='中国广东省广州市'}
Person{username='wkl', PASSWORD='1234', phone='1234567', address='gd'}
Person{username='wkl999', PASSWORD='1571681', phone='18171273', address='广东省'}
Person{username='wkl', PASSWORD='1234', phone='1234567', address='广东省'}
Person{username='wkl999', PASSWORD='1571681', phone='18171273', address='广东省'}
Person{username='wkl', PASSWORD='1234', phone='1234567', address='广东省'}
Process finished with exit code 0