Javaweb-Dao方法
Dao
Dao(Data Access Object 数据存储对象):Java和数据库打交道的代码,jdbc是dao的一种表现形式
位于业务逻辑和持久化数据之间,实现对持久化数据的访问。
Dao在类和数据表之间起着转换器的作用,把实体类转化为数据库中的记录。
项目的结构:
(1)pojo/models实体类:存放所有数据库表的实体映射
package com.fruit.pojo;
public class Fruit {
private int fid;
private String fname;
private int price;
private int fcount;
private String remark;
public Fruit(){}
public Fruit(int fid, String fname, int price, int fcount, String remark) {
this.fid = fid;
this.fname = fname;
this.price = price;
this.fcount = fcount;
this.remark = remark;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public void setFname(String fname) {
this.fname = fname;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {this.price = price;}
public int getFcount() {
return fcount;
}
public void setFcount(int fcount) {
this.fcount = fcount;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getFname() {
return fname;
}
@Override
public String toString() {
return "水果{" +
"编号=" + fid +
", 名字='" + fname + '\'' +
", 价格=" + price +
", 重量=" + fcount +
", 备注='" + remark + '\'' +
'}';
}
}
(2)tools工具类:存放DB.java(连接数据库的方法)、db.properties(数据库连接信息)
package com.fruit.tools;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DB {
//4个数据库参数
static String driver = null;
static String url = null;
static String name = null;
static String pwd = null;
//3个操作对象
protected Connection conn = null;
protected PreparedStatement ps = null;
protected ResultSet rs = null;
//2个方法
public void getConn() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,name,pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
if(rs!=null) {
try {
rs.close();
if(ps!=null)
ps.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//1个静态代码块
static {
InputStream is = DB.class.getClassLoader().getResourceAsStream("db.properties");
Properties pp = new Properties();
try {
pp.load(is);
driver = pp.getProperty("driver");
url = pp.getProperty("url");
name = pp.getProperty("username");
pwd = pp.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
}
//db.properties
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username = root
password = 123456
(3)dao:存放Dao接口(XxxDao),里面定义对数据的操作方法,没有具体实现。
存放Dao接口中操作的具体实现(XxxDaoImpl)。
package com.fruit.dao;
import com.fruit.pojo.Fruit;
import java.util.List;
//dao接口
public interface FruitDao {
void add(Fruit f);
void delete();
void update();
List<Fruit> findFruit();
}
package com.fruit.dao.impl;
import com.fruit.dao.FruitDao;
import com.fruit.pojo.Fruit;
import com.fruit.tools.DB;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//接口的实现类
public class FruitDaoImpl extends DB implements FruitDao {
@Override
public void add(Fruit f) {
String sql = "insert into fruit values(?,?,?,?,?)";
getConn();
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,f.getFid());
ps.setString(2,f.getFname());
ps.setInt(3,f.getPrice());
ps.setInt(4,f.getFcount());
ps.setString(5,f.getRemark());
ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally {
close();
} }
@Override
public void delete() {
getConn();
String sql = "delete from fruit where fid =?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, 4);
ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
close();
}
}
@Override
public void update() {
getConn();
String sql ="UPDATE fruit SET `remark`=? WHERE fid = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,"很小");
ps.setInt(2,2);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
close();
}
}
@Override
public List<Fruit> findFruit() {
List<Fruit> list = new ArrayList<>();
getConn();
String sql = "select * from fruit";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Fruit fruit = new Fruit();
fruit.setFid(rs.getInt(1));
fruit.setFname(rs.getString(2));
fruit.setPrice(rs.getInt(3));
fruit.setFcount(rs.getInt(4));
fruit.setRemark(rs.getString(5));
list.add(fruit);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
close();
}
return list;
}
}
(4)test:存放具体的测试类,XxxDao fd = new XxxDaoImpl(); //父类引用指向子类对象
package com.fruit.test;
import com.fruit.dao.FruitDao;
import com.fruit.dao.impl.FruitDaoImpl;
import com.fruit.pojo.Fruit;
public class TestAdd {
public static void main(String[] args) {
FruitDao fd = new FruitDaoImpl(); //父类引用指向子类对象
Fruit f = new Fruit();
f.setFid(4);
f.setFname("grape");
f.setPrice(22);
f.setFcount(3);
f.setRemark("中等");
fd.add(f);
}
}
public class TestDelete {
public static void main(String[] args) {
FruitDao fd = new FruitDaoImpl();
fd.delete();
}
}
public class TestQuery {
public static void main(String[] args) {
FruitDao fruitDao = new FruitDaoImpl();
List<Fruit> fruit = fruitDao.findFruit();
for (int i = 0; i < fruit.size() ; i++) {
System.out.println(fruit.get(i).toString());
}
}
}
public class TestUpdate {
public static void main(String[] args) {
FruitDao fruitDao = new FruitDaoImpl();
fruitDao.update();
}
}
浙公网安备 33010602011771号