数据库小问题
代码:
<%@ page contentType= "text/html;charset=GB2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%
Connection con;
Statement sql;
ResultSet rs;
try{Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch(ClassNotFoundException e){}
con=DriverManager.getConnection( "jdbc:odbc:sun ", "sa ", " ");
sql=con.createStatement();
rs=sql.executeQuery( "select * from user ");
while(rs.next())
{
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
}
con.close();
}
catch(SQLException e1){}
%>
</body>
</html>
报错:
org.apache.jasper.JasperException: Unable to compile class for JSP:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
[解决办法]
try{Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch(ClassNotFoundException e){}
//这里缺了try了...或者整合到前面那个try catch块中去
try {
con=DriverManager.getConnection( "jdbc:odbc:sun ", "sa ", " ");
sql=con.createStatement();
rs=sql.executeQuery( "select * from user ");
while(rs.next())
{
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
}
con.close();
}
catch(SQLException e1){}
[解决办法]
try{//try上也不行吗?
con=DriverManager.getConnection( "jdbc:odbc:sun ", "sa ", " ");
sql=con.createStatement();
rs=sql.executeQuery( "select * from user ");
while(rs.next())
{
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
}
con.close();
}//我也觉得是这里的问题
catch(SQLException e1){}
[解决办法]
<%@ page contentType= "text/html;charset=GB2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
con=DriverManager.getConnection( "jdbc:odbc:sun ", "sa ", " ");
sql=con.createStatement();
rs=sql.executeQuery( "select * from user ");
while(rs.next())
{
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
}
con.close();
}
catch(Exception e){}
%>
</body>
</html>
这样吧,我编译通过了,应该是try,catch的问题但是这里可能会有多个异常,ClassNotFoundException和SQLException,所以楼上的只捕捉SQLException是不够的,可以直接写上Exception,就都捕捉了,如果知道是几个异常就可以分别catch
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html