你可以試著修改一下你的資料庫連接字元串,在後面加上一些參數,如下:
jdbc:mysql://localhost:3306/數據內庫名?useUnicode=true&characterEncoding=gb2312
然後把容JSP編碼設置為gb2312,再試試看
② 如何在JSP頁面顯示mysql資料庫內容
顯示資料庫數據的jsp代碼如下:
解釋及說明在代碼的注釋中即可查看
java">
<spanstyle="font-size:12px;"><spanstyle="font-size:14px;"><%@pagelanguage="java"import="java.sql.*,java.io.*,java.util.*"%>
<%@pagecontentType="text/html;charset=utf-8"%>
<html>
<head>
<styletype="text/css">
table{
border:2px#CCCCCCsolid;
width:360px;
}
td,th{
height:30px;
border:#CCCCCC1pxsolid;
}
</style>
</head>
<body>
<%
//驅動程序名
StringdriverName="com.mysql.jdbc.Driver";
//資料庫用戶名
StringuserName="root";
//密碼
StringuserPasswd="szy";
//資料庫名
StringdbName="studentmanage";
//表名
StringtableName="student";
//連接字元串
Stringurl="jdbc:mysql://資料庫地址:埠號/"+dbName+"?user="
+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connectionconnection=DriverManager.getConnection(url);
Statementstatement=connection.createStatement();
Stringsql="SELECT*FROM"+tableName;
ResultSetrs=statement.executeQuery(sql);
%>
<br>
<br>
<tablealign="center">
<tr>
<th>
<%
out.print("學號");
%>
</th>
<th>
<%
out.print("姓名");
%>
</th>
<th>
<%
out.print("專業");
%>
</th>
<th>
<%
out.print("班級");
%>
</th>
</tr>
<%
while(rs.next()){
%>
<tr>
<td>
<%
out.print(rs.getString(1));
%>
</td>
<td>
<%
out.print(rs.getString(2));
%>
</td>
<td>
<%
out.print(rs.getString(3));
%>
</td>
<td>
<%
out.print(rs.getString(4));
%>
</td>
</tr>
<%
}
%>
</table>
<divalign="center">
<br><br><br>
<%
out.print("數據查詢成功,恭喜你");
%>
</div>
<%
rs.close();
statement.close();
connection.close();
%>
</body>
</html></span><spanstyle="font-size:24px;color:rgb(255,0,0);">
</span></span>
顯示結果如下所示:
③ 濡備綍鍦↗SP欏甸潰鏄劇ずmysql鏁版嵁搴撳唴瀹
鐢‥clipse tomcat鏂板緩涓涓狫SP欏甸潰錛堜竴錛変粙緇嶄簡濡備綍鍒涘緩涓涓獁eb紼嬪簭鍜岀涓涓猨sp欏甸潰錛屼互鍙奅clipse闇瑕佺殑涓浜涘繀瑕侀厤緗銆備粖澶╋紝鎴戜滑閲嶇偣璇翠竴涓嬪備綍浠庢暟鎹搴撲腑鏌ヨ㈡暟鎹錛屽苟涓斿湪JSP欏甸潰鏄劇ず銆
棣栧厛闇瑕佹敞鎰忚繖鏍蜂竴涓闂棰橈細
寤虹殑濡傛灉鏄疛ava欏圭洰錛屽彧闇瑕佸紩鍏MySQL-connector-java-5.1.10-bin.jar灝卞彲浠ヨ繍琛宩ava欏圭洰銆傚緩鐨勫傛灉鏄痺eb宸ョ▼錛屽綋Class.forName("com.mysql.jdbc.Driver");鏃訛紝Eclipse鏄涓嶄細鍘繪煡鎵懼瓧絎︿覆錛屼笉浼氬幓鏌ユ壘椹卞姩銆傛墍浠ラ渶瑕佹妸mysql-connector-java-5.1.10-bin.jar鎷瘋礉鍒皌omcat涓媗ib鐩褰曚笅錛岀劧鍚庯紝鍙抽敭銆愬伐紼嬨戱紝鐐瑰嚮銆恜roperties銆戱紝鐒跺悗鐐瑰嚮銆怞ava Build Path銆,鐐瑰嚮銆怉dd External Jars...銆戱紝浠巘omcat涓媗ib鐩褰曚腑閫夋嫨瀵瑰簲鐨刴ysql-connector-java-5.1.10-bin.jar錛屽備笅鍥炬墍紺猴紝鐒跺悗鐐瑰嚮銆怬K銆戝嵆鍙銆