java一个数据整理的方法代码实例

 更新时间:2019年09月06日 17:05:16   作者:包饱饱  
这篇文章主要介绍了java一个数据整理的方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了java一个数据整理的方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

import java.sql.*;

public class Main {
   //本地数据库
  // static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  // static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/aite?useSSL=false&serverTimezone=UTC";
  // static final String USER = "root";
  // static final String PASS = "root";

  //测试服务器数据库
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://192.168.1.236:3306/aite?useSSL=false&serverTimezone=UTC";
  static final String USER = "root";
  static final String PASS = "c6f20omhhg";

  public static void main(String[] args) {
    // Connection conn = null;
    // Statement stmt = null;
    // Statement stmt2 = null;
    // try{
    //   Class.forName(JDBC_DRIVER);
    //   System.out.println("连接数据库...");
    //   conn = DriverManager.getConnection(DB_URL,USER,PASS);
    //   System.out.println(" 实例化Statement对象...");
    //   stmt = conn.createStatement();
    //   String sql;
    //   sql = "SELECT id,id2 FROM sys_code_region where id2>736978";
    //   ResultSet rs = stmt.executeQuery(sql);
    //   int a=2;
    //   while(rs.next()){
    //     String id= rs.getString("id");
    //     //String name = rs.getString("id2");
    //     int b=rs.getInt("id2");
    //     String sql2;
    //     sql2= "update base_area set area_code="+id+" where id='"+b+"'";
    //     stmt2 = conn.createStatement();
    //     //stmt2.executeQuery(sql2);
    //     stmt2.execute(sql2);
    //     a++;
    //     System.out.print("ID: " + id);
    //     System.out.print(", id2: " + b);
    //     System.out.print("\n");
    //   }
    //   rs.close();
    //   stmt.close();
    //   conn.close();
    // }catch(SQLException se){
    //   se.printStackTrace();
    // }catch(Exception e){
    //   e.printStackTrace();
    // }finally{
    //  try{
    //     if(stmt!=null) stmt.close();
    //   }catch(SQLException se2){
    //   }
    //   try{
    //     if(conn!=null) conn.close();
    //   }catch(SQLException se){
    //     se.printStackTrace();
    //   }
    // }
    // System.out.println("Success!");

    //getPids();

    setPriority();
  }

