日期:2014-05-17  浏览次数:20986 次

jsp通过驱动程序连接sqlserver2005数据库,查询数据,显示表名无效
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>连接数据库</title>
</head>
<body>
<%!
 Connection con;
 Statement sql;
 ResultSet rs;
%>
<%
 try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatebaseName=test";
con=DriverManager.getConnection(url,"sa","si");
sql=con.createStatement();
String sqlexe="select * from"+" "+"\"user\"";
rs=sql.executeQuery(sqlexe);
rs.next();
out.println(rs.getString(1));

}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e.getMessage());


out.println("数据库连接成功!");
%>
</body>
</html>


在sqlserver2005中用select * from "user"可以查询到语句

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: An exception occurred processing JSP page /test/test.jsp at line 30

27: 
28: }catch(Exception e){
29:  e.printStackTrace();
30:  throw new RuntimeException(e.getMessage());
31: } 
32: 
33: out.println("数据库连接成功!");


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.RuntimeException: 对象名  'user' 无效。
org.apache.jsp.test.test_jsp._jspService(test_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.


--------------------------------------------

Apache Tomcat/7.0.27