js|mysql中文问题:
jsp和mysql连接的时候,要在每一个jsp文件中加:
<% page contentType="text/html;charset=gb2312" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
如果是表单的提交要加上:
jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&characterEncoding=GBK" 和
<% request.setCharacterEncoding("GBK"); %>
############################################################################
1.
<% page contentType="text/html;charset=gb2312" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
java.sql.Connection sqlConn;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;
//regiester jdbc driver
String DBUser="root";
String DBPassword="root";
String DBServer="127.0.0.1"; // Can't use localhost , you must use IP or CNAME
String DBNAME="publish"; // change to your db name
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//connect to the database
sqlConn=java.sql.DriverManager.getConnection
("jdbc:mysql://"+DBServer+":3306/"+DBNAME+"?useUnicode=true&characterEncoding=gb2312",
DBUser,DBPassword);
//create the driver handle
sqlStmt=sqlConn.createStatement
(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//excute sql
sqlRst=sqlStmt.executeQuery("select * from book");
%>
<html>
<head>
<title>JSP-JDBC-MYSQL TESTING</title>
</head>
<body>
<table border="1" cellspace ="0" align="center">
<tr><th>ID </th>
<th>Book name </th></tr>
<% while(sqlRst.next()) {%>
<tr>
<td> <%=sqlRst.getString("id")%></td>
<td><%=sqlRst.getString("title")%></td>
</tr>
<%}%>
</table>
</body>
</html>
<%
//sqlRst.close();
sqlStmt.close();
sqlConn.close();
%>
………………………………………………………………………………
2.
<% page contentType="text/html;charset=gb2312" %>
<%
java.sql.Connection conn;
java.lang.String strConn;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn= java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","root","");
%>
………………………………………………………………………………
3.
<% page import="java.sql.*" %>
<%
out.println(" Border is me !");
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E) {
out.println("Unable to load driver.");
}
%>
………………………………………………………………………………
jsp和mysql连接
80酷酷网 80kuku.com