码迷,mamicode.com
首页 > 编程语言 > 详细

java一个数据整理的方法

时间:2019-09-06 16:08:09      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:ack   order by   rgs   连接数   nec   out   statement   mys   stack   

  1 import java.sql.*;
  2 
  3 public class Main {
  4      //本地数据库
  5     // static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  6     // static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/aite?useSSL=false&serverTimezone=UTC";
  7     // static final String USER = "root";
  8     // static final String PASS = "root";
  9 
 10     //测试服务器数据库
 11     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 12     static final String DB_URL = "jdbc:mysql://192.168.1.236:3306/aite?useSSL=false&serverTimezone=UTC";
 13     static final String USER = "root";
 14     static final String PASS = "c6f20omhhg";
 15 
 16     public static void main(String[] args) {
 17         // Connection conn = null;
 18         // Statement stmt = null;
 19         // Statement stmt2 = null;
 20         // try{
 21         //     Class.forName(JDBC_DRIVER);
 22         //     System.out.println("连接数据库...");
 23         //     conn = DriverManager.getConnection(DB_URL,USER,PASS);
 24         //     System.out.println(" 实例化Statement对象...");
 25         //     stmt = conn.createStatement();
 26         //     String sql;
 27         //     sql = "SELECT id,id2 FROM sys_code_region where id2>736978";
 28         //     ResultSet rs = stmt.executeQuery(sql);
 29         //     int a=2;
 30         //     while(rs.next()){
 31         //         String id= rs.getString("id");
 32         //         //String name = rs.getString("id2");
 33         //         int b=rs.getInt("id2");
 34         //         String sql2;
 35         //         sql2= "update base_area set area_code="+id+" where id=‘"+b+"‘";
 36         //         stmt2 = conn.createStatement();
 37         //         //stmt2.executeQuery(sql2);
 38         //         stmt2.execute(sql2);
 39         //         a++;
 40         //         System.out.print("ID: " + id);
 41         //         System.out.print(", id2: " + b);
 42         //         System.out.print("\n");
 43         //     }
 44         //     rs.close();
 45         //     stmt.close();
 46         //     conn.close();
 47         // }catch(SQLException se){
 48         //     se.printStackTrace();
 49         // }catch(Exception e){
 50         //     e.printStackTrace();
 51         // }finally{
 52         //    try{
 53         //         if(stmt!=null) stmt.close();
 54         //     }catch(SQLException se2){
 55         //     }
 56         //     try{
 57         //         if(conn!=null) conn.close();
 58         //     }catch(SQLException se){
 59         //         se.printStackTrace();
 60         //     }
 61         // }
 62         // System.out.println("Success!");
 63 
 64         //getPids();
 65 
 66         setPriority();
 67     }
 68 
 69     /**
 70      * pids查询插入递归方法
 71      */
 72     public static void getPids(){
 73         Connection conn = null;
 74         Statement stmt = null;
 75         Statement stmt2 = null;
 76         String pids=null;
 77         try{
 78             Class.forName(JDBC_DRIVER);
 79             System.out.println("连接数据库...");
 80             conn = DriverManager.getConnection(DB_URL,USER,PASS);
 81             System.out.println(" 实例化Statement对象...");
 82             stmt = conn.createStatement();
 83             String sql;
 84             sql = "SELECT id,pid,name FROM base_area where id=2210";
 85             ResultSet rs = stmt.executeQuery(sql);
 86 
 87             while(rs.next()){
 88                 String str1=rs.getString("id");
 89                 String str2=rs.getString("pid");
 90                 String str3=rs.getString("name");
 91                 int a =Integer.parseInt(str1);
 92                 int b =Integer.parseInt(str2);
 93                 pids=str2;
 94                 int c=0;
 95                 c=getPid(b);
 96                 if (c>0){
 97                     do{
 98                         if (c >1) {
 99                             pids = c + "," + pids;
100                             c = getPid(c);
101                         }
102                     }while (c>1);
103                     pids = c + "," + pids;
104                 }
105                 stmt2 = conn.createStatement();
106                 String sql2;
107                 sql2= "update base_area set pids=‘"+pids+"‘ where id="+a;
108                 stmt2.execute(sql2);
109                 System.out.println("id: "+a+",name:"+str3+", pids: "+pids);
110             }
111             rs.close();
112             stmt.close();
113             conn.close();
114         }catch(SQLException se){
115             se.printStackTrace();
116         }catch(Exception e){
117           e.printStackTrace();
118         }finally{
119             try{
120                 if(stmt!=null) stmt.close();
121             }catch(SQLException se2){
122             }
123             try{
124                 if(conn!=null) conn.close();
125             }catch(SQLException se){
126                 se.printStackTrace();
127             }
128         }
129         System.out.println("Success!");
130     }
131 
132     /**
133      * pid 查询方法
134      * @param id
135      * @return pid
136      */
137     public static Integer getPid(int id){
138         Connection conn3 = null;
139         Statement stmt3 = null;
140         int pid =0;
141         try{
142         conn3= DriverManager.getConnection(DB_URL,USER,PASS);
143         stmt3=conn3.createStatement();
144         String sql3="select pid from base_area where id="+id;
145         ResultSet rs3=stmt3.executeQuery(sql3);
146         while (rs3.next()) {
147             String str = rs3.getString("pid");
148             pid = Integer.parseInt(str);
149         }
150             rs3.close();
151             stmt3.close();
152             conn3.close();
153          }catch(SQLException se){
154             se.printStackTrace();
155         }catch(Exception e){
156             e.printStackTrace();
157         }finally{
158             try{
159                 if(stmt3!=null) stmt3.close();
160             }catch(SQLException se2){
161             }
162             try{
163                 if(conn3!=null) conn3.close();
164             }catch(SQLException se){
165                 se.printStackTrace();
166             }
167 
168         }
169         return pid;
170     }
171 
172     /**
173      * 排序方法
174      */
175     public static void setPriority(){
176         try {
177             System.out.println("连接数据库...");
178             Connection conn4 = DriverManager.getConnection(DB_URL, USER, PASS);
179             System.out.println(" 实例化Statement对象...");
180             Statement stmt4 = conn4.createStatement();
181             ResultSet rs4 = stmt4.executeQuery("select id from base_area ");
182             while (rs4.next()){
183                 int id = rs4.getInt("id");
184                 Statement stmt5=conn4.createStatement();
185                 String sqlstr1="select id,pid from  base_area where pid="+id+" ORDER BY id ";
186                 ResultSet rs5=stmt5.executeQuery(sqlstr1);
187                 rs5.last();
188                 int count=rs5.getRow();
189                 if(count>0) {
190                     rs5.beforeFirst();
191                     int i=1;
192                     while (rs5.next()) {
193                         int id2 = rs5.getInt("id");
194                         Statement stmt6 = conn4.createStatement();
195                         String sqlstr2="update base_area set priority=" + i + " where id=" + id2;
196                         stmt6.execute(sqlstr2);
197                         System.out.println("id:" + id2 + ", priority:" + i);
198                         i++;
199                     }
200                 }
201                 //System.out.println(id);
202             }
203         }
204         catch (SQLException se){
205             se.printStackTrace();
206         }
207         catch (Exception e){
208               e.printStackTrace();
209         }
210          System.out.println("Complete!");
211     }
212 }

 

java一个数据整理的方法

标签:ack   order by   rgs   连接数   nec   out   statement   mys   stack   

原文地址:https://www.cnblogs.com/ycz109/p/11474996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!