JSP连接SQL 2000数据库的测试代码,运行了提示Statement stmt = con.createStatement(); 有错,可是
后来运行却不是那样提示错在哪一行了,错误如下:
HTTP status 500-
type Exception report
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.jsp.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:106)
root cause
java.lang.NullPointerException
org.apache.jsp.sql_005ftest_jsp._jspService(sql_005ftest_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.jsp.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:106)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.30 logs.
我的tomcat jdk什么的都可以配置好了的,就是数据库这一步连接不上了,请问到底怎么解决呢?
这是代码:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String userid="sa";
String passwd="111111";
Connection con = null;
try {
Class.forName(driver);
} catch(Exception e) {
out.println("无法载入"+driver+"驱动程序!");
e.printStackTrace();
}
try {
con=java.sql.DriverManager.getConnection(url,userid,passwd);
if(!con.isClosed())
out.println("成功连接数据库!");
} catch(SQLException SQLe) {
out.println("无法连接数据库!");
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from person");
while(rs.next()) {
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print("<br>");}
con.close();
%>
</BODY>
</HTML>
答案:<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<%
try {
// 1.注册驱动 构造驱动实例
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// new oracle.jdbc.driver.OracleDriver();
// 2.指定连接字符串 @机器IP 端口号 数据库名称
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
// 3.打开数据库联接
Connection conn = DriverManager
.getConnection(url, "sa", "111111");
// 4.构造查询语句对象
Statement stme = conn.createStatement();
// 5.提交查询语句并获得结果集
ResultSet rs = stme.executeQuery("select * from person");
// 6.遍历结果集
while (rs.next()) {
out.print(rs.getString(1));
out.print(rs.getString(2));
}
rs.close();
stme.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
</BODY>
</HTML>
上一个:请JSP高手帮我看看下面的代码,有问题
下一个:jsp背景图片如何设置,求代码