实现访问数据库并中读出数据并输出到页面:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title> database </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc
dbc:testDB";
Connection con=DriverManager.getConnection(url,"","");
Statement statement=con.createStatement();
String query="select * from testTable";
ResultSet result=statement.executeQuery(query);
while(result.next()){
out.println(result.getString(1));
}catch(Exception ex){
System.out.println(ex);
}
}
%>
</body>
</html>
为了实现这个例子正常运行,我做一下的配置:
1、建立一个符合ODBC标准的数据库文件,用的是Access,建立一个库,库名为testDB。然后,建立表,表名为testTable。随意建立多个字段,然后在字段中输入一些数据即可。
2、配置ODBC,以access文件为准。启动“控制面版”中的“数据源 (ODBC)”(Win2000中在“控制面版”的“管理工具”中)。然后,选择“系统DSN”,选择“添加”,在弹出的窗口中选择Driver do Microsoft Access (*.mdb)。新开窗口“ODBC Microsoft Access 安装”,填写“数据源名”为:testDB。然后通过“选择”按钮,浏览并找到指定的Access文件(后缀名为mdb),然后确定即可。
3、启动tomcat,然后在IE地址栏中输入
http://localhost:8080/database.jsp运行。
网面报错为:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 10 in the jsp file: /hxs.jsp
Generated servlet error:
[javac] Compiling 1 source file
D:\hxs\java\Tomcat 4.1\work\Standalone\localhost\test\hxs_jsp.java:62: 'catch' without 'try'
}catch(Exception ex){
^
An error occurred at line: 10 in the jsp file: /hxs.jsp
Generated servlet error:
D:\hxs\java\Tomcat 4.1\work\Standalone\localhost\test\hxs_jsp.java:53: 'try' without 'catch' or 'finally'
try {
^
2 errors
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:120)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:293)
at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:313)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:324)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:474)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:184)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2396)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.ja