你可以试着修改一下你的数据库连接字符串,在后面加上一些参数,如下:
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銆戝嵆鍙銆