环境: Resin+JDK1.4 + mysql-connector-java-3.1.10-bin.jar
下载 mysql-connector-java
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.1.10.zip/
from/http://gd.tuwien.ac.at/db/mysql/
先安装JDK, 再安装Resin服务器
把mysql-connector-java-3.1.10-bin.jar直接放在jdk目录下。
路径看起来是这样的:
D:\j2sdk1.4.2\jre\lib\ext\mysql-connector-java-3.1.10-bin.jar
然后直接连。
测试代码:
<%@ page contentType="text/html;CHARSET=utf8mb4" %>
<%@ page import="java.sql.*" %>
<html>
<body>
<div align="center" >
<%
String dbname="bbs"; //你自己的mysql数据库名字
String dbuser="admin"; //mysql用户名
String dbpass="admin"; //mysql密码
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/"+dbname+"?user="+dbuser+
"&password="+dbpass+"&useUnicode=true&characterEncoding=gb2312";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql="select * from cdb_members";
//本例子的数据库中的表至少是6个字段。(其实就是 discuz! 论坛的用户表)
ResultSet rs=stmt.executeQuery(sql);
%>
<div align="center" >
<table cellpadding="0" cellspacing="5" border="1" >
<tr background="#336699">
<td>第1个字段内容</td>
<td>第2个字段内容</td>
<td>第3个字段内容</td>
<td>第4个字段内容</td>
<td>第5个字段内容</td>
<td>第6个字段内容</td>
</tr>
<% while(rs.next()) { %>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
</tr>
<% } %>
</table>
</div>
<%
out.print("\n<br>\n<b>数据库操作成功,恭喜你</b>");
rs.close();
stmt.close();
conn.close();
}catch(java.sql.SQLException e){
out.println("<b> 数据库操作失败:</b>");
//e.printStackTrace();
int errno;
errno=e.getErrorCode();
out.println("\n<br>ErrorCode:"+errno);
if(errno==1045)
out.println("Access Denied for user : '" + dbuser + "@localhost' ");
out.println("\n<br>SQLState:"+e.getSQLState());
//out.println(e.getNextException());
}
%>
</div>
</body>
</html>