主题:[讨论]请教关于JDBC的问题!!
为什么我下面的程序会出错啊?
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*"%>
<%!
String DBDRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
String DBURL="jdbc:odbc:mldn";
Connection conn=null;
Statement pstmt=null;
String sql=null;
ResultSet rs=null;
%>
<%
//加载驱动程序
try
{
Class.forName(DBDRIVER);
}
catch(Exception e)
{
out.println("数据驱动程序加载失败");
}
//连接数据库
try
{
conn=DriverManager.getConnection(DBURL);
}
catch(Exception e)
{
out.println("连接数据库失败");
}
//操作数据库
try
{
sql="SELECT id,name,password,age FROM person";
pstmt=conn.createStatement();
rs=pstmt.executeQuery(sql);
while(rs.next())
{
if(rs.getInt("id")==8)
{
int id=rs.getInt("id");
String name=rs.getString("name");
String password=rs.getString("password");
int age=rs.getInt("age");
%>
<h1>ID:<%=id%>,姓名:<%=name%>,密码:<%=password%>,年龄:<%=age%></h1>
<%
}
}
}
catch(Exception e)
{
out.println("操作数据库失败");
}
//关闭数据库
try
{
rs.close();
pstmt.close();
conn.close();
}
catch(Exception e)
{
out.println("关闭数据库失败");
}
%>
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*"%>
<%!
String DBDRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
String DBURL="jdbc:odbc:mldn";
Connection conn=null;
Statement pstmt=null;
String sql=null;
ResultSet rs=null;
%>
<%
//加载驱动程序
try
{
Class.forName(DBDRIVER);
}
catch(Exception e)
{
out.println("数据驱动程序加载失败");
}
//连接数据库
try
{
conn=DriverManager.getConnection(DBURL);
}
catch(Exception e)
{
out.println("连接数据库失败");
}
//操作数据库
try
{
sql="SELECT id,name,password,age FROM person";
pstmt=conn.createStatement();
rs=pstmt.executeQuery(sql);
while(rs.next())
{
if(rs.getInt("id")==8)
{
int id=rs.getInt("id");
String name=rs.getString("name");
String password=rs.getString("password");
int age=rs.getInt("age");
%>
<h1>ID:<%=id%>,姓名:<%=name%>,密码:<%=password%>,年龄:<%=age%></h1>
<%
}
}
}
catch(Exception e)
{
out.println("操作数据库失败");
}
//关闭数据库
try
{
rs.close();
pstmt.close();
conn.close();
}
catch(Exception e)
{
out.println("关闭数据库失败");
}
%>