zoukankan      html  css  js  c++  java
  • 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+'
    ');
    //                    bw.close();
    //                }
    //
    //                rst.close();
    //
    //                System.out.println(start);
    //
    //                if (n_count == 0)
    //                    break;
    //
    //                start += size;
    //            }
                
                
                /*
                 * while (rs.next()) { System.out.println(rs.getString("id") + "	"
                 * + rs.getString("author").trim() + "		" +
                 * rs.getString("violenceScore"));
                 * 
                 * bw.write(rs.getString("id")+"	"+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基础知识

  • 相关阅读:
    对网页图片的增删改管理
    还没搞完的排序(后期更新)
    web实现图片动态
    C++11 笔记
    如何解决刷新系统桌面响应速度很慢的问题
    CGrowableArray解析 _ DXUT容器
    测试...外部指针访问private
    CustomUI Direct3D9_Sample
    缺少.lib文件导致的Link2019 解决方案汇总
    在DirectX9中使用DXUT定制按钮来控制模型旋转的问题
  • 原文地址:https://www.cnblogs.com/XDJjy/p/5446498.html
Copyright © 2011-2022 走看看