本系统包含两个模块:仓库模块、记录模块
仓库模块功能:入库,出库,修改,删除,查询
记录模块功能:查询
接下来展示代码:
首先确定涉及到两个表,那就要做两个Bean类
public class Goods {
private int id;
private String name;
private int number;
public Goods(){
super();
}
public Goods(int id, String name, int number) {
this.id = id;
this.name = name;
this.number = number;
}
public Goods(String name, int number) {
this.name = name;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
import java.time.LocalDate;
public class Lists {
private int id;
private String name;
private double score;
private LocalDate birth;
private String city;
public String getWay() {
return way;
}
public void setWay(String way) {
this.way = way;
}
private String way;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public LocalDate getBirth() {
return birth;
}
public void setBirth(LocalDate birth) {
this.birth = birth;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Lists(String name, double score, LocalDate birth, String city,String way) {
super();
this.name = name;
this.score = score;
this.birth = birth;
this.city = city;
this.way = way;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", score=" + score + ", birth=" + birth + ", city=" + city
+", way=" + way
+ "]";
}
public Lists() {
super();
}
}
Util类
既然涉及到数据库,那就离不开JDBC,所以首先要创建工具类,同时还有一个数据处理的工具类
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtil {
public static final ComboPooledDataSource ds=new ComboPooledDataSource();
static
{
//加载连接数据库的信息
try {
InputStream is =new FileInputStream("jdbc.properties");
Properties porp =new Properties();
porp.load(is);
is.close();
//获取连接数据库信息
String user =porp.getProperty("user");
String pwd =porp.getProperty("pwd");
String url=porp.getProperty("url");
String driver =porp.getProperty("driver");
//设置链接库的信息
ds.setUser(user);
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setPassword(pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void release(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
public class DateUtil {
//将日期转换为字符串
public static LocalDate dateToLocalDate(Date date) {
return LocalDate.ofEpochDay(date.getTime()/86400000).plusDays(1);
}
public static Object[][] listToArray(List<Lists> stus){
Object[][] data=new Object[stus.size()][];
int size=stus.size();
for(int i=0;i<size;i++) {
Lists s = stus.get(i);
Object[] info =new Object[] {
s.getId(),s.getName(),s.getScore(),s.getBirth(),s.getCity(),s.getWay()};
data[i]=info;
}
return data;
}
public static Object[][] listToArray2(List<Goods> stus){
Object[][] data=new Object[stus.size()][];
int size=stus.size();
for(int i=0;i<size;i++) {
Goods s = stus.get(i);
Object[] info =new Object[] {
s.getId(),s.getName(),s.getNumber()};
data[i]=info;
}
return data;
}
}
接下来是Dao层
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 ListDao {
public void add(Lists s) throws SQLException {
Connection conn = JdbcUtil.getConnection();
String sql="insert into stus values(null,?,?,?,?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, s.getName());
ps.setDouble(2, s.getScore());
ps.setObject(3, s.getBirth());
ps.setString(4, s.getCity());
ps.setString(5, s.getWay());
ps.executeUpdate();
JdbcUtil.release(conn);
}
public void add2(Goods s) throws SQLException {
Connection conn = JdbcUtil.getConnection();
String sql="insert into good values(null,?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, s.getName());
ps.setInt(2, s.getNumber());
System.out.println(s.getName()+"..."+s.getNumber());
ps.executeUpdate();
JdbcUtil.release(conn);
}
public void detele2(int id) throws SQLException {
Connection conn = JdbcUtil.getConnection();
String sql="delete from good where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
JdbcUtil.release(conn);
}
public void update2(Goods s) throws SQLException {
Connection conn = JdbcUtil.getConnection();
String sql