日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

fatjar打包mysql_一个简单的解压ZIP Excl文件---gt;按时间读取Excle文件,通过Fat jar打成jar相对应的添加到mysql数据库中。 - 明月 - ITeye博客...

發(fā)布時間:2024/9/19 数据库 30 豆豆

寫道

一個簡單的解壓ZIP Excl文件--->按時間讀取Excle文件,通過Fat jar打成jar相對應的添加到mysql數(shù)據(jù)庫中。

package common.gaoyang;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

/**

* 文件的讀寫

*

* @author liming

*

*/

public class CardZipFileOperating {

private static final Logger logger = LoggerFactory.getLogger(CardZipFileOperating.class);

//??? // 解壓到的目標文件

//??? public static String directory = "E:\\demo\\";

//??? // 解壓文件

//??? public static String stringg = "F:\\繳費網(wǎng)\\DL20111213003195.zip";

/**

* 解壓,處理下載的zip工具包文件

*

* @param directory

*??????????? 要解壓到的目錄

* @param string

*??????????? 工具包文件

* @throws IOException

* @throws Exception

*???????????? 操作失敗時拋出異常

*/

public static int unzipFile(String directory,String stringg) throws IOException{

ZipInputStream zis=null;

int f=0;

try {

zis = new ZipInputStream(new FileInputStream(stringg));

} catch (Exception e) {

f++;

System.out.println("解壓ZIP文件失敗!");

System.out.println("沒有找到"+stringg+"目錄下文件,檢查下是不是文件的路徑格式寫的不對,如:D盤下的DL202111213003195.zip文件格式寫成:D:\\"+"\\DL202111213003195.zip");

}

if(zis!=null){

ZipEntry ze = null;

try {

ze = zis.getNextEntry();

} catch (IOException e) {

// TODO Auto-generated catch block

f++;

e.printStackTrace();

}

File parent = new File(directory);

if (!parent.exists() && !parent.mkdirs()) {

System.out.println("創(chuàng)建給文件解壓到那個 \"" + directory+ "\"目錄 失敗");

System.out.println("檢查下解壓的目錄是否格式寫錯:例如給文件解壓到E盤下的 demo文件夾下,路徑應該寫成:E:\\"+"\\demo\\"+"\\");

f++;

}else{

while (ze != null) {

String name = ze.getName();

File child = new File(parent, name);

FileOutputStream output = new FileOutputStream(child);

byte[] buffer = new byte[10240];

int bytesRead = 0;

while ((bytesRead = zis.read(buffer)) > 0) {

output.write(buffer, 0, bytesRead);

}

output.flush();

output.close();

ze = zis.getNextEntry();

}

}

zis.close();

}

return f;

}

// 函數(shù)GetTestXlsFileName功能:遍歷fileAbsolutePath目錄下的所有指定擴展名文件

// 并將文件名保存在Vector中

@SuppressWarnings("unchecked")

public static List getTestXlsFileName(String fileAbsolutePath) {

List list = new ArrayList();

File file = new File(fileAbsolutePath);

File[] subFile = file.listFiles();

if(subFile!=null){

for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {

// 判斷是否為文件夾

if (!subFile[iFileLength].isDirectory()) {

String tempName = subFile[iFileLength].getName();

// 判斷是否為xls或xlsx結尾

if (tempName.trim().toLowerCase().endsWith(".xls")

|| tempName.trim().toLowerCase().endsWith(".xlsx")) {

// 去掉tXXXX.xls文件的首字母t

// String fileName = tempName.substring(1);

list.add(tempName);

}

}

}

}

return list;

}

public static int OperatingExcl(String objectPath,String directory)

throws Exception {

int flag=0;

String pathoperratingExcl = directory + objectPath;

// 定義一個文件類

InputStream is = null;

// 定義一個工作book

Workbook workBook = null;

is = new FileInputStream(pathoperratingExcl);

// 獲取值

workBook = Workbook.getWorkbook(is);

// 獲取哪一頁sheet

Sheet sheet = workBook.getSheet(0);

@SuppressWarnings("unused")

Cell cell = null;

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://xx.xx.xx.xx:3306/realpay?useUnicode=true&characterEncoding=UTF-8";

String user = "xx";

String password = "xx";

Class.forName(driver);

Connection conn = DriverManager.getConnection(url, user, password);

Statement statement = conn.createStatement();

for (int j = 1; j < sheet.getRows(); j++) {

if(!sheet.getCell(1, j).getContents().equals("")){

String order_no=null;

String sp_order_no=null;

String order_status=null;

String shengfen=null;

String yunyingshang=null;

String amount=null;

String order_date=null;

String liushui=null;

String phone=null;

String danjia=null;

String shifu=null;

String daozhang=null;

String tuikuan=null;

String create_date=null;

String update_date=null;

String this_status=null;

order_no=sheet.getCell(2, j).getContents();

sp_order_no=sheet.getCell(3, j).getContents();

String order_status1=sheet.getCell(15, j).getContents();

if(order_status1.equals("失敗")){

order_status="0";

}else if(order_status1.equals("成功")){

order_status="1";

}else if(order_status1.equals(" ")){

order_status="2";

}else if(order_status1.equals("未付款")){

order_status="3";

}else if(order_status1.equals("部分退款")){

order_status="4";

}else{

order_status="2";

}

shengfen=sheet.getCell(6, j).getContents();

yunyingshang=sheet.getCell(7, j).getContents();

amount=sheet.getCell(9, j).getContents();

String ddd=sheet.getCell(1, j).getContents();

int index = ddd.indexOf(".");

String dddd=ddd.substring(0,index);

order_date=dddd;

liushui=sheet.getCell(4, j).getContents();

phone=sheet.getCell(10, j).getContents();

danjia=sheet.getCell(11, j).getContents();

shifu=sheet.getCell(12, j).getContents();

daozhang=sheet.getCell(13, j).getContents();

tuikuan=sheet.getCell(14, j).getContents();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

create_date=sdf.format(new Date());

update_date="0000-00-00 00:00:00";

this_status="1";

String sql = "INSERT INTO gaoyang_checking(order_no,sp_order_no,order_status,shengfen,yunyingshang,amount,order_date,liushui,phone,danjia,shifu,daozhang,tuikuan,create_date,update_date,this_status)" +

"VALUES" +

"('"+order_no+"','"+sp_order_no+"','"+order_status+"','"+shengfen+"','"+yunyingshang+"'," +

"'"+amount+"','"+order_date+"','"+liushui+"','"+phone+"','"+danjia+"'" +

",'"+shifu+"','"+daozhang+"','"+tuikuan+"','"+create_date+"','"+update_date+"','"+this_status+"')";

int? rs = statement.executeUpdate(sql);

if(rs>0){

//System.out.println("添加成功》》》》"+j);

flag++;

}else{

System.out.println("添加失敗》》》》");

System.out.println("失敗的sql"+sql);

}

}

}

conn.close();

conn.close();

workBook.close();

return flag;

}

public static int DelOperatingExcl(String objectPath){

int id=0;

try {

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://1xx.1xx8.1.2xx2:3306/realpay?useUnicode=true&characterEncoding=UTF-8";

String user = "xx";

String password = "xx";

Class.forName(driver);

Connection conn = DriverManager.getConnection(url, user, password);

Statement statement = conn.createStatement();

String sql = "DELETE? FROM gaoyang_checking? WHERE order_date LIKE '%"+objectPath+"%'";

System.out.println(sql);

id = statement.executeUpdate(sql);

conn.close();

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

return id;

}

/**

* 讀取excl文件并給存入到Map中去

*

* @throws Exception

*/

@SuppressWarnings("unchecked")

public static void readExcl(String adatete,String directory,String stringg) throws Exception {

int j=0;

//解壓到的文件?? 解壓文件

int zipa=unzipFile(directory,stringg);

if(zipa<1){

//String y=adatete.split("-")[1].split("-")[0];

//??? ??? String m=adatete.split("-")[0];

//??? ??? String d=adatete.split("-")[2];

String f1=adatete.substring(0,4);

String f2=adatete.substring(4,6);

String f3=adatete.substring(6,8);

String zTime=f1+"-"+f2+"-"+f3;

List list = getTestXlsFileName(directory);

String pas="DL20111213003195_"+adatete+".xls";

for (int i = 0; i < list.size(); i++) {

//System.out.println(list.get(i));

if(pas.equals(list.get(i))){

int shan=DelOperatingExcl(zTime);

if(shan>0){

System.out.println("數(shù)據(jù)庫有"+adatete+"記錄,已經(jīng)刪除!");

}else{

System.out.println("數(shù)據(jù)庫未有"+adatete+"記錄!");

}

//路徑?? 要添加的路徑

int chenggong=OperatingExcl(pas,directory);

System.out.println("添加已經(jīng)結束,添加了:"+chenggong+"條!");

}else{

j++;

}

if(j==list.size()){

System.out.println("沒有找到"+adatete+"日期相對應的excel文件文件");

System.out.println("1、當天沒有文件,2、檢查下日期是否寫錯,例如2012年2月9號的必須寫成:20120209");

}

}

}

}

public static void main(String[] args) {

try {

int dd=args.length;

if(dd==3){

System.out.println(dd);

String adatete = args[0];

String directory = args[1];

String stringg = args[2];

if(adatete.equals("") || adatete==null){

System.out.println("請按順序輸入3個參數(shù),1 日期、2解壓到的目錄 、3需要解壓的文件目錄");

}else if(directory.equals("") || directory==null){

System.out.println("請按順序輸入3個參數(shù),1 日期、2解壓到的目錄 、3需要解壓的文件目錄");

}else if(directory.equals("")? || directory==null){

System.out.println("請按順序輸入3個參數(shù),1 日期、2解壓到的目錄 、3需要解壓的文件目錄");

}else{

readExcl(adatete,directory,stringg);

}

}else{

System.out.println("請按順序輸入3個參數(shù),1 日期、2解壓到的目錄 、3需要解壓的文件目錄");

}

//??? ??? ??? String m=adatete.split("-")[0];

//??? ??? ??? String d=adatete.split("-")[2];

//readExcl("20120229","E:\\demo\\","F:\\繳費網(wǎng)\\DL20111213003195.zip");

} catch (Exception e) {

e.printStackTrace();

}

}

}

在eclipse下安裝fat jar插件進行給項目打成jar包:

安裝:

問題:有兩個項目,他們都是兩個引用包(沒main入口)。像這樣

這兩個項目,kernel是最底層的引用包,而androidConnection也是一個引用了kernel包的引用包,現(xiàn)在我想打包androidConnection,但是用普通的import成jar是不包括kernel的jar的。

因此搜索了一下,網(wǎng)上大概有幾個不錯的方法,這里我打算用fatjar,因為它作為eclipse的插件,用起來夠傻瓜式的,很方便。^-^

//下面是截取網(wǎng)上的教材

查看Eclipse如何打包的過程中我發(fā)現(xiàn)有個fatjar插件功能更強些,可以把需要的資源打進一個jar里。所以下載了一個,下載地址:http://sourceforge.net/project/showfiles.php?group_id=115990&package_id=125924

壓縮后是一個.../plugins/(net...)把plugins下面的(net..)文件夾拷貝到eclipse的plugins下,重新啟動

Eclipse3.1,Windows=>prefernce=>fat jar

preference看到他就說明已經(jīng)安裝成功了。如果沒有看到,沒有關系,刪除D:\eclipse310\configuration

\org.eclipse.update\platform.xml文件(此文件可以自動生成)不用擔心以前的插件會因此而消失,沒事。

剛開始找不到如何使用它,后來看到你的項目=〉Export..=>向導里有fat jar =>使用起來真的很方便。

(上面圖片的紅色框主要是填你的入口函數(shù),當然引用包是沒有的,因此你可以直接跳過什么都不用填,生成出來的jar包它會自動將你所引用到的其他jar包也包括進來)

總結:

一般把src打包export=>Jar File?即可。

想把src附屬的資源一起打包,必須使用FatJar => export..=>FatJar.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

最后在運行Jar包:cmd:? java -jar?? F:\Myeclipsworkspace\javaExcleDemo\javaExcleDemo_fat.jar??? E:\\demo\\??? F:\\繳費網(wǎng)\\DL20111213003195.zip

最后運行結果:

分享到:

2012-05-16 10:58

瀏覽 1190

評論

總結

以上是生活随笔為你收集整理的fatjar打包mysql_一个简单的解压ZIP Excl文件---gt;按时间读取Excle文件,通过Fat jar打成jar相对应的添加到mysql数据库中。 - 明月 - ITeye博客...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。