jsp 连接数据库问题
我在自学jsp,在看到连接数据库时,自己动手连接下,就是连接不上,流浪器提示如下:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 11 in the jsp file: /jsp/4.jsp
Duplicate local variable con
8: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
9:
10:
11: Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433/jsp;DatabaseName=test","sa","123456");
12: sql=con.createStatement();
13: rs=sql.executeQuery("SELECT * FROM student");
14: out.print("<Table Border>");
代码如下:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% Connection con;
Statement sql;
ResultSet rs;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433/jsp;DatabaseName=test","sa","123456");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM student");
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+" 学号");
out.print("<TH width=100>"+" 姓名");
out.print("<TH width=50>"+" 数学成绩");
out.print("<TH width=50>"+" 英语成绩");
out.print("<TH width=50>"+" 物理成绩");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
out.print("<TD >"+rs.getString(1)+"</TD>");
out.print("<TD >"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getInt(" 数学成绩")+"</TD>");
out.print("<TD >"+rs.getInt(" 英语成绩")+"</TD>");
out.print("<TD >"+rs.getInt(" 物理成绩")+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
%>
</BODY>
</HTML>
------解决方案--------------------con引用重复申明了。
把
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433/jsp;DatabaseName=test","sa","123456");
改成
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433/jsp;DatabaseName=test","sa","123456");
就可以了
------解决方案--------------------错误不全,第八行猜测,驱动jar包没有导入项目。com.microsoft.jdbc.sqlserver.SQLServerDriver