小弟请高手帮忙看看
<%@ 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)
{
out.print( "类找不到! ");
}
try
{
con=DriverManager.getConnection( "jdbc:odbc:testDB ");
sql=con.createStatement();
rs=sql.executeQuery( "SELECT * FROM userTable ");
out.print( " <Table Border> ");
out.print( " <TR> <td colspan=8 align=center> 用户数据 </td> </tr> ");
out.print( " <TR> ");
out.print( " <Td width=100 > "+ "用户ID号 ");
out.print( " <Td width=50 > "+ "用户名 ");
out.print( " <Td width=100> "+ "用户真实姓名 ");
out.print( " <Td width=50> "+ "年龄 ");
out.print( " <Td width=50> "+ "性别 ");
out.print( " <Td width=100> "+ "联系地址 ");
out.print( " <Td width=100> "+ "联系电话 ");
out.print( " <Td width=100> "+ "添加时间 ");
out.print( " </TR> ");
while(rs.next())
{ out.print( " <TR> ");
out.print( " <TD > "+rs.getLong(1)+ " </TD> ");
out.print( " <TD > "+rs.getString(2)+ " </TD> ");
out.print( " <TD > "+rs.getString(4)+ " </TD> ");
out.print( " <TD > "+rs.getInt( "user_age ")+ " </TD> ");
out.print( " <TD > "+rs.getString( "user_sex ")+ " </TD> ");
out.print( " <TD > "+rs.getString( "user_address ")+ " </TD> ");
out.print( " <TD > "+rs.getString( "user_telephone ")+ " </TD> ");
out.print( " <TD > "+rs.getString( "add_time ")+ " </TD> ");
out.print( " </TR> ") ;
}
out.print( " </Table> ");
con.close();
}
catch(SQLException e1)
{
out.print( "SQL异常! ");
}
%>
</BODY>
</HTML>
运行时是:SQL异常! " 我是错在那个地方?是MSSQL没有建立好?还是其它什么原因?
[解决办法]
你还是把异常打印出来吧。
[解决办法]
1try ..catch不要包含太多的方法体 不是什么地方都要抛异常
2将异常输出 要输出异常的本身 如
catch(SQLException e1)
{
out.print( "SQL异常!: "+sl.getMessage());
}
[解决办法]
把out.printStackTrace();改成System.out.printStrackTrace();