<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.net.*"%>
<%@page import="java.text.*"%><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<jsp:useBean id="tools" class="tool.ToolBox"/>
<title>test</title>
</head><body bgcolor="#7B0000" text="#FFFF00" vlink="#663399" alink="#FF0000">
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="181" height="232" valign="top"> 
      <table width="100%" height="202" border="1" cellpadding="0" cellspacing="0">
        <tr> 
          <td width="28%"><font color="#333399"><b><img src="image/win.gif" width="14" height="11" /><font color="#FFFF00">论坛公告:</font></b></font></td>
          <td width="72%"> <marquee width="100%" height="100%" direction="up">
<%
Connection con = tools.getConn ();
Statement  stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql = "select title,post_date,content from bulletin_info order by id desc";
String Notice="";
int i=0; //Number of messages to be shown in bulletin message bar
ResultSet  rs = stmt.executeQuery(sql);
while ((rs.next()) && (i<5))
{
String Time=rs.getString("post_date");
Time=Time.substring(5,16);
Notice="<B>"+tools.ex_chinese(rs.getString("title"))+":</B>" + 
"<font color=black>"+tools.ex_chinese(rs.getString("content"))+"</font>" + 
"<font color=gray>&nbsp;"+Time+"</font>&nbsp;&nbsp;&nbsp;" + 
Notice;
i++;
}
out.println(Notice);
%>
            </marquee> </td>
        </tr>
      </table>
    <p>&nbsp;</p></td>
    <td width="569" valign="top">&nbsp; </td>
  </tr>
</table>
<br />
</body>
</html>

解决方案 »

  1.   

    这是bean:
    ///////////////////////////////////////////////////////////
    //function:
    //1) get connection from the forum database which is base on
    //   jdbc-odbc connection bridge, and return the ronncetion.
    //2) format the given string into chinese GBcode
    //3) get the current time and return as a string
    ///////////////////////////////////////////////////////////package tool;import java.io.*;
    import java.sql.*;
    import java.text.*;
    import java.util.*;public class ToolBox
    {
       private Connection conn;   //construction
       public ToolBox ()
       {
       }   //get connection from database
       public Connection getConn ()
       {
          try {
             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
             conn = DriverManager.getConnection ("jdbc:odbc:forum");
          } catch (Exception e) {
             e.printStackTrace ();
             System.out.println ("Database Connection Encounted an Exception" + e.getMessage());
          }
          return conn;
       }   //change the string into chinese GBcode
       public String ex_chinese (String str)
       {
          if (str == null) {
             str = "";
          }
          else {
             try {
                str = new String (str.getBytes ("iso-8859-1"), "gb2312");
             } catch (Exception ex) {
             }
          }
          return str;
       }   //get the current time and return as a string
       public String getcurrTime ()
       {
          String datestr = "";
          try {
             DateFormat df = new SimpleDateFormat ("yyyy-M-d HH:ss");
             java.util.Date date = new java.util.Date ();
             datestr = df.format (new java.util.Date ());
          } catch (Exception ex) {
          }
          return datestr;
       }
    }
      

  2.   

    there should be username and password of your ODBC
      

  3.   

    to bdsc():
    but i havn't set the username and password when i create my database.
    which name and password should i use?
      

  4.   

    I don't want to say too much
    Could you just try to set them and provide them in java code, first?
      

  5.   

    i've just tried it, but i don't think it was belonging to the reason you've just say.
    here is my testing:
    when i change the centence in my jsp file:
    Notice = "<B>"+tools.ex_chinese(rs.getString("title"))+":</B>" + 
             "<font color=black>"+tools.ex_chinese(rs.getString("content"))+
             "</font>" + "<font color=gray>&nbsp;"+Time+"</font>&nbsp;&nbsp;&nbsp;" 
             + Notice;into:Notice = "<font color=black>"+tools.ex_chinese(rs.getString("content"))+
             "</font>" + 
             "<font color=gray>&nbsp;"+Time+"</font>&nbsp;&nbsp;&nbsp;" + Notice;it can runs normally. but when i convert it into the former one, it was encount an exception which was shown above.
    of course, i've tried to set the username and password before my testing, but i'm sure my connection between my jsp file and database, because i'd connected the database sucessfully for many times!
    can you tell me why did it encount the miracle exception? 
    thanks!!!!!!!!!!
      

  6.   

    Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);==>>
     Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);It is the most flexible mode, if it works, you can narrow down till the best performance reached