X-man

导航

java 连接数据库

1.获取服务器端数据库blog中记录数

package dataprocess;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class newDBshower {

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URI = "jdbc:mysql://202.114.46.54:3306/corpus?useServerPrepStmts=false&rewriteBatchedStatements=true&characterEncoding=utf8";
    private static final String username = "ccnunlp";
    private static final String password = "ccnunlp";

    public newDBshower(String year) throws IOException {
        try {
            Class.forName(DRIVER);
            Connection conn = DriverManager.getConnection(URI, username, password);

            int start = 0;
            int size = 10000;
            
            String corpus = "blog";
            //String year = "2015";
            String sql = "select count(*) from " + corpus ;
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery(sql);
            rst.next();
            System.out.println(rst.getInt(1));
            rst.close();
//            while (true) {
//                String sql = "select * from " + corpus + " where id >= " + start + " and id < " + (start + size)+" and date like '" +year+"%'" ;
//                PreparedStatement pst = conn.prepareStatement(sql);
//                ResultSet rst = pst.executeQuery();
//
//                int n_count = 0;
//                while (rst.next()) {
//                    n_count++;
//                    String content = rst.getString("content");
//                    int id = rst.getInt("id");
//                    int date = rst.getInt("date");
//                    //System.out.println(rst.getString("date"));
//                    //System.out.println(content);
//                    //System.out.println(content.substring(0, Math.min(content.length(), 30)));
//                     String s = String.valueOf( id);
//                    BufferedWriter bw = new BufferedWriter(new FileWriter("C:/data/blog/"+year+"/"+date+"-"+s+".txt"));
//                    bw.write(content+'\n');
//                    bw.close();
//                }
//
//                rst.close();
//
//                System.out.println(start);
//
//                if (n_count == 0)
//                    break;
//
//                start += size;
//            }
            
            
            /*
             * while (rs.next()) { System.out.println(rs.getString("id") + "\t"
             * + rs.getString("author").trim() + "\t\t" +
             * rs.getString("violenceScore"));
             * 
             * bw.write(rs.getString("id")+"\t"+rs.getString("content"));
             * bw.newLine(); }
             * 
             * 
             * bw.close();
             * 
             */

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } /*
             * catch (IOException e) { // TODO Auto-generated catch block
             * e.printStackTrace(); }
             */
    }

    public static void main(String[] args) throws IOException {
        newDBshower my = new newDBshower(args[0]);
    }
}

 

 

SQL基础知识

posted on 2016-04-29 15:32  雨钝风轻  阅读(230)  评论(0编辑  收藏  举报