你写的乱套了·?为什么不写在一个类中,然后用BEAN调用呢?为什么不去遵循MVC体系结构呢?咳······这样堆出来的东西谁也不愿意看………………

解决方案 »

  1.   

    <%@ page import="java.sql.*"%>
    <% out.println("test mysql database!");
      try{
          Class.forName("org.gjt.mm.mysql.Driver").newInstance();
      }catch(Exception e){out.println("Unable to load driver.");}
      try{
          Connection cn=DriverManager.getConnection    ("jdbc:mysql://localhost/addressbook?user=root&password=root&useUnicode=true&characterEncoding=8859_1");
          Statement stmt=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
          ResultSet rs=stmt.executeQuery("select * from addressbook");
      for (int i=1;i<20;i++){
                 if (rs.absolute(i)){
      out.println(rs.getString(1));
      out.println(rs.getString(2)));
      out.println(rs.getString(3)));
               else{break;}
      }
      rs.close();
      stmt.close();
      cn.close();
      }
      catch (SQLException e)
      {
      out.println("SQLException:"+e.getMessage());
      out.println("SQLState:"+e.getSQLState());
      out.println("VendorError:"+e.getErrorCode());
      }
      %>
    调试结果:
    test mysql database! SQLException:Unable to connect to any hosts due to exception: java.net.ConnectException: Connection refused: connect SQLState:08S01 VendorError:0
      

  2.   

    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.sql.*"%> 
    <html> 
    <body> 
    <%
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    /*或String url ="jdbc:mysql://localhost/test?user=root&password=&useUnicode=true&characterEncoding=8859_1";
    Connection conn=DriverManager.getConnection(url);
    */
    String url ="jdbc:mysql://localhost:3306/test" ; //test为你的数据库名 
    Connection conn= DriverManager.getConnection(url,"","");  //或Connection conn= DriverManager.getConnection(url);
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
    String sql="select * from t_temp"; 
    ResultSet rs=stmt.executeQuery(sql); 
    while(rs.next()) 
    {
        out.println(rs.getString(1)+"<br>");
    }
    rs.close(); 
    stmt.close(); 
    conn.close(); 
    %> 
    </body> 
    </html>
    将mysql数据库驱动mm.mysql-2.0.4-bin.jar这个包放到webapps\ROOT\WEB-INF\lib目录下,然后重新启动tomcat服务器就可以了。