在学习Java的初期阶段,制作一些小的练手项目时,在没有接触过日志框架的情况下实现简单的日志记录功能
Dao层代码:
//项目新增
public int addProject(Project project) throws SQLException{
Connection conn=JDBCUtils.getConn();
String sql="insert into project(pname,enterprise,information,plan) values(?,?,?,?)";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1,project.getPname());
pst.setString(2,project.getEnterprise());
pst.setString(3,project.getInformation());
pst.setString(4,project.getPlan());
String sql2="insert into logg(ldesc) values(?)";
PreparedStatement pst2=conn.prepareStatement(sql2);
pst2.setString(1,project.getLdesc());
int row=pst.executeUpdate();
int row2=pst2.executeUpdate();
JDBCUtils.close(conn, pst);
if(row>0&&row2>0){
return row;
}else{
return 0;
}
}
Service层代码:
//项目新增
public int addProject(Project project){
int row=0;
try {
row=projectDao.addProject(project);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
Controller层代码:
//新增项目
public int addProject(String pname,String enterprise,String information,String plan,String ldesc){
Project project=new Project();
project.setPname(pname);
project.setEnterprise(enterprise);
project.setInformation(information);
project.setPlan(plan);
project.setLdesc(ldesc);
int row=projectService.addProject(project);
return row;
}
View层代码:
// 新增功能
public void addProject(int unumber) {
Scanner sc = new Scanner(System.in);
System.out.println("--------新增项目--------");
System.out.println("请输入项目名称:");
String pname = sc.next();
System.out.println("请输入公司名称:");
String enterprise = sc.next();
System.out.println("请输入项目需求:");
String information = sc.next();
System.out.println("请输入工作计划:");
String plan = sc.next();
String ldesc = projectController.getUserUname(unumber) + "新建了一个" + pname + "项目";
// 调用controller层的新增方法
int row = projectController.addProject(pname, enterprise, information, plan, ldesc);
// js代码判断
if (row > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
}
可以简单的实现日志记录的功能