zoukankan      html  css  js  c++  java
  • 偷懒工具设计之sql2Excel

     偷懒工具设计之sql2Excel

            今天在公司陪山东客户调试,远程登录,我在linux下什么工具都没有,用ssh登录服务器,直接用mysql查询数据库,提出记录中的所有汉字全是乱码。哎,可恶的公司,不让我用windows,要不我就可以用putty或者EMS了,我ft!
             甚是不爽之下,我决定自己写个工具了,把客户数据库中的数据全部提取并保存到Excel中,这样我不就可以一目了然了嘛,嘿嘿,好吧,那我就写一个工具吧。

    第一部分就是谁都会的jdbc操作,连接数据库,提取数据集合。
        Connection con;
        Statement state;
        
    /**初始化连接
         * 
    @param serverIp
         * 
    @param dataBase
         * 
    @param userName
         * 
    @param password
         * 
    @throws ClassNotFoundException
         * 
    @throws SQLException
         
    */

        
    public void init(String serverIp,String dataBase,String userName,String password) throws ClassNotFoundException, SQLException{
            Class.forName(
    "com.mysql.jdbc.Driver");
            
    //配置数据源
            String url="jdbc:mysql://"+serverIp+"/"+dataBase+"?useUnicode=true&characterEncoding=GB2312";
            con
    =DriverManager.getConnection(url,userName,password); 
        }

        
    /**得到查询结果集
         * 
    @param sql
         * 
    @return
         * 
    @throws SQLException
         
    */

        
    public ResultSet getResultSet(String sql) throws SQLException{
            state 
    = con.createStatement();
            ResultSet res 
    = state.executeQuery(sql);
            
    return res;
        }

        
    /**关闭连接
         * 
    @throws SQLException
         
    */

        
    public void close() throws SQLException{
            
    if(con!=null)
                con.close();
            
    if(state!=null)
                state.close();
        }

    第二部分就是把ResultSet中的记录写入一个Excel文件
    操作Excel,我用的是jxl,不熟的同学可以参考:利用java操作Excel文件
        /**将查询结果写入Excel文件中
         * 
    @param rs
         * 
    @param file
         * 
    @throws SQLException
         
    */

        
    public void writeExcel(ResultSet rs,File file) throws SQLException{
              WritableWorkbook wwb 
    = null;
                
    try{
                    
    //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
                    wwb = Workbook.createWorkbook(file);
                }
     catch (IOException e){
                    e.printStackTrace();
                }

                
    if(wwb!=null){
                    WritableSheet ws 
    = wwb.createSheet("sheet1"0);
                    
    int i=0;
                    
    while(rs.next()){
                        Label label1 
    = new Label(0, i, rs.getString("id"));
                        Label label2 
    = new Label(1, i, rs.getString("category"));
                         
    try {
                            ws.addCell(label1);
                            ws.addCell(label2);
                        }
     catch (RowsExceededException e) {
                            e.printStackTrace();
                        }
     catch (WriteException e) {
                            e.printStackTrace();
                        }

                        i
    ++;
                    }
        

                    
    try {
                        
    //从内存中写入文件中
                        wwb.write();
                        
    //关闭资源,释放内存
                        wwb.close();
                    }
     catch (IOException e) {
                        e.printStackTrace();
                    }
     catch (WriteException e){
                        e.printStackTrace();
                    }

                }

        }

    测试程序:
            Sql2Excel se = new Sql2Excel();
            
    try {
                se.init(
    "127.0.0.1","mydabase""root""1234");
                ResultSet rs 
    = se.getResultSet("select id,category from xx ");
                se.writeExcel(rs, 
    new File("/root/sql2excel.xls"));
                se.close();
            }
     catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
     catch (SQLException e) {
                e.printStackTrace();
            }


             呵呵,比较简单吧,不过还是很有用的,其实那些数据库查询工具EMS,Mysql Query Browser之类的和这个差不多,掌握了原理,我们也可以自己开发查询工具,备份工具。
  • 相关阅读:
    画江恩线
    从装饰者模式的理解说JAVA的IO包
    Form表单标签的Enctype属性的作用及应用示例介绍
    spring中的通配符
    简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
    Java数据封装成树形结构,多级
    详解InitializingBean、initMethod和@PostConstruct
    SpringCloud确保服务只能通过gateway转发访问,禁止直接调用接口访问
    Spring中的InitializingBean接口的使用
    Linux下Centos7对外开放端口
  • 原文地址:https://www.cnblogs.com/hehe520/p/6330238.html
Copyright © 2011-2022 走看看