rt

解决方案 »

  1.   

    http://www.d8net.cn/read.php?tid=16837
    这里连接代码比较齐全。
      

  2.   

    <%@ page contentType="text/html;charset=GB2312" %>
    <%@ page import="java.sql.*" %>
    <%
    int dipage=1;//当前页码数默认为1
    String pages=request.getParameter("dipage");
    if(pages==null)
    {
    pages="1";
    }
    try
    {
    dipage=Integer.parseInt(pages);
    }
    catch(Exception e)
    {
    dipage=1; 
    }
    %>
    <HTML>
    <head>
    <title>用户数据</title>
    <link href="mystyle.css" rel="stylesheet" type="text/css">
    </head>
    <BODY>
    <center>
     <% Connection con;
        Statement sql; 
        ResultSet rs;
        try
        {
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        }
        catch(ClassNotFoundException e)
        {
          out.print("类找不到!");
        }
    // try
    //    {
    //      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动程序
    //    }
    //    catch(ClassNotFoundException e)
    //    {
    //      out.print("类找不到!");
    //    }
        try 
        {  
             con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs","sa","");
             sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    //         con=DriverManager.getConnection("jdbc:odbc:pubs","sa","");
    //         sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    //  sql.setMaxRows(10);//控制输出记录最大数量为10
             rs=sql.executeQuery("SELECT * FROM titles");
             int countRecord=0;//记录条数
             int countPageRecord=0;//每页记录条数
             int countPage=0;//总页数
             countPageRecord=5;//每页5条记录,要设置每页记录条数就更改这个变量的值
             //得到记录的条数
             rs.last();
             countRecord=rs.getRow();
    //  out.print(countRecord);
             //得到总页数
             if(countRecord/countPageRecord==0)
                countPage=countRecord/countPageRecord;
             else
                countPage=countRecord/countPageRecord+1;
             //把记录指针移至当前页第一条记录之前
             if((dipage-1)*countPageRecord==0)
                rs.beforeFirst();
             else
                rs.absolute((dipage-1)*countPageRecord);
             out.print("<Table Border style='font-size: 10pt'>");
             out.print("<TR><td colspan=10 align=center><strong>用户数据</strong></td></tr>");   
             out.print("<TR>");
                out.print("<Td nowrap>"+"title_id");
                out.print("<Td nowrap>"+"title");
                out.print("<Td nowrap>"+"type");
                out.print("<Td nowrap>"+"pub_id");
                out.print("<Td nowrap>"+"price");
                out.print("<Td nowrap>"+"advance");
                out.print("<Td nowrap>"+"royalty");
                out.print("<Td nowrap>"+"ytd_sales");
    //            out.print("<Td nowrap>"+"notes");
                out.print("<Td nowrap>"+"pubdate");
             out.print("</TR>");
           int i=0;
           while(rs.next())
           { out.print("<TR>");
                 out.print("<TD nowrap>"+rs.getString("title_id")+"</TD>");
                 out.print("<TD nowrap>"+rs.getString("title")+"</TD>"); 
                 out.print("<TD nowrap>"+rs.getString("type")+"</TD>");
                 out.print("<TD nowrap>"+rs.getString("pub_id")+"</TD>"); 
                 out.print("<TD nowrap>"+rs.getFloat("price")+"</TD>");
                 out.print("<TD nowrap>"+rs.getLong("advance")+"</TD>");
                 out.print("<TD nowrap>"+rs.getInt("royalty")+"</TD>");
                 out.print("<TD nowrap>"+rs.getInt("ytd_sales")+"</TD>");
    //             out.print("<TD nowrap>"+rs.getString("notes")+"</TD>");
                 out.print("<TD nowrap>"+rs.getString("pubdate")+"</TD>");
            out.print("</TR>") ;
            i++;
            if(i>=countPageRecord) break; //当前页显示完,则退出循环
            }
            out.print("<TR><td colspan=10 align=center>");
               out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
               if(dipage==1)//当前是首页
                  ;
               else//当前不是首页
               {
                  out.print("<a href=userPage1.jsp?dipage=1>首页</a>,");
                  out.print("<a href=userPage1.jsp?dipage="+(dipage-1)+">上一页</a>,");
               }
               if(dipage==countPage)//当前是末页
                  ;
               else//当前不是末页
               {
                  out.print("<a href=userPage1.jsp?dipage="+(dipage+1)+">下一页</a>,");
                  out.print("<a href=userPage1.jsp?dipage="+countPage+">末页</a>");
               }          
               
               out.print("</td></tr>"); 
            out.print("</Table>");
            con.close();
         }
       catch(SQLException e1) 
       {
          out.print(e1+"<br>");
          out.print("SQL异常!");
       }
     %>
     </center>
    </BODY>
    </HTML>