老师请看下我的代码完成的如何?

老师请看下我的代码完成的如何?

这是项目结构

http://img1.sycdn.imooc.com//climg/5cdf90560001e10204780851.jpg

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mason.Dao;
 
 
import com.mason.Utils.ConnectionUtil;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
 
public class addCourseDao {
    public static boolean addCourse(String name, String category, String desp){
        Connection conn=null;
        PreparedStatement pstmt=null;
        boolean flag=false;
        try {
            //获得连接
            conn=ConnectionUtil.getConnection();
            //编写SQL语句
            String sql="INSERT course VALUES(NULL,?,?,?,NOW())";
            //预编译SQL
            pstmt = conn.prepareStatement(sql);
            //设置具体参数
            pstmt.setString(1,name);
            pstmt.setString(2,category);
            pstmt.setString(3,desp);
            //执行SQL
            int i = pstmt.executeUpdate();
            if (i > 0) {
                System.out.println("添加成功");
                flag=true;
            else {
                System.out.println("失败成功");
                flag=false;
            }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.release(conn,pstmt);
        }
        return flag;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.mason.Dao;
 
import com.mason.Utils.ConnectionUtil;
import com.mason.entity.Course;
 
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
 
//获取所有课程
public class selectCourseDao {
    public static ArrayList<Course> selectCourse() {
        //创建集合,存入查询到的每一个课程
        ArrayList<Course> courseList = new ArrayList<>();
        Connection conn=null;
        ResultSet rs=null;
        PreparedStatement pstmt=null;
        try {
            //获取连接
            conn=ConnectionUtil.getConnection();
            //编写SQL
            String sql="SELECT * FROM course";
            //预编译
            pstmt = conn.prepareStatement(sql);
            //执行SQL
            rs=pstmt.executeQuery();
            //遍历结果集,并把结果存入集合中
            while (rs.next()) {
                //从结果集中获取数据
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String category=rs.getString("category");
                String desp=rs.getString("desp");
                Date createTime = rs.getDate("createTime");
                //创建课程类
                Course course=new Course(id,name,category,desp,createTime);
                //存入查询到的课程集合中
                courseList.add(course);
            }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            ConnectionUtil.release(conn,rs,pstmt);
        }
        return courseList;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.mason.entity;
 
import java.util.Date;
 
/**
 * 括编号、名称、方向、描述、创建时间等属性及相关的get和set方法、构造方法
 */
public class Course {
    private Integer id;
    private String name;
    private String category;
    private String desp;
    private Date createTime;
 
    public Course() {
    }
 
    public Course(Integer id, String name, String category, String desp, Date createTime) {
        this.id = id;
        this.name = name;
        this.category = category;
        this.desp = desp;
        this.createTime = createTime;
    }
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getCategory() {
        return category;
    }
 
    public void setCategory(String category) {
        this.category = category;
    }
 
    public String getDesp() {
        return desp;
    }
 
    public void setDesp(String desp) {
        this.desp = desp;
    }
 
    public Date getCreateTime() {
        return createTime;
    }
 
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mason.servlet;
 
import com.mason.Dao.addCourseDao;
import com.mason.Dao.selectCourseDao;
import com.mason.entity.Course;
 
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
 
 
public class serviceServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)  throws IOException, ServletException {
        if (Objects.equals("/addCourse",req.getServletPath())){
            req.setCharacterEncoding("UTF-8");
            String name = req.getParameter("name");
            String category = req.getParameter("category");
            String desp = req.getParameter("desp");
            boolean flag = addCourseDao.addCourse(name, category, desp);
            if (flag) {
                resp.sendRedirect(req.getContextPath()+"/selectCourse");
            }else {
                resp.sendRedirect(req.getContextPath()+"/jsp/error.jsp");
            }
        }
        if (Objects.equals("/selectCourse", req.getServletPath())) {
            ArrayList<Course> courses = selectCourseDao.selectCourse();
            req.setAttribute("courses",courses);
            req.getRequestDispatcher("/jsp/showCourse.jsp").forward(req,resp);
 
        }
 
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.mason.Utils;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
/**
 * 主要用于获取和关闭数据库连接对象
 */
public class ConnectionUtil {
    //创建c3p0连接池对象
    private static final ComboPooledDataSource dataSource=new ComboPooledDataSource();
    //获取连接
    public static Connection getConnection() {
        Connection conn=null;
        try {
            conn=dataSource.getConnection();
        catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //关闭数据库连接对象
    public static void release(Connection conn, ResultSet rs, PreparedStatement pstmt) {
        if (conn!=null){
            try {
                conn.close();
            catch (SQLException e) {
                e.printStackTrace();
            }
            conn=null;
        }
        if (rs!=null){
            try {
                rs.close();
            catch (SQLException e) {
                e.printStackTrace();
            }
            rs=null;
        }
        if (pstmt!=null){
            try {
                pstmt.close();
            catch (SQLException e) {
                e.printStackTrace();
            }
            pstmt=null;
        }
    }
    public static void release(Connection conn,  PreparedStatement pstmt) {
        if (conn!=null){
            try {
                conn.close();
            catch (SQLException e) {
                e.printStackTrace();
            }
            conn=null;
        }
        if (pstmt!=null){
            try {
                pstmt.close();
            catch (SQLException e) {
                e.printStackTrace();
            }
            pstmt=null;
        }
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学的代码完成的非常不错,但是还有一点需要注意,类名首字母一定要大写。继续加油。

祝:学习愉快~

  • MasonM 提问者 #1
    好滴!谢谢老师!
    2019-05-18 18:14:41
提问者 MasonM 2019-05-18 13:01:22

运行结果:

http://img1.sycdn.imooc.com//climg/5cdf91540001916305200352.jpg

http://img1.sycdn.imooc.com//climg/5cdf915b0001c8e106320498.jpg

请老师评价一下我的代码怎样,有没有可以优化的地方

我运用了DAO层来访问数据库,并用了c3p0连接池,不知道做得怎样呢?

提问者 MasonM 2019-05-18 12:58:28

页面代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加课程</title>
</head>
<body>
<div>
    <h1>课程添加</h1>
    <form action="/addCourse">
        课程名<input type="text" name="name"></br>
        所属方向<input type="text" name="category"></br>
        课程描述<input type="text" name="desp">
        <input type="submit" value="添加">
    </form>
</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误页面</title>
</head>
<body>
出错咧
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>显示课表</title>
</head>
<style>
    table {
        border: 1px solid black;
    }
    tr,td{
        border: 1px solid black;
    }
</style>
<body>
<h1>添加成功</h1>
    <table>
        <tr>
            <td>课程名称</td>
            <td>所属方向</td>
            <td>课程描述</td>
            <td>创建时间</td>
        </tr>
        <c:forEach var="co" items="${courses}" varStatus="idx">
            <tr>
                <td>${co.name}</td>
                <td>${co.category}</td>
                <td>${co.desp}</td>
                <td>${co.createTime}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>



XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <welcome-file-list>
        <welcome-file>jsp/addCourse.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>serviceServlet</servlet-name>
        <servlet-class>com.mason.servlet.serviceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>serviceServlet</servlet-name>
        <url-pattern>/addCourse</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>serviceServlet</servlet-name>
        <url-pattern>/selectCourse</url-pattern>
    </servlet-mapping>
    <error-page>
        <error-code>404</error-code>
        <location>/jsp/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/jsp/error.jsp</location>
    </error-page>
</web-app>


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码