我用JSP连接oracle,在开头导入了java.sql.*,但是还是用不了PrepareStatement,错误信息如下:
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 17 in the jsp file: /WEB-INF/tags/oracle.tag
PrepareStatement cannot be resolved to a type
14: out.print(e);
15: }
16: Connection conn;
17: PrepareStatement pstmt;
18: ResultSet rs;
19: try{
20:
An error occurred at line: 23 in the jsp file: /WEB-INF/tags/oracle.tag
DatabaseMetadata cannot be resolved to a type
20:
21: String URL="jdbc:oracle:thin:@localhost:1521:"+database;
22: conn=DriverManager.getConnection(URL,username,password);
23: DatabaseMetadata metadata=conn.getMetadata();
24: ResultSet rshead=metadata.getColumns(null,null,tablename,null);
25: int headCount=0;
26: result.append("<table border=1>");
An error occurred at line: 23 in the jsp file: /WEB-INF/tags/oracle.tag
The method getMetadata() is undefined for the type Connection
20:
21: String URL="jdbc:oracle:thin:@localhost:1521:"+database;
22: conn=DriverManager.getConnection(URL,username,password);
23: DatabaseMetadata metadata=conn.getMetadata();
24: ResultSet rshead=metadata.getColumns(null,null,tablename,null);
25: int headCount=0;
26: result.append("<table border=1>");
不仅如此DatabaseMetadata和getMetadata()也不能用,这些不是sql包里面自带的吗?求高手解答!
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 17 in the jsp file: /WEB-INF/tags/oracle.tag
PrepareStatement cannot be resolved to a type
14: out.print(e);
15: }
16: Connection conn;
17: PrepareStatement pstmt;
18: ResultSet rs;
19: try{
20:
An error occurred at line: 23 in the jsp file: /WEB-INF/tags/oracle.tag
DatabaseMetadata cannot be resolved to a type
20:
21: String URL="jdbc:oracle:thin:@localhost:1521:"+database;
22: conn=DriverManager.getConnection(URL,username,password);
23: DatabaseMetadata metadata=conn.getMetadata();
24: ResultSet rshead=metadata.getColumns(null,null,tablename,null);
25: int headCount=0;
26: result.append("<table border=1>");
An error occurred at line: 23 in the jsp file: /WEB-INF/tags/oracle.tag
The method getMetadata() is undefined for the type Connection
20:
21: String URL="jdbc:oracle:thin:@localhost:1521:"+database;
22: conn=DriverManager.getConnection(URL,username,password);
23: DatabaseMetadata metadata=conn.getMetadata();
24: ResultSet rshead=metadata.getColumns(null,null,tablename,null);
25: int headCount=0;
26: result.append("<table border=1>");
不仅如此DatabaseMetadata和getMetadata()也不能用,这些不是sql包里面自带的吗?求高手解答!
DatabaseMetadata metadata=conn.getMetadata();这句也错了
DatabaseMetaData metadata=conn.getMetadata();
多用用IDE,你要都自己打的话,你会死的很惨
==》得到的对象是PreparedStatement对象(有d)
这里不知道为什么会是这样,可能是BUG 也可能是其他原因 写代码的时候自己注意就好。
PrepareStatement cannot be resolved to a type,就是编译错误的意思,难道你的jsp再myeclipse里没报错么?就是出现红色XX。。
下面是tag文件代码:
<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="database" required="true" %>
<%@ attribute name="tablename" required="true" %>
<%@ attribute name="username" required="true" %>
<%@ attribute name="password" required="true" %>
<%@ variable name-given="queryResult" scope="AT_END" %>
<%
StringBuffer result=new StringBuffer();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(Exception e){
out.print(e);
}
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
try{
String URL="jdbc:oracle:thin:@localhost:1521:"+database;
conn=DriverManager.getConnection(URL,username,password);
DatabaseMetaData metadata=conn.getMetaData();
ResultSet rshead=metadata.getColumns(null,null,tablename,null);
int headCount=0;
result.append("<table border=1>");
result.append("<tr>");
while(rshead.next()){
headCount++;
String columnName=rshead.getString(4);
result.append("<td>"+columnName+"</td>");
}
result.append("</tr>");
pstmt=conn.prepareStatement("select * from "+tablename);
rs=pstmt.executeQuery();
while(rs.next()){
result.append("<tr>");
for(int i=1;i<=headCount;i++){
result.append("<td>"+rs.getString(i)+"</td>");
}
result.append("</tr>");
}
result.append("</table>");
rs.close();
pstmt.close();
conn.close();
}
catch(SQLException e){
result.append("用户名或密码错误");
}
jspContext.setAttribute("queryResult",new String(result));
%>
jsp文件:
<%@page contentType="text/html;charset=gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="odbc"%>
<html>
<body>
<odbc:oracle database="ORCL" tablename="goods" username="vendition" password="vendition"/>
<br>在表中查询到记录:
<br><%=queryResult%>
</body>
</html>
DatabaseMetaData metadata=conn.getMetaData();
ResultSet rshead=metadata.getColumns(null,null,tablename,null);
感谢各位大神!