我用如下jsp代码
<%@ page contentType="text/html;charset=gb2312" %>
<%@page language="java" import="java.sql.*"%>
<html>
<head>
<title>上传论文</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<form name="form1" method="post" action="send.jsp" enctype="multipart/form-data">
<table width="100%" border="1">
<tr>
<td width="30%">
<div align="right">题目:</div>
</td>
<td width="70%">
<input type="text" name="Theme" size="40">
</td>
</tr>
<tr>
<td width="30%">
<div align="right">作者:</div>
</td>
<td width="70%">
<input type="text" name="Author" size="40">
</td>
</tr>
<tr>
<td width="30%">
<div align="right">内容:</div>
</td>
<td width="70%">
<textarea name="Content" cols="80" rows="20"></textarea>
</td>
</tr>
<tr>
<td width="30%">
<div align="right">论文体:</div>
</td>
<td width="70%">
<input type="file" name="Fujian" size="40" >
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="button" name="Submit" value="发送" onclick="chk()">
<input type="reset" name="Submit2" value="重写">
<script language="javascript">
function chk()
{
if(document.form1.Theme.value=="")
{
window.alert("请填写题目!");
window.form1.Theme.focus();
return false;
}
if(document.form1.Author.value=="")
{
window.alert("请填写作者!");
window.form1.Author.focus();
return false;
}
document.form1.submit();
}
</script>
</div>
</td>
</tr>
</table>
</head>
</html>
当点击“发送”时来调用以下代码:
<%@ page contentType="text/html;charset=gb2312" %>
<%@page language="java" import="java.sql.*"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("GBK");
String temp=new String(temp_t ,"ISO8859_1");
return temp;
}
catch (Exception e)
{
e.printStackTrace();
}
return "love";
}
%>
<%
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:NPM";
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
try{
Class.forName(sDBDriver);
}
catch(Exception e)
{
out.println(e.getMessage());
}
try
{
conn=DriverManager.getConnection(sConnStr);
stmt=conn.createStatement();
}
catch(Exception ex)
{
out.println(ex.getMessage());
}
%>
<%
String Theme,Author,Content,Fujian;
Theme=request.getParameter("Theme");
Author=request.getParameter("Author");
Content=request.getParameter("Content");
Fujian=request.getParameter("Fujian");
Theme=getStr(Theme);/////转换成中文
Author=getStr(Author);
Content=getStr(Content);
Fujian=getStr(Fujian);
String sql;
sql="insert into paper(theme,author,content,fujian)" ;
sql=sql+"Values("+Theme+","+Author+","+Content+","+Fujian+")";
try
{
stmt.executeQuery(sql);
}
catch(Exception e)
{
out.println(e);
}
%>
成功发送
</body>
</html>
目的是向数据库插入数据,但是数据库中表中每一项都显示love,(这是我在定义中文转换函数getStr()时定义的
异常返回return "love";请问这是怎么回事???
把你的方法改成,试试
public String getStr(String str)
{
try
{
return new String(str.getBytes("gb2312"),"8859_1");
}
catch (Exception e)
{
e.printStackTrace();
return "love";
}
}