连接数据库问题
文件connetcion.jsp
<%@page import= "java.util.* "%>
<%@page import= "java.sql.* "%>
<%
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch(ClassNotFoundException e){}
try{
Connection con = DriverManager.getConnection( "jdbc:odbc:stu ", "sa ", "sa ");
Statement stm=con.createStatement();
Statement stm1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(SQLException e1){}
%>
文件student.jsp
<%@page contentType= "text/html;charset=gb2312 "%>
<%@page import= "java.sql.* "%>
<%@ include file= "connetcion.jsp "%>
<!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>
<%!
String stm1;
String stu_name,stu_id,sex;
String sql= "select * from student ";
ResultSet sqlRst=stm1.executeQuery(sql);
%>
<body>
<table border= "1 " width= "400 " align= "center ">
<tr align= "center " bgcolor= "pink ">
<td> 学号 </td>
<td> 姓名 </td>
<td> 性别 </td>
</tr>
<%
while(sqlRst.next()){
stu_id=sqlRst.getString( "stu_id ");
stu_name=sqlRst.getString( "stu_name ");
sex =sqlRst.getString( "sex ");
%>
<tr align= "center " >
<td> <%=stu_id%> </td>
<td> <%=stu_name%> </td>
<td> <%=sex%> </td>
</tr>
<%
}
%>
</table>
<%
sqlRst.close();
%>
</body>
</html>
报错:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 10 in the jsp file: /student.jsp
Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\fenyue\org\apache\jsp\student_jsp.java:17: cannot find symbol
symbol : method executeQuery(java.lang.String)
location: class java.lang.String
ResultSet sqlRst=stm1.executeQuery(sql);
^
An error occurred at line: 10 in the jsp file: /student.jsp
Generated servlet error:
Note: D:\Tomcat 5.0\work\Catalina\localhost\fenyue\org\apache\jsp\student_jsp.java uses unchecked or unsafe operations.
An error occurred at line: 10 in the jsp file: /student.jsp
Generated servlet error:
Note: Recompile with -Xlint:unchecked for details.
1 error
怎么解决啊,刚学不懂
[解决办法]
你这个例子哪里找来的,写的太差了,稍微改了下你全部复制就能用
------------------------------
<%@page import= "java.util.* "%>
<%@page import= "java.sql.* "%>
??student.jsp
<%@page contentType= "text/html;charset=gb2312 "%>
<%@page import= "java.sql.* "%>
<%@ include file= "connetcion.jsp "%>
<!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>
<table border= "1 " width= "400 " align= "center ">
<tr align= "center " bgcolor= "pink ">
<td> 学号 </td>
<td> 姓名 </td>
<td> 性别 </td>
</tr>
<%
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
Connection con = DriverManager.getConnection( "jdbc:odbc:stu ", "sa ", "sa ");
Statement stm=con.createStatement();
Statement stm1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String stu_name,stu_id,sex;
String sql= "select * from student ";
sqlRst=stm1.executeQuery(sql);
while(sqlRst.next()){
stu_id=sqlRst.getString( "stu_id ");
stu_name=sqlRst.getString( "stu_name ");
sex =sqlRst.getString( "sex ");
%>
<tr align= "center " >
<td> <%=stu_id%> </td>
<td> <%=stu_name%> </td>
<td> <%=sex%> </td>
</tr>
<%
}
sqlRst.close();
}
catch(SQLException e1){}
%>
</table>
</body>
</html>
------解决方案--------------------
文件connetcion.jsp 这么修改就可以
<%@page import= "java.util.* "%>
<%@page import= "java.sql.* "%>
<%
Connection con=null;
Statement stm=null;
Statement stm1=null;
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch(ClassNotFoundException e){}
try{
con = DriverManager.getConnection( "jdbc:odbc:stu ", "sa ", "sa ");
stm=con.createStatement();
stm1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch(SQLException e1){}
%>
因为原先你是在try里面定义Connection,Statement,所以有效范围只在try{}里面
[解决办法]
上面各位都是好人
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html