信息写不进数据库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>日 期</td><td><input type="text" name="date"</td>
<td>货物名</td><td><input type="text" name="gName"</td>
<td>颜 色</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 java.io.*;
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 数据库
----------------解决方案--------------------------------------------------------
<form action="helpInput" name="form">
你在看看你的表单提交,这里没方法啊, <form action="helpInput" name="form" method="post">
还有你的XML配置了吗?
----------------解决方案--------------------------------------------------------
回复 2楼 czj11600
这个吗?
----------------解决方案--------------------------------------------------------
<form action="helpInput" name="form" method="post">
问下,不指定具体的Jsp处理页面可以吗? action="helpInput.jsp"
----------------解决方案--------------------------------------------------------
回复 4楼 问中学中问
这个表单中的action有问题,不出意外,你的程序会有404异常,原因看你的配置文件中<url-pattern>/HandleInput</url-pattern>的H是大写,但是页面中的action处H却是小写,这两个必须保持一致才行。,只能看出这点问题,写入数据库的操作看起来没有大问题,但是得注意数据库那边是否可以连通,是否有异常抛出等 ----------------解决方案--------------------------------------------------------
回复 5楼 baifenghan
我在定义servlet时就是定义的helpInput的 ----------------解决方案--------------------------------------------------------
如果不是这个问题,就没有错误,你的程序我跑了一下,没有问题
----------------解决方案--------------------------------------------------------
回复 7楼 baifenghan
你跑了下可以写进数据库的?为什么我的机子上就是写不进去啊!郁闷! ----------------解决方案--------------------------------------------------------
同意四楼的
----------------解决方案--------------------------------------------------------