<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%
String action = request.getParameter("action");
if (action != null&& action.equals("post")) {
request.setCharacterEncoding("gbk");
String title = request.getParameter("title");
System.out.println(title);
String cont = request.getParameter("cont");
cont = cont.replaceAll("\n","<br>");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false);
String sql = "insert into article values(null,0,?,?,?,now(),0)";
PreparedStatement psta = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
psta.setInt(1,-1);
psta.setString(2,title);
psta.setString(3,cont);
psta.executeUpdate();
ResultSet rsKey = psta.getGeneratedKeys();
rsKey.next();
int key = rsKey.getInt(1);
rsKey.close();
String sql1 = "update article set rootid =? where id =?";
PreparedStatement psta1 = conn.prepareStatement(sql1);
psta1.setInt(1,key);
psta1.setInt(2,key);
psta1.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
psta.close();
psta1.close();
conn.close();
response.sendRedirect("ShowArticleTree.jsp");
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body> <form action="Post.jsp" method ="post">
<input type="hidden" name="action" value="post">
<table>
<tr>
<td><input type="text" size=80 name="title"/></td>
</tr>
<tr>
<td><textarea cols="80" rows="20" name="cont"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
发现插入数据库的中文是乱码,怎么回事?
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%
String action = request.getParameter("action");
if (action != null&& action.equals("post")) {
request.setCharacterEncoding("gbk");
String title = request.getParameter("title");
System.out.println(title);
String cont = request.getParameter("cont");
cont = cont.replaceAll("\n","<br>");
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false);
String sql = "insert into article values(null,0,?,?,?,now(),0)";
PreparedStatement psta = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
psta.setInt(1,-1);
psta.setString(2,title);
psta.setString(3,cont);
psta.executeUpdate();
ResultSet rsKey = psta.getGeneratedKeys();
rsKey.next();
int key = rsKey.getInt(1);
rsKey.close();
String sql1 = "update article set rootid =? where id =?";
PreparedStatement psta1 = conn.prepareStatement(sql1);
psta1.setInt(1,key);
psta1.setInt(2,key);
psta1.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
psta.close();
psta1.close();
conn.close();
response.sendRedirect("ShowArticleTree.jsp");
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body> <form action="Post.jsp" method ="post">
<input type="hidden" name="action" value="post">
<table>
<tr>
<td><input type="text" size=80 name="title"/></td>
</tr>
<tr>
<td><textarea cols="80" rows="20" name="cont"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
发现插入数据库的中文是乱码,怎么回事?
楼主的数据库是用的 mysql 的。
mysql数据库插入中文是要设置字符集的。
connectionTimeout="20000"
redirectPort="8443" URIEncoding="GBK"/>加上 这个 因为我一直是UTF-8编码 所以改的是UTF-8 改过之后就基本没有出现过中文乱码了。。GBK的效果不太知道 你可以试试
mysql的数据库需要设置字符集的 UTF-8
pageEncoding="gbk"%>pageEncoding改为UTF-8