在网上搜出一大把,各有各的说法,试了几个没成功的,我用的是XP+apache-tomcat-5.5.20.exe+jdk-1_5_0_10-nb-5_5-win.exe+mysql-5.0.26-win32.exe+mysql-connector-java-5.0.3-bin.jar
测试用的这个:
<!--首先导入一些必要的packages-->
<%@ page import= "java.io.* "%>
<%@ page import= "java.util.* "%>
<!--告诉编译器使用SQL包-->
<%@ page import= "java.sql.* "%>
<!--设置中文输出-->
<%@ page contentType= "text/html; charset=GB2312 " %>
<html>
<head>
<title> mySQL test </title>
</head>
<body>
<%
Connection con;
Statement stmt;
ResultSet rs;
//加载驱动程序,下面的代码为加载MySQL驱动程序
Class.forName( "com.mysql.jdbc.Driver ");
//注册MySQL驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//用适当的驱动程序连接到数据库
String dbUrl = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GB2312 ";
String dbUser = "root "; //用户名
String dbPwd = " "; //密码
//建立数据库连接
//con = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPwd);
String dbUrl = "jdbc:mysql://localhost:3306/mysql?
user=root&password=abcd1001&useUnicode=true&characterEncoding=gb2312 ";
con = DriverManager.getConnection(dbUrl);
//创建一个JDBC声明
stmt = con.createStatement();
//增加新记录
//stmt.executeUpdate( "INSERT INTO books (id,name,title,price) VALUES ( '999 ', 'Tom ', 'Tomcat Bible ',44.5) ");
//查询记录
rs = stmt.executeQuery( "select * from user ");
//输出查询结果
out.println( " <table border=1 width=400> ");
while (rs.next())
{
String col1 = rs.getString(1);
String col2 = rs.getString(2);
String col3 = rs.getString(3);
String col4 = rs.getString(4);
//打印所显示的数据
out.println( " <tr> <td> "+col1+ " </td> <td> "+col2+ " </td> <td> "+col3+ " </td> <td> "+col4+ " </td> </tr> ");
}
out.println( " </table> ");
//关闭数据库连结
rs.close();
stmt.close();
con.close();
%>
</body>
</html>
报错:root cause
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 14 in the jsp file: /testmysql.jsp
Generated servlet error:
com.mysql.jdbc.Driver cannot be resolved to a type