jsp连接oracle登录问题
写了一个jsp连接数据库的程序,代码如下,老是报错,求改正
<%@page import="java.sql.*"%>
<%!
public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";//驱动
public static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";
public static final String DBUSER="scott";
public static final String DBPASS="tiger";
%>
<%
String userid = request.getParameter("userid");
String password = request.getParameter("password");
boolean flag=false;
%>
<%
try{
Connection conn=null;
PreparedStatement ppstm=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql ="select userid,name from tuser where userid=? and password=?";
ppstm =conn.preparedStatement(sql);
ppstm.setString(1,userid);
ppstm.setString(2,password);
rs=ppstm.executeQuery();
if(rs.next()){
flag=true;
}
}catch(Exception e){
}finally{
try{
conn.close();
}catch(Exception e){}
}
if(flag){
%>
<jsp:forward page="right.jsp"/>
<%
}else{
%>
<jsp:forward page="wrong.jsp"/>
<%
}
%>
错误如下
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 21 in the jsp file: /check.jsp
The method preparedStatement(String) is undefined for the type Connection
18: Class.forName(DBDRIVER);
19: conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
20: String sql ="select userid,name from tuser where userid=? and password=?";
21: ppstm =conn.preparedStatement(sql);
22: ppstm.setString(1,userid);
23: ppstm.setString(2,password);
24: rs=ppstm.executeQuery();
An error occurred at line: 31 in the jsp file: /check.jsp
conn cannot be resolved
28: }catch(Exception e){
29: }finally{
30: try{
31: conn.close();
32: }catch(Exception e){}
33: }
34: if(flag){
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:443)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
[解决办法]
conn.preparedStatement(sql); 没有这个方法
应该是 conn.prepareStatement(sql);