当前位置: 代码迷 >> Java Web开发 >> 大家帮小弟我看看数据库连接的小疑点,多谢
  详细解决方案

大家帮小弟我看看数据库连接的小疑点,多谢

热度:532   发布时间:2016-04-17 12:15:30.0
大家帮我看看数据库连接的小问题,谢谢.
下面是个连接数据库的小程序,用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.getString("voter_country");  
  address=RS_vote.getString("ip_address");
  time=RS_vote.getStrig("voter_time");
  %>
  <tr>
  <td><%=candidate1%></td>
  <td><%=name%></td>
  <td><%=company1></td>
  相关解决方案