JPS连接数据库并做查询输出语句,表和数据都已经输入进去了。但是在我加载驱动的时候报错,我在用JDBC的时候是可以正常使用的。
我的包放在TOMCAT的lib文件夹下面。
<%@page import="java.util.Properties"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn=null;
PreparedStatement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("GBK");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException ce)
{
out.println(ce.getMessage());//只输出这个地方的com.mysql.jdbc.Driver,显示加载驱动错误。但是我的包有导进去呀,难道还要设置环境变量???
}
try{
String url="jdbc:mysql://localhost:3306/xianxian";
conn = DriverManager.getConnection(url,"root","12345");
stmt = conn.prepareStatement("select * from websites");
rs = stmt.executeQuery();
out.print("<TR><TD>" + "<b>id</b>" + "</TD>");
out.print("<TD>" + "<b>name</b>" + "</TD>");
out.print("<TD>" + "<b>url</b>" + "</TD></TR>");
while(rs.next())
{
out.print("<TR><TD>" + rs.getString(1) + "</TD>");
out.print("<TD>" + rs.getString(2) + "</TD>");
out.print("<TD>" + rs.getString(3) + "</TD></TR>");
}
out.print("</TABLE>");
rs.close();
stmt.close();
conn.close();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
%>
</body>
</html>