信息写不进数据库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 *;
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);
}
}