日期:2014-05-18  浏览次数:20694 次

大家帮我看看数据库连接的小问题,谢谢.
下面是个连接数据库的小程序,用SQL2000建立数据库Vote,然后建两个表voter和result.下面的javaBean文件名(conn.java)package vote;
import java.sql.*;
public class conn{
  String DBDdriver="sun.jdbc.odbc.JdbcOdbcDriver";

  String rsDBDdriver="jdbc:odbc:vote";

  Connection con=null;
  ResultSet rs=null;

  public conn(){
  try{
  Class.forName(DBDdriver);
  }catch(java.lang.ClassNotFoundException e){
  System.err.println(e.getMessage());
  }
  }

  public ResultSet executeQuery(String sql){
  rs=null;
  try{
  con=DriverManager.getConnection(rsDBDdriver); 
  Statement stmt=con.createStatement();
  rs=stmt.executeQuery(sql);
  }catch(SQLException ex){
  System.err.println(ex.getMessage());
  }  

  return rs;
}
}


下面是显示表格的jsp文件(show.jsp)
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="voteBean" class="vote.conn" scope="page"/>
<html>
<head>
<title>SQL SHOW!</title>
<meta http-equiv="Content-Type" content="text/http;charset=gb-2312-80">
<meta content="blueriver" name="Author">
<meta http-equiv="refresh" content="10;URl=show.jsp">
</head>
<body bgcolor="red">
 <div align="center">
  <h1>当前统计票数</h1>
  <table border="1" width="60%">
  <tr>
  <td width="51%">候选公司名</td>
  <td width="49%">当前票数</td>
  </tr>
  <%
  ResultSet rst=voteBean.executeQuery("select * from result");
  String companyName;
  int voteNum;
  while(rst.next()){
  companyName=rst.getString("candidate");
  voteNum=rst.getInt("vote_num");
  %> 
  <tr>
  <td width="51%"><%=companyName%></td>
  <td width="49%"><%=voteNum%></td> 
  </tr>
  <%
  }
  rst.close(); 
  %> 
 </table>
 </div>
 <div align="center">
  <h1>投票人的详细资料</h1>
  <table border="1" width="100%">
  <tr>
  <td>投票的公司</td>
  <td>投票人姓名</td>
  <td>投票人所在公司</td>
  <td>投票人所在国家</td>
  <td>投票人的IP地址</td>
  <td>投票时间</td>
  </tr>
  <%
  ResultSet RS_vote;
  RS_vote=voteBean.executeQuery("select * from voter");
  String candidate1,name,company1,country,address;
  java.util.Date time;
  while(RS_vote.next()){
  candidate1=RS_vote.getString("candidate");
  name=RS_vote.getString("voter_name");
  company1=RS_vote.getString("voter_company");
  country=RS_vote.ge