求sql分页利用java的变量的代码

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
creatFile(fn, FILE_NAME);
Class.forName("com.mysql.jdbc.Driver");

System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);


System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement(); 
int sunNum = 30;
int pagesize = 0; pagesize = sunNum/2;
for(int i=0;i<=pagesize;i++) {

String sql = "SELECT ID,name, url From websites limit i*2,2;";
ResultSet rs = stmt.executeQuery(sql);

String fn = "F://NEWtest//again4.txt";
wf(fn,"ID,name,url.");
while(rs.next()){

    int id  = rs.getInt("id"); 
     String name = rs.getString("name");
    String url = rs.getString("url");
    //String country = rs.getString("country");
    //String fourth = rs.getString("fourth");
      System.out.print(", FR: " + id); 

    System.out.print(", SE: " + name);

    System.out.print(", TH: " + url);
    //System.out.print(", FO: " + country);

    System.out.print("\n");
    wf(fn,id+ name + url );
}

rs.close();
stmt.close();
conn.close();}
    看看我的代码对不,想利用循环分页,目前不知道怎么样把java的变量赋值给sql,帮忙解决一下,写个代码出来吧,我刚学java不到一个星期,谢谢。

4个回答

之前你的提问:java用limit循环读取mysql直到全部读完,并能显示读了多少条,我给你写了个通用工具,你没采纳,建议你还是研究下的给你贴的代码

public int getPage(String param){
int recordCount=0,t1=0,t2=0;
if(param==null)
{
param="";
}
String param_new = "%"+param+"%";
PreparedStatement pstmt=null;
ResultSet rs=null;
JdbcUtil jdbc=new JdbcUtil();
Connection conn=jdbc.connect();
String sql="select count(*) from webnews where wnkeyword like ? or wntitle like ? or introreading like ?;";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,param_new);
pstmt.setString(2,param_new);
pstmt.setString(3,param_new);
rs=pstmt.executeQuery();
rs.next();
recordCount=rs.getInt(1);
t1=recordCount%5;
t2=recordCount/5;
} catch (Exception e) {

    e.printStackTrace();
    }finally{
    jdbc.close(conn, pstmt, rs);
    }
    if(t1 != 0){
    t2=t2+1;
    }
    return t2;
    }
    /**
    * 查询指定页的数据
    * @param pageNo
    * @return
    */
    public List<WebNews> listWebNews(int pageNo,String param){
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    List<WebNews> list=new ArrayList<WebNews>();
    int pageSize=5;
    int page=(pageNo-1)*5;
    if(param==null)
    {
        param="";
    }
    String param_new = "%"+param+"%";
    JdbcUtil jdbc=new JdbcUtil();
    Connection conn=jdbc.connect();
    String sql="select * from webnews where wnkeyword like ? or wntitle like ? or introreading like ? order by wnid limit ?,?;";
    //String sql1="select * from webnews where wnkeyword order by wnid limit ?,?;";
    try {
    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,param_new);
    pstmt.setString(2,param_new);
    pstmt.setString(3,param_new);
    pstmt.setInt(4, page);
    pstmt.setInt(5, pageSize);
    rs=pstmt.executeQuery();
    while(rs.next()){
        WebNews webnews = new WebNews();
        webnews.setWnid(rs.getInt("wnid"));
        webnews.setWntitle(rs.getString("wntitle"));
        webnews.setWnurl(rs.getString("wnurl"));
        webnews.setWnkeyword(rs.getString("wnkeyword"));
        webnews.setWnimgurl(rs.getString("wnimgurl"));
        webnews.setWnmedia(rs.getString("wnmedia"));
        webnews.setWncreatetime(rs.getString("wncreatetime"));
        webnews.setWntext(rs.getString("wntext"));
        webnews.setTplink1(rs.getString("tplink1"));
        webnews.setTplink2(rs.getString("tplink2"));
        webnews.setIntroreading(rs.getString("introreading"));
        list.add(webnews);
    }
    } catch (Exception e) {

    e.printStackTrace();
    }finally{
    jdbc.close(conn, pstmt, rs);
    }
    return list;
    }

java变量传到SQL里面,在SQL里面写问好,然后在stmt.set里面设置

例如总共1000条以上记录,循环每次查询100条
int sum=1010;
int pageSize=100;
int pageNum = sum%pageSize==0?sum%pageSize:(sum%pageSize)+1;
String sql = "";
for(int i=1;i<=pageNum;i++){
String sql = "SELECT ID,name, url From websites limit "+(i-1)*pageSize,pageSize;
ResultSet rs = stmt.executeQuery(sql);
.....
}


Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