⑴ 怎麼用java實現mysql資料庫的導入導出
使用Java實現對MySql資料庫的導入與導出
packagecom.project.ajaxs;
importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.OutputStreamWriter;
importjava.util.Calendar;
importjava.util.Date;
publicclassBakMysql{
//main的方法,主要是我用於測試的,是想著取得CLASS的路徑,然後備份的文件寫在伺服器的類路徑下
publicstaticvoidmain(String[]args){
BakMysqlbk=newBakMysql();
bk.backup();
//bk.load();
}
//backup方法是備份資料庫到伺服器地址
publicvoidbackup(){
Calendarnow=Calendar.getInstance();
Stringname=now.getTime()+""+(now.getTime().getMonth()+1)+""+now.getTime().getDate();
Stringfilename=name.substring(24)+""+name.substring(11,13)+""+name.substring(14,16)+""+name.substring(17,19);
try{
StringfilePath="e:/project"+filename+".sql";
Runtimert=Runtime.getRuntime();
//調用mysql的cmd:
Processchild=rt.exec("C:/ProgramFiles/MySQL/MySQLServer5.0/bin/mysqlmp.exe-uroot-p8095longchunproject");//設置導出編碼為utf8。這里必須是utf8
//注意這一句,是指運行mysqlmp命令,後面跟的是登錄名和登錄的密碼,接著後面的是指備份的資料庫的名字,到此結束,以此生成一個執行的進程,取得此進程的輸出流到我們要備份的文件
//把進程執行中的控制台輸出信息寫入.sql文件,即生成了備份文件。註:如果不對控制台信息進行讀出,則會導致進程堵塞無法運行
InputStreamin=child.getInputStream();//控制台的輸出信息作為輸入流
InputStreamReaderxx=newInputStreamReader(in,"utf-8");//設置輸出流編碼為utf8。這里必須是utf8,否則從流中讀入的是亂碼
StringinStr;
StringBuffersb=newStringBuffer("");
StringoutStr;
//組合控制台輸出信息字元串
BufferedReaderbr=newBufferedReader(xx);
while((inStr=br.readLine())!=null){
sb.append(inStr+" ");
}
outStr=sb.toString();//備份出來的內容是一個字條串
//要用來做導入用的sql目標文件:
FileOutputStreamfout=newFileOutputStream(filePath);
OutputStreamWriterwriter=newOutputStreamWriter(fout,"utf8");
writer.write(outStr);//寫文件
//註:這里如果用緩沖方式寫入文件的話,會導致中文亂碼,用flush()方法則可以避免
writer.flush();
//別忘記關閉輸入輸出流
in.close();
xx.close();
br.close();
writer.close();
fout.close();
}catch(Exceptione){
e.printStackTrace();
}
}
//資料庫的導入
publicvoidload(){
try{
StringfPath="e:/aa.sql";
Runtimert=Runtime.getRuntime();
Processchild=rt.exec("C:/ProgramFiles/MySQL/MySQLServer5.0/bin/mysqladmin.exe-uroot-p8095longchuncreateproject");
Processchild1=rt.exec("C:/ProgramFiles/MySQL/MySQLServer5.0/bin/mysql.exe-uroot-p8095longchunproject");
OutputStreamout=child1.getOutputStream();//控制台的輸入信息作為輸出流
StringinStr;
StringBuffersb=newStringBuffer("");
StringoutStr;
BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(fPath),"utf-8"));
while((inStr=br.readLine())!=null){
sb.append(inStr+" ");
}
outStr=sb.toString();
OutputStreamWriterwriter=newOutputStreamWriter(out,"utf8");
writer.write(outStr);
//註:這里如果用緩沖方式寫入文件的話,會導致中文亂碼,用flush()方法則可以避免
writer.flush();
out.close();
br.close();
writer.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
原文來自http://www.cnblogs.com/anxz/archive/2012/11/19/2777782.html
⑵ java(eclipse) 文件導入導出
導入:
直接選擇常規裡面的
現有項目到工作空間中
而導出:
就要看你需要導出成Java項目
還是
jar包
如果是exe可執行文件
就需要先導出成jar然後藉助三方軟體如exe4j導出即可
⑶ java如何實現導入和導出功能
看你要導入什麼了,什麼格式,是電話還是其他軟體或者資料,只要兼容就能導入導出的。
⑷ eclipse里寫好的小Java程序怎麼導出可運行的小程序
1、打開eclipse,點擊File->New->Project,選擇java->java project,效果如圖所示
⑸ java使用什麼技術實現excel數據的批量導入導出
java使用第三方工具包POI技術實現excel數據的批量導入導出。
舉例如下:
1、下載apache的相關jar包。poi-ooxml-3.6.jar xmlbeans-2.3.0.jar等,如圖:
2、編寫相關的讀寫類
/**
* 讀取xls文件內容
*/
private
List<XlsDto> readXls() throws
IOException {
InputStream is = new
FileInputStream("test.xls");
HSSFWorkbook hssfWorkbook = new
HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new
ArrayList<XlsDto>();
// 循環工作表Sheet
for
(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if
(hssfSheet == null) {
continue;
}
// 循環行Row
for
(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if
(hssfRow == null) {
continue;
}
xlsDto = new
XlsDto();
// 循環列Cell
// 0學號 1姓名 2學院 3課程名 4 成績
// for (int cellNum = 0; cellNum <=4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if
(xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if
(xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if
(yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if
(kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if
(cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return
list;
}
3、導出就是輸入到一個新的excel文件裡面
public void writeXls(List<Student> list, String path) throws Exception {
if (list == null) {原始數據為空,直接返回
return;
}
int countColumnNum = list.size();//設置列數
HSSFWorkbook book = new HSSFWorkbook(); //創建工作表對象
HSSFSheet sheet = book.createSheet("studentSheet");
// 創建第一行
HSSFRow firstRow = sheet.createRow(0);
HSSFCell[] firstCells = new HSSFCell[countColumnNum];
//創建表頭
String[] options = { "no", "name", "age", "score" };
//循環數據域
for (int j = 0; j < options.length; j++) {
firstCells[j] = firstRow.createCell(j);
firstCells[j].setCellValue(new HSSFRichTextString(options[j]));
}
//處理每一個cell的值
for (int i = 0; i < countColumnNum; i++) {
HSSFRow row = sheet.createRow(i + 1);
Student student = list.get(i);
for (int column = 0; column < options.length; column++) {
HSSFCell no = row.createCell(0);
HSSFCell name = row.createCell(1);
HSSFCell age = row.createCell(2);
HSSFCell score = row.createCell(3);
no.setCellValue(student.getNo());
name.setCellValue(student.getName());
age.setCellValue(student.getAge());
score.setCellValue(student.getScore());
}
}
File file = new File(path);
OutputStream os = new FileOutputStream(file);
System.out.println(Common.WRITE_DATA + path);
book.write(os);
os.close();
}