标题:信息写不进数据库jsp+servlet
只看楼主
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
结帖率:87.5%
 问题点数:0 回复次数:8 
信息写不进数据库jsp+servlet
我想通过input.jsp页面吧信息写入数据库,但写不进去,求高人帮忙!
附上我的代码,大家顺便帮我看下
input.jsp
<%@page contentType="text/html; charset=GB2312"%>
<html>
    <center><table>
                 <form action="helpInput" name="form">
                     <tr><td colspan="5" align="center"><b>输入你今天的销售信息,如无填0</b></td></tr>
                     <center><table>
                             <tr><td>日&nbsp;&nbsp;期</td><td><input type="text" name="date"</td>
                                 <td>货物名</td><td><input type="text" name="gName"</td>
                                 <td>颜&nbsp;&nbsp;色</td><td><input type="text" name="color"></td>
                                 <td>进货价</td><td><input type="text" name="bPrice"></td>
                                 <td>进货数量</td><td><input type="text" name="bCount"</td>
                             </tr>
                             <tr><td>售出价</td><td><input type="text" name="sPrice"></td>
                                 <td>售出数量</td><td><input type="text" name="sCount"</td>
                                 <td>收邮递费</td><td><input type="text" name="iPostage"</td>
                                 <td>出邮递费</td><td><input type="text" name="oPostage"</td>
                                 <td>赠品价</td><td><input type="text" name="gPrice"></td>
                             </tr>
                       </table></center>
                     <tr><td colspan="10" align="right"><a href="scanMess.jsp">查看销售单</a><input type="submit" value="提交"></td>
                     </tr>
                  </form>
