我在一个jsp代码中需要向数据库插入数据,数据插入通过编写好的javabean中的方法:add()实现,插入数据通过add函数的参数传递,而数据的获得通过session对象的属性值,但是向数据库中插入数据没有提示错误,但是数据没有插入到数据库中,为什么?以下是我的代码,代码中的变量user、content、Lang等都能在jsp页面中正常显示,但是向数据库中插入数据时却不成功。
然而,如果我对user、content、Lang等的赋值通过new String()函数实现(就是用//注释掉的代码),则能顺利地把数据插入到数据库中,不知道为什么?
我个人认为原因可能出在用session对象的属性对变量赋值上,请大家指教,怎样能把从session对象获得属性值顺利插入数据库?
<%@ page import="Term.Corpus.AddTerm" %>
<%@ page contentType="text/html; charset=GBK" %>
<html >
<head >
</head>
<%
String termlist=new String(request.getParameter("termarea").getBytes("ISO_8859_1"), "GBK");
String user=session.getAttribute("username").toString() ;
String content=session.getAttribute("content").toString() ;
String Lang=session.getAttribute("Lang").toString() ;
String SourceCorpus=session.getAttribute("sourcecorpus").toString() ;
String FileName=session.getAttribute("FileName").toString() ;
//String termlist=new String("hihi,");
//String user=new String("zhangxia");
//String content=new String("<s id=\"chapter 1 .part I .Null0.Section1.p1.s1\">hihi:1</s>");
//String Lang=new String("EN");
//String SourceCorpus=new String("");
//String FileName=new String("");%>
<jsp:useBean id="addterm" scope="page" class="Term.Corpus.AddTerm">
</jsp:useBean>
<%
addterm.add(user,termlist,SourceCorpus,FileName,content,Lang) ;
//addterm.add("zhangxia","title,","","","<s id=\"chapter 1 .part I .Null0.Section1.p1.s1\">title:1</s>","EN") ;
%>
<body >
<%
out.println(termlist+"<br>") ;
out.println(user+"<br>") ;
out.println(content+"<br>") ;
out.println(Lang+"<br>") ;
out.println(SourceCorpus+"<br>") ;
out.println(FileName+"<br>") ;
%>
<h2>
术语已成功加载到术语库,您可以进行下一步的工作!
</h2>
</body>
</html>
然而,如果我对user、content、Lang等的赋值通过new String()函数实现(就是用//注释掉的代码),则能顺利地把数据插入到数据库中,不知道为什么?
我个人认为原因可能出在用session对象的属性对变量赋值上,请大家指教,怎样能把从session对象获得属性值顺利插入数据库?
<%@ page import="Term.Corpus.AddTerm" %>
<%@ page contentType="text/html; charset=GBK" %>
<html >
<head >
</head>
<%
String termlist=new String(request.getParameter("termarea").getBytes("ISO_8859_1"), "GBK");
String user=session.getAttribute("username").toString() ;
String content=session.getAttribute("content").toString() ;
String Lang=session.getAttribute("Lang").toString() ;
String SourceCorpus=session.getAttribute("sourcecorpus").toString() ;
String FileName=session.getAttribute("FileName").toString() ;
//String termlist=new String("hihi,");
//String user=new String("zhangxia");
//String content=new String("<s id=\"chapter 1 .part I .Null0.Section1.p1.s1\">hihi:1</s>");
//String Lang=new String("EN");
//String SourceCorpus=new String("");
//String FileName=new String("");%>
<jsp:useBean id="addterm" scope="page" class="Term.Corpus.AddTerm">
</jsp:useBean>
<%
addterm.add(user,termlist,SourceCorpus,FileName,content,Lang) ;
//addterm.add("zhangxia","title,","","","<s id=\"chapter 1 .part I .Null0.Section1.p1.s1\">title:1</s>","EN") ;
%>
<body >
<%
out.println(termlist+"<br>") ;
out.println(user+"<br>") ;
out.println(content+"<br>") ;
out.println(Lang+"<br>") ;
out.println(SourceCorpus+"<br>") ;
out.println(FileName+"<br>") ;
%>
<h2>
术语已成功加载到术语库,您可以进行下一步的工作!
</h2>
</body>
</html>
我估计是某些值不符合数据库插入的要求。比如有些是null的话,要特别留意。
和
String content=new String("<s id=\"chapter 1 .part I .Null0.Section1.p1.s1\">hihi:1</s>");
值一样吗?如果
String termlist=new String("hihi,");
String user=new String("zhangxia");
String content=new String("<s id=\"chapter 1 .part I .Null0.Section1.p1.s1\">hihi:1</s>");
String Lang=new String("EN");
String SourceCorpus=new String("");
String FileName=new String("");
......
addterm.add(user,termlist,SourceCorpus,FileName,content,Lang) ;
这样能成功吗?
(怀疑是content的值有问题)
String termlist=new String(request.getParameter("termarea").getBytes("ISO_8859_1"), "GBK");
String content=session.getAttribute("content").toString() ;
加入代码进行测试:
out.print(content.indexOf(termlist)+"<br>") ;
结果为-1,但是termlist变量值是content里边的一个字符串,不应为-1,
比如说我有termlist获得的字符串为“chapter”,也能够通过下面的代码out.println(termlist+"<br/>") ;输出到页面,但是在indexof函数值却显示-1,但是呢,如果这样写
out.print(content.indexOf("chapter")+"<br>") ;则能得到我想要的结果,能正确定位,不知道是为什么,我想是不是字符串编码的问题,但是又不知道怎么做,大家指点!