无法在数据库中插入字段

时间:2014-07-19 05:56:58

标签: java mysql jsp servlets

我是java web开发的新手。我正在尝试开发一个项目。我得到SQLException错误。

这是我的servlet代码:

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    // TODO Auto-generated method stub
    res.setContentType("text/html");
    PrintWriter pw=res.getWriter();

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ocp2","root", "");
        ps=con.createStatement();
        RequestDispatcher rr;

        String courseid=req.getParameter("courseid");
        String coursename=req.getParameter("coursename");
        String s1=req.getParameter("sday");
        String s2=req.getParameter("smonth");
        String s3=req.getParameter("syear");
        String sdate=s1+'-'+s2+'-'+s3;
        String s4=req.getParameter("eday");
        String s5=req.getParameter("emonth");
        String s6=req.getParameter("eyear");
        String edate=s4+'-'+s5+'-'+s6;
        String duration=req.getParameter("duration");
        String fee=req.getParameter("fee");

        pw.println("insert into course values('"+courseid+"','"+coursename+"','"+sdate+"','"+edate+"','"+duration+"',"+fee+")");
        ps.executeUpdate("insert into course values('"+courseid+"','"+coursename+"','"+sdate+"','"+edate+"','"+duration+"',"+fee+")");
        con.commit();

        rr= req.getRequestDispatcher("incousuc.html");
        rr.forward(req,res);
    } catch(ClassNotFoundException e) {
        pw.println("exception"+e);
    } catch(SQLException e) {
        pw.println("SQL EXCEPTION");    
        pw.println("TRY AGAIN");
    } catch(Exception e) {
        pw.println("can't load driver"+e.getMessage());
    }
}

这是我的HTML代码:

</table>
</td>

<td width="75%" align="center">
    <h2>New Course Details</h2>
    <form name="facreg" onsubmit="return check()" action="./inscourse" method="post">
        <table width="500" height="30" border="0">
            <tr>
                <td>
                    <center>courseid</center>
                </td>
                <td>
                    <input id="courseid" name="courseid" type="text" size="50" maxlength="100" value="">
                </td>
            </tr>
            <tr>
                <td>
                    <center>coursename</center>
                </td>
                <td>
                    <input id="couname" name="couname" type="text" size="50" maxlength="100" value="">
                </td>
            </tr>
            <tr>
                <td>StartingDate</td>
                <td>
                    <select name="sday" size="">
                        <option value=01>1</option>
                        <option value=02>2</option>
                        <option value=03>3</option>
                        <option value=04>4</option>
                        <option value=05>5</option>
                        <option value=06>6</option>
                        <option value=07>7</option>
                        <option value=08>8</option>
                        <option value=09>9</option>
                        <option value=10>10</option>
                        <option value=11>11</option>
                        <option value=12>12</option>
                        <option value=13>13</option>
                        <option value=14>14</option>
                        <option value=15>15</option>
                        <option value=16>16</option>
                        <option value=17>17</option>
                        <option value=18>18</option>
                        <option value=19>19</option>
                        <option value=20>20</option>
                        <option value=21>21</option>
                        <option value=22>22</option>
                        <option value=23>23</option>
                        <option value=24>24</option>
                        <option value=25>25</option>
                        <option value=26>26</option>
                        <option value=27>27</option>
                        <option value=28>28</option>
                        <option value=29>29</option>
                        <option value=30>30</option>
                        <option value=31>31</option>
                    </select>&nbsp;&nbsp;&nbsp;&nbsp;
                    <select name="smonth" size="">
                        <option value="Jan">Jan</option>
                        <option value="Feb">Feb</option>
                        <option value="Mar">Mar</option>
                        <option value="Apr">Apr</option>
                        <option value="May">May</option>
                        <option value="Jun">Jun</option>
                        <option value="Jul">Jul</option>
                        <option value="Aug">Aug</option>
                        <option value="Sep">Sep</option>
                        <option value="Oct">Oct</option>
                        <option value="Nov">Nov</option>
                        <option value="Dec">Dec</option>
                    </select>&nbsp;&nbsp;&nbsp;&nbsp;
                    <input id="syear" name="syear" type="text" size="8" maxlength="4" value="" onkeypress=checknumeric()>(yyyy)&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td>EndingDate</td>
                <td>
                    <select name="eday" size="">
                        <option value=01>1</option>
                        <option value=02>2</option>
                        <option value=03>3</option>
                        <option value=04>4</option>
                        <option value=05>5</option>
                        <option value=06>6</option>
                        <option value=07>7</option>
                        <option value=08>8</option>
                        <option value=09>9</option>
                        <option value=10>10</option>
                        <option value=11>11</option>
                        <option value=12>12</option>
                        <option value=13>13</option>
                        <option value=14>14</option>
                        <option value=15>15</option>
                        <option value=16>16</option>
                        <option value=17>17</option>
                        <option value=18>18</option>
                        <option value=19>19</option>
                        <option value=20>20</option>
                        <option value=21>21</option>
                        <option value=22>22</option>
                        <option value=23>23</option>
                        <option value=24>24</option>
                        <option value=25>25</option>
                        <option value=26>26</option>
                        <option value=27>27</option>
                        <option value=28>28</option>
                        <option value=29>29</option>
                        <option value=30>30</option>
                        <option value=31>31</option>
                    </select>&nbsp;&nbsp;&nbsp;&nbsp;
                    <select name="emonth" size="">
                        <option value="Jan">Jan</option>
                        <option value="Feb">Feb</option>
                        <option value="Mar">Mar</option>
                        <option value="Apr">Apr</option>
                        <option value="May">May</option>
                        <option value="Jun">Jun</option>
                        <option value="Jul">Jul</option>
                        <option value="Aug">Aug</option>
                        <option value="Sep">Sep</option>
                        <option value="Oct">Oct</option>
                        <option value="Nov">Nov</option>
                        <option value="Dec">Dec</option>
                    </select>&nbsp;&nbsp;&nbsp;&nbsp;
                    <input id="eyear" name="eyear" type="text" size="8" maxlength="4" value="" onkeypress=checknumeric()>(yyyy)&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <center>duration</center>
                </td>
                <td>
                    <input id="duration" name="duration" type="text" size="50" maxlength="100" value="">
                </td>
            </tr>
            <tr>
                <td>
                    <center>Fee</center>
                </td>
                <td>
                    <input id="fee" name="fee" type="text" size="50" maxlength="100" value="" onkeypress="checknumeric()">
                </td>
            </tr>
            <tr>
                <td colspan=2>
                    <center>
                        <input name="submit" type="submit" value="SUBMIT">
                        <input name="reset" type="reset" value="RESET">
                </td>
            </tr>
        </table>
    </form>
