JSP表单提交数据到MySQL
我这个代码的功能是进入add_text.jsp里面输入内容提交,然后讲提交的内容写入MySQL,请问各位大大为什么这个实现操作[color=#FF0000]只有提交数字才能写入数据库,提交英文和中文都不能写入到MySQL里面。SQL里面定义的是两个vachar类型的属性,编码格式都是GBK。[/color]
add_text.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
-->
<script type="text/javascript"">
function validate()
{
var Tname = document.forms[0].Tname.value;
var Ttext = document.forms[0].Ttext.value;
//document.getElementById("form").submit();
}
</script>
</head>
<body>
<br>
<center>
<h2>添加留言文本</h2><hr>
<form action="insert.jsp" method="post" id="form" onSubmit="return validate()" >
<h4> 昵称:<input type="text" name="Tname" class="{required:true}"></input><br></h4>
<h4> 留言内容:<input type="text" name="Ttext"></input><br></h4>
<input type="submit" value="提交"/>
</form>
<a href="">查询所有留言</a>
</center>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
-->
</head>
<body>
<%
request.setCharacterEncoding("gbk");
String Tname = request.getParameter("Tname");
String Ttext = request.getParameter("Ttext");
System.out.println(Ttext);
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mldn";
String user = "root";
String password = "root";
try{
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String sql = "insert into TextInfo(Tname,Ttext) values(" + Tname + ",'" + Ttext + "')";
stat.executeUpdate(sql);
rs = stat.executeQuery("select * from student"); }
catch(Exception e){}
%>
<center>
<%
try{
if(rs.next())
{
out.print("<br><h3>成功输入!</h3>");
}
else{
out.print("<br><h3>输入失败!</h3>");
}
}
catch(Exception e){}
%>
<br>
<a href=add_text.jsp>返回添加信息页面</a> <a href=showInfo.jsp>进入信息查询页面</a>
</center>
<%
if(rs != null)
{
rs.close();
rs = null;
}
if(stat != null)
{
stat.close();
stat = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
%>
</body>
</html>