java一个数据整理的方法代码实例
这篇文章主要介绍了java一个数据整理的方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
importjava.sql.*;
publicclassMain{
//本地数据库
//staticfinalStringJDBC_DRIVER="com.mysql.jdbc.Driver";
//staticfinalStringDB_URL="jdbc:mysql://127.0.0.1:3306/aite?useSSL=false&serverTimezone=UTC";
//staticfinalStringUSER="root";
//staticfinalStringPASS="root";
//测试服务器数据库
staticfinalStringJDBC_DRIVER="com.mysql.jdbc.Driver";
staticfinalStringDB_URL="jdbc:mysql://192.168.1.236:3306/aite?useSSL=false&serverTimezone=UTC";
staticfinalStringUSER="root";
staticfinalStringPASS="c6f20omhhg";
publicstaticvoidmain(String[]args){
//Connectionconn=null;
//Statementstmt=null;
//Statementstmt2=null;
//try{
//Class.forName(JDBC_DRIVER);
//System.out.println("连接数据库...");
//conn=DriverManager.getConnection(DB_URL,USER,PASS);
//System.out.println("实例化Statement对象...");
//stmt=conn.createStatement();
//Stringsql;
//sql="SELECTid,id2FROMsys_code_regionwhereid2>736978";
//ResultSetrs=stmt.executeQuery(sql);
//inta=2;
//while(rs.next()){
//Stringid=rs.getString("id");
////Stringname=rs.getString("id2");
//intb=rs.getInt("id2");
//Stringsql2;
//sql2="updatebase_areasetarea_code="+id+"whereid='"+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(SQLExceptionse){
//se.printStackTrace();
//}catch(Exceptione){
//e.printStackTrace();
//}finally{
//try{
//if(stmt!=null)stmt.close();
//}catch(SQLExceptionse2){
//}
//try{
//if(conn!=null)conn.close();
//}catch(SQLExceptionse){
//se.printStackTrace();
//}
//}
//System.out.println("Success!");
//getPids();
setPriority();
}
/**
*pids查询插入递归方法
*/
publicstaticvoidgetPids(){
Connectionconn=null;
Statementstmt=null;
Statementstmt2=null;
Stringpids=null;
try{
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库...");
conn=DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("实例化Statement对象...");
stmt=conn.createStatement();
Stringsql;
sql="SELECTid,pid,nameFROMbase_areawhereid=2210";
ResultSetrs=stmt.executeQuery(sql);
while(rs.next()){
Stringstr1=rs.getString("id");
Stringstr2=rs.getString("pid");
Stringstr3=rs.getString("name");
inta=Integer.parseInt(str1);
intb=Integer.parseInt(str2);
pids=str2;
intc=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();
Stringsql2;
sql2="updatebase_areasetpids='"+pids+"'whereid="+a;
stmt2.execute(sql2);
System.out.println("id:"+a+",name:"+str3+",pids:"+pids);
}
rs.close();
stmt.close();
conn.close();
}catch(SQLExceptionse){
se.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(stmt!=null)stmt.close();
}catch(SQLExceptionse2){
}
try{
if(conn!=null)conn.close();
}catch(SQLExceptionse){
se.printStackTrace();
}
}
System.out.println("Success!");
}
/**
*pid查询方法
*@paramid
*@returnpid
*/
publicstaticIntegergetPid(intid){
Connectionconn3=null;
Statementstmt3=null;
intpid=0;
try{
conn3=DriverManager.getConnection(DB_URL,USER,PASS);
stmt3=conn3.createStatement();
Stringsql3="selectpidfrombase_areawhereid="+id;
ResultSetrs3=stmt3.executeQuery(sql3);
while(rs3.next()){
Stringstr=rs3.getString("pid");
pid=Integer.parseInt(str);
}
rs3.close();
stmt3.close();
conn3.close();
}catch(SQLExceptionse){
se.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(stmt3!=null)stmt3.close();
}catch(SQLExceptionse2){
}
try{
if(conn3!=null)conn3.close();
}catch(SQLExceptionse){
se.printStackTrace();
}
}
returnpid;
}
/**
*排序方法
*/
publicstaticvoidsetPriority(){
try{
System.out.println("连接数据库...");
Connectionconn4=DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("实例化Statement对象...");
Statementstmt4=conn4.createStatement();
ResultSetrs4=stmt4.executeQuery("selectidfrombase_area");
while(rs4.next()){
intid=rs4.getInt("id");
Statementstmt5=conn4.createStatement();
Stringsqlstr1="selectid,pidfrombase_areawherepid="+id+"ORDERBYid";
ResultSetrs5=stmt5.executeQuery(sqlstr1);
rs5.last();
intcount=rs5.getRow();
if(count>0){
rs5.beforeFirst();
inti=1;
while(rs5.next()){
intid2=rs5.getInt("id");
Statementstmt6=conn4.createStatement();
Stringsqlstr2="updatebase_areasetpriority="+i+"whereid="+id2;
stmt6.execute(sqlstr2);
System.out.println("id:"+id2+",priority:"+i);
i++;
}
}
//System.out.println(id);
}
}
catch(SQLExceptionse){
se.printStackTrace();
}
catch(Exceptione){
e.printStackTrace();
}
System.out.println("Complete!");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。