如何从Dao到Servlet
首先去bean建立class
在这里写上你所需要的数据
private String instockNumber,medicineNumber,instockTime,amount,remarks;
private double price,sellPrice;
右键空白处:
对应下面两张图片
右键选择ToString
根据你的class来敲出一个空的函数块,作为后面调用这个函数的接口
public InStock(){
}
一个bean的数据层就这样生成了
下一步写dao
dao 函数
首先使用c3p0连接池连接数据库
手动打开方法
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
//创建连接池
ComboPooledDataSource dataSource=new ComboPooledDataSource();
//设置连接池参数
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/pet+?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setMaxPoolSize(20);
dataSource.setInitialPoolSize(3);
//获得连接
conn=dataSource.getConnection();
//编写Sql这里开始编写代码下一个代码看这里
}catch(Exception e) {
e.printStackTrace();
}finally {
ConnectionUtils.release(rs, pstmt, conn);
}
下一步写SQL语句
String sql="insert into inv_inventory(instockNumber,medicineNumber,instockTime,price,sellingPrice,amount)"
+ "values(?,?,?,?,?,?)";
下一步预编译SQL
下一步填入数据12345对应的是问号的地方
下一步检查数据是否填入成功
最后返回str
调用dao函数的数据
然后新建一个Servlet
在doget写代码
采用工厂模式把表单的数据写入Map表里
假如数据是double类型要用强制转换
跳转页面
输出错误值
涉及到时间在系统后台生成,表单上不填写
关于时间的生成,后在存入bean层数据中
原文链接:https://blog.csdn.net/weixin_44691281/article/details/106543902