</table></center>
</html>
这个是控制输入的javabean
Input.java
package mybean.data;
public class Input
{
    String date="",gName="",color="",bPrice="",sPrice="",iPostage="",oPostage="",gPrice="", bCount="",sCount="";
    String backNews;
    public void setdate(String date)
    {
        this.date=date;
    }
    public String getdate()
    {
        return date;
    }
    public void setgName(String gName)
    {
        this.gName=gName;
    }
    public String getgName()
    {
        return gName;
    }
    public void setcolor(String color)
    {
        this.color=color;
    }
    public String getcolor()
    {
        return color;
    }
    public void setbPrice(String bPrice)
    {
        this.bPrice=bPrice;
    }
    public String getbPrice()
    {
        return bPrice;
    }
    public void setbCount(String bCount)
    {
        this.bCount=bCount;
    }
    public String getbCount()
    {
        return bCount;
    }
    public void setsPrice(String sPrice)
    {
        this.sPrice=sPrice;
    }
    public String getsPrice()
    {
        return sPrice;
    }
     public void setsCount(String sCount)
    {
        this.sCount=sCount;
    }
    public String getsCount()
    {
        return sCount;
    }
    public void setiPostage(String iPostage)
    {
        this.iPostage=iPostage;
    }
    public String getiPostage()
    {
        return iPostage;
    }
    public void setoPostage(String oPostage)
    {
        this.oPostage=oPostage;
    }
    public String getoPostage()
    {
        return oPostage;
    }
    public void setgPrice(String gPrice)
    {
        this.gPrice=gPrice;
    }
    public String getgPrice()
    {
        return gPrice;
    }
    public void setbackNews(String backNews)
    {
        this.backNews=backNews;
    }
   public String getbackNews()
   {
       return backNews;
   }
}
这个是servlet
HandleInput.java
package myservlet.control;
import mybean.data.*;
import java.sql.*;
import *;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleInput extends HttpServlet
{
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        try
        {
            //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        catch(ClassNotFoundException e)
        {
            System.out.print(e);
        }
    }
    public String handleString(String s)
    {
        try
        {
            byte bb[]=s.getBytes("iso-8859-1");
            s=new String(bb);
        }
        catch(Exception ee)
        {
            System.out.print(ee);
        }
        return s;
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException
    {
        Connection con;
        PreparedStatement sql;
        Input inp=new Input();
        request.setAttribute("input",inp);
        String date=request.getParameter("date").trim(),
                gName=request.getParameter("gName").trim(),
                color=request.getParameter("color").trim(),
                bPrice=request.getParameter("bPrice").trim(),
                sPrice=request.getParameter("bCount").trim(),
                iPostage=request.getParameter("sPrice").trim(),
                oPostage=request.getParameter("sCount").trim(),
                gPrice=request.getParameter("iPostage").trim(),
                bCount=request.getParameter("oPostage").trim(),
                sCount=request.getParameter("gPrice").trim();
        String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Good";
        if(date==null) date="";
        if(gName==null) gName="";
        if(color==null) color="";
        if(bPrice==null) bPrice="";
        if(bCount==null) bCount="";
        if(sPrice==null) sPrice="";
        if(sCount==null) sCount="";
        if(iPostage==null) iPostage="";
        if(oPostage==null) oPostage="";
        if(gPrice==null) gPrice="";
        String backNews="";
        try
        {
            con=DriverManager.getConnection(uri,"sa","sa");
            String insertCondition="insert into Sell_Mess values(?,?,?,?,?,?,?,?,?,?)";
            sql=con.prepareStatement(insertCondition);
            if(true)
            {
                sql.setString(1,date);
                sql.setString(2,gName);
                sql.setString(3,color);
                sql.setString(4,bPrice);
                sql.setString(5,bCount);
                sql.setString(6,sPrice);
                sql.setString(7,sCount);
                sql.setString(8,iPostage);
                sql.setString(9,oPostage);
                sql.setString(10,gPrice);
                int m=sql.executeUpdate();
                if(m!=0)
                {
                    backNews="写入成功";
                    inp.setbackNews(backNews);
                    inp.setdate(date);
                    inp.setgName(gName);
                    inp.setcolor(color);
                    inp.setbPrice(bPrice);
                    inp.setbCount(bCount);
                    inp.setsPrice(sPrice);
                    inp.setsCount(sCount);
                    inp.setiPostage(iPostage);
                    inp.setoPostage(oPostage);
                    inp.setgPrice(gPrice);

                }
            }
            else
            {
                backNews="信息填写不完整
                inp.setbackNews(backNews);
            }
            con.close();
        }
        catch(SQLException exp)
        {
            backNews="写入失败";
            inp.setbackNews(backNews);
            System.out.print(exp);
        }
        RequestDispatcher dispatcher=request.getRequestDispatcher("test.jsp");  //写入成功跳转到test.jsp
        dispatcher.forward(request, response);

    }
    public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException
    {
        doPost(request,response);
    }
}






搜索更多相关主题的帖子: jsp servlet 数据库 
2010-02-11 14:45
czj11600
Rank: 1
等 级:新手上路
帖 子:25
专家分:8
注 册:2010-2-11
得分:0 
  <form action="helpInput" name="form">
 
你在看看你的表单提交,这里没方法啊,  <form action="helpInput" name="form" method="post">
还有你的XML配置了吗?

我很善良,嘿嘿。
2010-02-25 15:43
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
得分:0 
回复 2楼 czj11600

这个吗?
2010-02-26 14:58
问中学中问
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2009-12-22
得分:0 
<form action="helpInput" name="form" method="post">
    问下,不指定具体的Jsp处理页面可以吗? action="helpInput.jsp"
2010-02-27 14:13
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
得分:0 
回复 4楼 问中学中问
这个表单中的action有问题,不出意外,你的程序会有404异常,原因看你的配置文件中<url-pattern>/HandleInput</url-pattern>的H是大写,但是页面中的action处H却是小写,这两个必须保持一致才行。,只能看出这点问题,写入数据库的操作看起来没有大问题,但是得注意数据库那边是否可以连通,是否有异常抛出等
2010-02-28 04:42
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
得分:0 
回复 5楼 baifenghan
我在定义servlet时就是定义的helpInput的
2010-03-03 08:11
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
得分:0 
如果不是这个问题,就没有错误,你的程序我跑了一下,没有问题
2010-03-09 22:32
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
得分:0 
回复 7楼 baifenghan
你跑了下可以写进数据库的?为什么我的机子上就是写不进去啊!郁闷!
2010-03-10 12:28
柠檬可乐
Rank: 4
等 级:业余侠客
威 望:6
帖 子:66
专家分:205
注 册:2009-6-16
得分:0 
同意四楼的
2010-03-16 23:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-297581-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.070044 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved