<%@ page contentType="text/html; charset=GB2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%!
public class sql_data{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sUrl="jdbc:microsoft:sqlserver://192.168.1.107:1433;DatabaseName=pubs;user=sa;password=119";
Connection conn=null;
ResultSet rs=null;
public sql_data(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("sql_data():"+e.getMessage());
}
}
public void eInsert(String sql){
try{
conn=DriverManager.getConnection(sUrl);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("sql_data.executeUpdate:"+ex.getMessage());
}
}
public ResultSet eQuery(String sql){
rs=null;
try{
conn=java.sql.DriverManager.getConnection(sUrl);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("sql_data.executeQuery:"+ex.getMessage());
}
return rs;
}
public void eDelete(String sql){
try{
conn=DriverManager.getConnection(sUrl);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("sql_data.executeDelete:"+ex.getMessage());
}
}
}
%>
<%
String sql="select * from student";
sql_data dbc=new sql_data();
ResultSet rs = dbc.eQuery(sql);
%>
<%
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"学号");
out.print("<TH width=100>"+"姓名");
out.print("<TH width=50>"+"数学成绩");
out.print("<TH width=50>"+"英语成绩");
out.print("<TH width=50>"+"物理成绩");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
out.print("<TD>"+rs.getString(1)+"</TD>");
out.print("<TD>"+rs.getString(2)+"</TD>");
out.print("<TD>"+rs.getInt("数学成绩")+"</TD>");
out.print("<TD>"+rs.getInt("英语成绩")+"</TD>");
out.print("<TD>"+rs.getInt("物理成绩")+"</TD>");
out.print("</TR>");
}
out.print("</Table>");
%>
</body>
</html>
浏览报的错是:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
org.apache.jsp.Example5_005f1_jsp._jspService(Example5_005f1_jsp.java:114)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.