不想麻烦大家..只是真的搞到头大了..查了帖子也解决不了,为什么代码1(某书光盘中的)往SQL插入中文数据就完全正常,而代码2(我改了数据库名,数据类型全是CHAR,还改了些变量)...插入中文时就有些会乱码了,比如产品的"产"变成?号..
还有就是代码1中的String SQL = "insert Single_tab (SName,SSex,SIDCred,SAge,Snationality,SUnit,SPhone,SMemo) "+
"values('"+name+"',"+sex+","+idcred+","+age+",'"+nationality+"','"+unit+"','"
+phone+"','"+memo+"')";
为什么sex,idcred,age不用单引号呢?代码2要是不用的话就插入不成功了..代码1:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%request.setCharacterEncoding("GB2312");%>
<%!
public Connection Con() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con = DriverManager.getConnection("jdbc:odbc:db_database05","sa","sa");
return Con;
}
catch(Exception e)
{
return null;
}
}
public int InsertSQL(String name,String sex,String idcred,String age,
String nationality,String unit,String phone,String memo){ try
{
String SQL = "insert Single_tab (SName,SSex,SIDCred,SAge,Snationality,SUnit,SPhone,SMemo) "+
"values('"+name+"',"+sex+","+idcred+","+age+",'"+nationality+"','"+unit+"','"
+phone+"','"+memo+"')";
//VALUES('A',B,C,D,'E','F','G','H');
Connection Con = Con();
Statement Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int UpdateCount = Smt.executeUpdate(SQL);
return UpdateCount;
}
catch(SQLException e)
{
System.out.println("记录插入失败!");
return 0;
}
}
%>
...
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<base href="<%=basePath%>">
<title>单条数据录入</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <style type="text/css">
<!--
.style1 {
font-size: 14px;
font-weight: bold;
color: #33FF00;
}
.style2 {
font-size: 12px;
color: #663399;
}
.style4 {font-size: 12}
.style5 {color: #FF0000}
-->
</style>
</head>
<body>
<form name="form" method="post" action="/myjsp/WebRoot/index.jsp" onsubmit="if (checkEmpty(form)) { <%
InsertSQL(request.getParameter("name"),request.getParameter("sex"),request.getParameter("idcred"),
request.getParameter("age"),request.getParameter("nationality"),
request.getParameter("unit"),request.getParameter("phone"),
request.getParameter("memo"));
%>}">
......
</form>
<br>
</body>
</html>
代码2:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%!
public Connection Con() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con = DriverManager.getConnection("jdbc:odbc:ylxlam","sa","sa");
return Con;
}
catch(Exception e)
{
return null;
}
}
public int InsertSQL(String id,String name,String abc,String spec,
String sort,String unit,String price,String stock){ try
{
String SQL = "insert product (pID,pName,pShort,pSpec,pSort,pUnit,pPrice,pStock)"+
"values('"+id+"','"+name+"','"+abc+"','"+spec+"','"+sort+"','"+unit+"','"+price+"','"+stock+"')";
Connection Con = Con();
Statement Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int UpdateCount = Smt.executeUpdate(SQL);
return UpdateCount;
}
catch(SQLException e)
{
System.out.println("记录插入失败!");
return 0;
}
}
%>
...
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<base href="<%=basePath%>">
<title>单条数据录入</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <style type="text/css">
<!--
.style1 {
font-size: 14px;
font-weight: bold;
color: #33FF00;
}
.style2 {
font-size: 12px;
color: #663399;
}
.style4 {font-size: 12}
.style5 {color: #FF0000}
-->
</style>
</head>
<body>
<form name="form" method="post" action="/myjsp/WebRoot/insert1.jsp" onsubmit="<%
InsertSQL(request.getParameter("id"),request.getParameter("name"),request.getParameter("abc"),
request.getParameter("spec"),request.getParameter("sort"),
request.getParameter("unit"),request.getParameter("price"),
request.getParameter("stock"));
%>">
...
...
</form>
<br>
</body>
</html>
还有就是代码1中的String SQL = "insert Single_tab (SName,SSex,SIDCred,SAge,Snationality,SUnit,SPhone,SMemo) "+
"values('"+name+"',"+sex+","+idcred+","+age+",'"+nationality+"','"+unit+"','"
+phone+"','"+memo+"')";
为什么sex,idcred,age不用单引号呢?代码2要是不用的话就插入不成功了..代码1:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%request.setCharacterEncoding("GB2312");%>
<%!
public Connection Con() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con = DriverManager.getConnection("jdbc:odbc:db_database05","sa","sa");
return Con;
}
catch(Exception e)
{
return null;
}
}
public int InsertSQL(String name,String sex,String idcred,String age,
String nationality,String unit,String phone,String memo){ try
{
String SQL = "insert Single_tab (SName,SSex,SIDCred,SAge,Snationality,SUnit,SPhone,SMemo) "+
"values('"+name+"',"+sex+","+idcred+","+age+",'"+nationality+"','"+unit+"','"
+phone+"','"+memo+"')";
//VALUES('A',B,C,D,'E','F','G','H');
Connection Con = Con();
Statement Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int UpdateCount = Smt.executeUpdate(SQL);
return UpdateCount;
}
catch(SQLException e)
{
System.out.println("记录插入失败!");
return 0;
}
}
%>
...
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<base href="<%=basePath%>">
<title>单条数据录入</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <style type="text/css">
<!--
.style1 {
font-size: 14px;
font-weight: bold;
color: #33FF00;
}
.style2 {
font-size: 12px;
color: #663399;
}
.style4 {font-size: 12}
.style5 {color: #FF0000}
-->
</style>
</head>
<body>
<form name="form" method="post" action="/myjsp/WebRoot/index.jsp" onsubmit="if (checkEmpty(form)) { <%
InsertSQL(request.getParameter("name"),request.getParameter("sex"),request.getParameter("idcred"),
request.getParameter("age"),request.getParameter("nationality"),
request.getParameter("unit"),request.getParameter("phone"),
request.getParameter("memo"));
%>}">
......
</form>
<br>
</body>
</html>
代码2:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%!
public Connection Con() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con = DriverManager.getConnection("jdbc:odbc:ylxlam","sa","sa");
return Con;
}
catch(Exception e)
{
return null;
}
}
public int InsertSQL(String id,String name,String abc,String spec,
String sort,String unit,String price,String stock){ try
{
String SQL = "insert product (pID,pName,pShort,pSpec,pSort,pUnit,pPrice,pStock)"+
"values('"+id+"','"+name+"','"+abc+"','"+spec+"','"+sort+"','"+unit+"','"+price+"','"+stock+"')";
Connection Con = Con();
Statement Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int UpdateCount = Smt.executeUpdate(SQL);
return UpdateCount;
}
catch(SQLException e)
{
System.out.println("记录插入失败!");
return 0;
}
}
%>
...
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<base href="<%=basePath%>">
<title>单条数据录入</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <style type="text/css">
<!--
.style1 {
font-size: 14px;
font-weight: bold;
color: #33FF00;
}
.style2 {
font-size: 12px;
color: #663399;
}
.style4 {font-size: 12}
.style5 {color: #FF0000}
-->
</style>
</head>
<body>
<form name="form" method="post" action="/myjsp/WebRoot/insert1.jsp" onsubmit="<%
InsertSQL(request.getParameter("id"),request.getParameter("name"),request.getParameter("abc"),
request.getParameter("spec"),request.getParameter("sort"),
request.getParameter("unit"),request.getParameter("price"),
request.getParameter("stock"));
%>">
...
...
</form>
<br>
</body>
</html>
好奇怪的书?
是什么写法
落下了`?
<%request.setCharacterEncoding("GBK");%>
我同时加了这两个也还是不行的