请教一下:
前两天开始做jsp连接数据库的测试,总提示空指针的错误!!
我想问的是如果用JDBC-ODBC桥接方式连接SQL数据库用单独安装驱动程序吗?
书上说tomcat和JDK都带有jdbc.odbc.JdbcOdbcDriver这个驱动,可是我测试的时候总也不能连接成功!!
还有我在管理工具里配置好了数据源(配置的时候测试成功了的),连接的时候报错 :未发现数据源名称并且未指定默认驱动程序
代码:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
//加载jdbc-odbc驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//通过数据源连接:
String url ="jdbc:odbc:try";
Connection conn = DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
<%
ResultSet RS= Stmt.executeQuery("SELECT * FROM EMPLOYEE WHERE eage<25"); /*连接到数据库中的表EMPLOYEE上*/
String tt;
out.print("<table border=2 style=color:blue >");
while (RS.next()) {
out.print("<tr><td>");
tt = RS.getString("ename"); /*查询表中NUM字段的内容,字段内容可能使数组,默认列输出*/
out.print(tt);
out.print("</td><td>");
tt = RS.getString("esex");
out.print(tt);
out.print("</td><td>");
tt = RS.getString("eage");
out.print(tt);
out.print("</td><td>");
tt=RS.getString("egrade");
out.print(tt);
out.print("</td><td>");
}
out.print("</table>");
RS.close();
out.println("数据查询成功!");
%>
<%
Rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
错误信息:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /test1.jsp:5
2: <%@ page import="java.sql.*"%>
3: <html>
4: <body>
5: <%
6: //加载jdbc-odbc驱动程序
7: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
8:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.test1_jsp._jspService(test1_jsp.java:94)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.test1_jsp._jspService(test1_jsp.java:53)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.5.23