java一个数据整理的方法

  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 }

 

转载于:https://www.cnblogs.com/ycz109/p/11474996.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
90.3 92.2 94.2 96.3 98.1]; p = polyfit(x, y, 1); disp(['斜率:' num2str(p(1))]); disp(['截距:' num2str(p(2))好的,以下是Java中常用的方法及其实例: 1. String类中的常用方法 - length()]); ``` 代码中,polyfit函数可以用于执行线性回归分析,1表示使用一次多项式进行:返回字符串的长度 - equals():比较两个字符串是否相等 - toUpperCase():将字符串转换为大写 - toLowerCase():将字符串转换为小写 - substring():截取字符串的一部分 -回归分析。执行代码后,将会得到回归直线的斜率和截距。 3. 插值 插值是一种常见的数值分析方法,用于估计未知数据点的值。MATLAB可以用 indexOf():查找字符串中某个字符或子串的位置 示例代码: ```java String str = "Hello World于执行各种类型的插值,例如线性插值、样条插值等。 以下是一个线性插值的"; System.out.println(str.length()); System.out.println(str.equals("Hello")); System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); System.out.println(str.substring(0, 5)); System.out.println(str.indexOf("World")); ``` 2. Math类中的常用方法例子。假设有一个包含10个数据点的数据集,其中x和y之间的关系未知。可以使用MATLAB的interp1函数执行线性插值。以下是MATLAB代码: ``` x = [1 2 - abs():返回绝对值 - pow():返回一个数的指定次幂 - sqrt():返回一个数的平方根 - random():返回一个0到1之间的随机数 示例代码: ```java 3 4 5 6 7 8 9 10]; y = [2 4 5 7 8 System.out.println(Math.abs(-10)); System.out.println(Math.pow(2, 3)); System.out.println(Math.sqrt(9)); System.out9 10 11 13 15]; xi = linspace(1, 10, 100); yi = interp1(x, y.println(Math.random()); ``` 3. ArrayList类中的常用方法 - add():向ArrayList中添加元素 - remove, xi, 'linear'); plot(x, y, 'o', xi, yi); ``` 代码中,interp1函数可以用于():从ArrayList中移除元素 - get():获取ArrayList中指定位置的元素 - size():获取ArrayList中元素的个数 示例代码: ```java ArrayList<String> list = new ArrayList<String>(); list.add("Java"); list.add("Python"); list.add("C++"); System.out.println(list.size()); System.out.println(list.get(1)); list.remove("Python"); System.out.println(list.size()); ``` 4. File类中的常用方法 - exists():判断文件是否存在 - isDirectory():判断是否为目录 - isFile():判断是否为文件 - getName():获取文件名 - getPath():获取文件路径 示例代码: ```java File file = new File("test.txt"); System.out.println(file.exists()); System.out.println(file.isDirectory()); System.out.println(file.isFile()); System.out.println(file.getName()); System.out.println(file.getPath()); ``` 以上是Java中常用的方法及其实例,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值