</td>
</tr>
</table>

当我运行此代码时: 我收到了以下错误:

Jul 19, 2014 11:36:18 AM org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already.  Could not load java.net.BindException.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1588)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at com.mysql.jdbc.SQLError.createLinkFailureMessageBasedOnHeuristics(SQLError.java:1275)
    at com.mysql.jdbc.exceptions.jdbc4.CommunicationsException.<init>(CommunicationsException.java:60)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3321)
    at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1667)
    at com.mysql.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:4322)
    at com.mysql.jdbc.ConnectionImpl.cleanup(ConnectionImpl.java:1348)
    at com.mysql.jdbc.ConnectionImpl.finalize(ConnectionImpl.java:2679)
    at java.lang.System$2.invokeFinalize(Unknown Source)
    at java.lang.ref.Finalizer.runFinalizer(Unknown Source)
    at java.lang.ref.Finalizer.access$100(Unknown Source)
    at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
    Jul 19, 2014 11:36:18 AM org.apache.catalina.loader.WebappClassLoader loadClass
    INFO: Illegal access: this web application instance has been stopped already.  Could not load   com.mysql.jdbc.ProfilerEventHandlerFactory.  The eventual following stack trace is caused by an  error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
    java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1588)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at com.mysql.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:4346)
    at com.mysql.jdbc.ConnectionImpl.cleanup(ConnectionImpl.java:1348)
    at com.mysql.jdbc.ConnectionImpl.finalize(ConnectionImpl.java:2679)
    at java.lang.System$2.invokeFinalize(Unknown Source)
    at java.lang.ref.Finalizer.runFinalizer(Unknown Source)
    at java.lang.ref.Finalizer.access$100(Unknown Source)
    at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

我的数据库也没有更新。 谁能告诉我我在哪里做错了?我该如何解决呢?

1 个答案:

答案 0 :(得分:3)

你得到coursename的空值你必须改变这行servlet代码

   String coursename=req.getParameter("coursename");

这一个

   String coursename=req.getParameter("couname");

检查你的HTML代码,你会得到理由