中文问题:
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.”);
}
%>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101214.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...