  /**
   * pids查询插入递归方法
   */
  public static void getPids(){
    Connection conn = null;
    Statement stmt = null;
    Statement stmt2 = null;
    String pids=null;
    try{
      Class.forName(JDBC_DRIVER);
      System.out.println("连接数据库...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);
      System.out.println(" 实例化Statement对象...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT id,pid,name FROM base_area where id=2210";
      ResultSet rs = stmt.executeQuery(sql);
      while(rs.next()){
        String str1=rs.getString("id");
        String str2=rs.getString("pid");
        String str3=rs.getString("name");
        int a =Integer.parseInt(str1);
        int b =Integer.parseInt(str2);
        pids=str2;
        int c=0;
        c=getPid(b);
        if (c>0){
          do{
            if (c >1) {
              pids = c + "," + pids;
              c = getPid(c);
            }
          }while (c>1);
          pids = c + "," + pids;
        }
        stmt2 = conn.createStatement();
        String sql2;
        sql2= "update base_area set pids='"+pids+"' where id="+a;
        stmt2.execute(sql2);
        System.out.println("id: "+a+",name:"+str3+", pids: "+pids);
      }
      rs.close();
      stmt.close();
      conn.close();
    }catch(SQLException se){
      se.printStackTrace();
    }catch(Exception e){
     e.printStackTrace();
    }finally{
      try{
        if(stmt!=null) stmt.close();
      }catch(SQLException se2){
      }
      try{
        if(conn!=null) conn.close();
      }catch(SQLException se){
        se.printStackTrace();
      }
    }
    System.out.println("Success!");
  }

  /**
   * pid 查询方法
   * @param id
   * @return pid
   */
  public static Integer getPid(int id){
    Connection conn3 = null;
    Statement stmt3 = null;
    int pid =0;
    try{
    conn3= DriverManager.getConnection(DB_URL,USER,PASS);
    stmt3=conn3.createStatement();
    String sql3="select pid from base_area where id="+id;
    ResultSet rs3=stmt3.executeQuery(sql3);
    while (rs3.next()) {
      String str = rs3.getString("pid");
      pid = Integer.parseInt(str);
    }
      rs3.close();
      stmt3.close();
      conn3.close();
     }catch(SQLException se){
      se.printStackTrace();
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try{
        if(stmt3!=null) stmt3.close();
      }catch(SQLException se2){
      }
      try{
        if(conn3!=null) conn3.close();
      }catch(SQLException se){
        se.printStackTrace();
      }

    }
    return pid;
  }

  /**
   * 排序方法
   */
  public static void setPriority(){
    try {
      System.out.println("连接数据库...");
      Connection conn4 = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println(" 实例化Statement对象...");
      Statement stmt4 = conn4.createStatement();
      ResultSet rs4 = stmt4.executeQuery("select id from base_area ");
      while (rs4.next()){
        int id = rs4.getInt("id");
        Statement stmt5=conn4.createStatement();
        String sqlstr1="select id,pid from base_area where pid="+id+" ORDER BY id ";
        ResultSet rs5=stmt5.executeQuery(sqlstr1);
        rs5.last();
        int count=rs5.getRow();
        if(count>0) {
          rs5.beforeFirst();
          int i=1;
          while (rs5.next()) {
            int id2 = rs5.getInt("id");
            Statement stmt6 = conn4.createStatement();
            String sqlstr2="update base_area set priority=" + i + " where id=" + id2;
            stmt6.execute(sqlstr2);
            System.out.println("id:" + id2 + ", priority:" + i);
            i++;
          }
        }
        //System.out.println(id);
      }
    }
    catch (SQLException se){
      se.printStackTrace();
    }
    catch (Exception e){
       e.printStackTrace();
    }
     System.out.println("Complete!");
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • spring(java,js,html) 截图上传图片实例详解

    spring(java,js,html) 截图上传图片实例详解

    这篇文章主要介绍了spring(java,js,html) 截图上传图片实例详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • SpringCloud网关(Zuul)如何给多个微服务之间传递共享参数

    SpringCloud网关(Zuul)如何给多个微服务之间传递共享参数

    这篇文章主要介绍了SpringCloud网关(Zuul)如何给多个微服务之间传递共享参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • springboot如何实现异步响应请求(前端请求超时的问题解决)

    springboot如何实现异步响应请求(前端请求超时的问题解决)

    这篇文章主要给大家介绍了关于springboot如何实现异步响应请求(前端请求超时的问题解决)的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用springboot具有一定的参考学习价值,需要的朋友可以参考下
    2023-01-01
  • Java+MySQL实现图书管理系统(完整代码)

    Java+MySQL实现图书管理系统(完整代码)

    这篇文章主要介绍了Java+MySQL实现图书管理系统(完整代码),本文给大家介绍的非常想详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • 解决maven项目tomcat启动失败war exploded:Error during artifact deploym问题

    解决maven项目tomcat启动失败war exploded:Error during 

    在SpringMVC项目中,使用war和warexploded两种部署方式可能会导致不同的路径问题,从而出现404错误,war模式将项目打包上传,而warexploded模式则保持文件夹结构上传,开发时建议使用warexploded模式,方便利用Update classes and resources功能自动更新
    2024-10-10
  • idea导入springboot项目没有maven的解决

    idea导入springboot项目没有maven的解决

    这篇文章主要介绍了idea导入springboot项目没有maven的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • 基于Java编写一个简单的内部网段扫描程序

    基于Java编写一个简单的内部网段扫描程序

    这篇文章主要为大家详细介绍了如何基于Java编写一个简单的内部网段扫描程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解下
    2025-02-02
  • 远程debug调试入门

    远程debug调试入门

    这篇文章主要介绍了Eclipse的Debug调试技巧大全(总结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧吗,希望能给你带来帮助
    2021-06-06
  • 使用mybatis切片实现数据权限控制的操作流程

    使用mybatis切片实现数据权限控制的操作流程

    数据权限控制需要对查询出的数据进行筛选,对业务入侵最少的方式就是利用mybatis或者数据库连接池的切片对已有业务的sql进行修改,本文给大家介绍了使用mybatis切片实现数据权限控制的操作流程,需要的朋友可以参考下
    2024-07-07
  • java中抽象类和接口的相同和不同点介绍

    java中抽象类和接口的相同和不同点介绍

    大家好,本篇文章主要讲的是java中抽象类和接口的相同和不同点介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12

最新评论