我的环境:jdk 1.5.0+ tomcat 5.0.28+mysql -5.0.7-beta,安装配置正常,在mysql底下也创建了我得数据库fortest和表books,添加了记录,并且mysql-connector-java-3.1.10-bin已经放到了\tomcat\commonn\lib目录下面,为了测试我得数据库连接情况,写了一个测试的jsp文件,如下:
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>DbJsp.jsp</title>
</head>
<body>
<%
Connection con;
Statement stmt;
ResultSet rs;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e)
{
out.println("无法加载驱动程序!");
}
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String dbUrl="jdbc:mysql://localhost:3306/fortest?useUnicode=true&characterEncoding=GB2312";
String dbUser="root";
String dbPwd="123";
try
{
System.out.println("check point 1");
con=java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
stmt=con.createStatement();
stmt.executeUpdate("insert into books(id,name,title,price) values ('999','Tom','Tomcat Bible','44.5')");
rs=stmt.executeQuery("select id,name,title,price from books");
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);
float col4=rs.getFloat(4);
out.println("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td></tr>");
}
System.out.println("check point 2");
out.println("</table>");
stmt.executeUpdate("delete from books where id='999'");
rs.close();
stmt.close();
con.close();
System.out.println("check point 3");
}
catch (SQLException ee)
{
out.println("数据库连接失败!");
ee.printStackTrace();
}
%>
</body>
</html>
--转自