| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1505 人关注过本帖
标题:信息写不进数据库jsp+servlet
取消只看楼主 加入收藏
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
结帖率:87.5%
收藏
 问题点数:0 回复次数:3 
信息写不进数据库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
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
收藏
得分:0 
回复 2楼 czj11600
图片附件: 游客没有浏览图片的权限,请 登录注册

这个吗?
2010-02-26 14:58
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
收藏
得分:0 
回复 5楼 baifenghan
我在定义servlet时就是定义的helpInput的
2010-03-03 08:11
didozhp
Rank: 2
等 级:论坛游民
帖 子:38
专家分:20
注 册:2009-8-7
收藏
得分:0 
回复 7楼 baifenghan
你跑了下可以写进数据库的?为什么我的机子上就是写不进去啊!郁闷!
2010-03-10 12:28
快速回复:信息写不进数据库jsp+servlet
数据加载中...
 
   



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

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