日期:2014-05-20  浏览次数:20615 次

jsp向数据库添加行
各位大侠帮帮忙
我用jsp向数据库中添加行时老出现错误
部分代码:
<%!
    String   msgTitle;
    String   msgContent;
    String   msgAuthor;
%>
<%
    String   uno=session.getAttribute( "text_Uno ").toString();
    String   str_Identity=session.getAttribute( "text_Identity ").toString();
    //int   identity=new   Integer(str_Identity);
    int   identity=Integer.valueOf(str_Identity).intValue();
    out.println( "uon= "+uno+ ",identity= "+identity);
 //这里测试的整形是对的,但是在后面的添加中,添加不进去,经过我一次次的测试证明是这里整形添加的问题,删除这项就可以运行

    msgTitle=new   String(request.getParameter( "text_MsgTitle ").getBytes( "ISO8859_1 "), "GBK ");
    msgContent=new   String(request.getParameter( "text_MsgContent ").getBytes( "ISO8859_1 "), "GBK ");
    msgAuthor=new   String(request.getParameter( "text_MsgAuthor ").getBytes( "ISO8859_1 "), "GBK ");
    //out.print(msgTitle+ ": "+msgContent+ ": "+msgAuthor);
    Statement   s;
    String   url= "insert   into   MsgBoard(Uno,Identity,MsgTitle,MsgContent,MsgAuthor) "     + "values( ' "+uno+ " ', ' "+identity+ " ', ' "+msgTitle+ " ', ' "+msgContent+ " ', ' "+msgAuthor+ " ') ";  
    try{
        s=conn.getStatement();
s.execute(url);
conn.close();
%>
<Script>
if   (confirm( "留言已上传! "))
      location   =   "gstBoard.jsp ";
else
      location   =   "gstBoard.jsp ";
</Script>
    <%}
    catch(Exception   e){
out.println( "wrong ");
    //out.println(e.getMessage());
e.printStackTrace();    
    }    

    //out.print(dno);
%>
数据库表MsgBoard设计为
Uno   char(10)
Identity   int(4)
```````(其他的省略)

------解决方案--------------------
报什么错也不帖出来,会不会提问啊??
+ "values( ' "+uno+ " ', ' "+identity+ " ', ' "+msgTitle+ " ', ' "+msgContent+ " ', ' "+msgAuthor+ " ') ";
既然是整形,为什么还要用单引号??
------解决方案--------------------
直接使用java bean 不行吗?
------解决方案--------------------
把这些要插入的字段的值都打印出来,然后在数据库执行一下这个sql,insert into MsgBoard(Uno,Identity,MsgTitle,MsgContent,MsgAuthor) " + "values( ' "+uno+ " ', ' "+identity+ " ', ' "+msgTitle+ " ', ' "+msgContent+ " ', ' "+msgAuthor+ " ') "; 看能执行成功不