當前位置:首頁 » 數據倉庫 » java導出資料庫txt
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java導出資料庫txt

發布時間: 2022-04-23 06:42:07

Ⅰ java代碼怎樣將oracle資料庫中數據下載本地,為.txt文件或者.excel文件。

第一個類:

package totabel.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import topdf.TableToPdf;
import totabel.view.TabelData;
import totabel.xls.ExcelDemo;

public class TableAction implements ActionListener {
TabelData data;

public TableAction(TabelData data) {
this.data = data;
}

public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("添加".equals(str)) {
data.addData();
} else if ("導出到Excel".equals(str)) {
ExcelDemo demo = new ExcelDemo();
demo.method(data);
} else if ("刪除".equals(str)) {
if (data.getRow() != -1) {
data.delRow();
} else {
JOptionPane.showMessageDialog(null, "請選擇要刪除的行!");
}
}else if("從Excel導入".equals(str)){
data.getXlsInfo();
}else if("從Excel導入到資料庫".equals(str)){
data.toDb();
}else if("從table導出到pdf".equals(str)){
TableToPdf pdf=new TableToPdf();
pdf.newPage(data);
}else if("計算學分".equals(str)){
data.getXlsInfoToCredit();
}
}

}

第二個類:資料庫連接
package totabel.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConnection {
private static JdbcConnection con;

public static JdbcConnection getCon() {
if (con == null) {
con = new JdbcConnection();
}
return con;
}

public Connection getConnection() {
Connection connection=null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle";
String user = "scott";
String password = "tiger";
connection = DriverManager.getConnection(url, user,
password);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// public static void main(String[] args) {
// JdbcConnection connection=new JdbcConnection();
// connection.getConnection("asd", "99");
// }

}

第三個類:主類(入口)
package totabel.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConnection {
private static JdbcConnection con;

public static JdbcConnection getCon() {
if (con == null) {
con = new JdbcConnection();
}
return con;
}

public Connection getConnection() {
Connection connection=null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle";
String user = "scott";
String password = "tiger";
connection = DriverManager.getConnection(url, user,
password);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// public static void main(String[] args) {
// JdbcConnection connection=new JdbcConnection();
// connection.getConnection("asd", "99");
// }

}
第四個類:
package totabel.xls;

import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import totabel.view.TabelData;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelDemo {

/**
*
* @param args
*/
private Vector title = new Vector();

private Vector[] array;

// public static void main(String[] args) {
// ExcelDemo demo = new ExcelDemo();
// demo.getXlsInfo();
//
// }

public void method(TabelData table) {
int row = table.getRowSize();
int column = table.getColumnSize();
WritableWorkbook book = null;
Vector title = table.setTitle();
Object[] str = title.toArray();
try {
book = Workbook.createWorkbook(new File("test.xls"));
WritableSheet sheet = book.createSheet("成績表", 0);
for (int i = 0; i < str.length; i++) {
sheet.addCell(new Label(i, 0, (String) str[i]));
}
for (int i = 1; i < row + 1; i++) {
for (int j = 1; j < column + 1; j++) {
sheet.addCell(new Label(j - 1, i, table.getTableInfo(i - 1,
j - 1)));
}
}
book.write();
JOptionPane.showMessageDialog(null, "導出完成!");
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
book.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 輸出Excel的數據到表單
*
* @return
*/
public Vector getXlsInfo() {
Vector v = new Vector();
jxl.Workbook rwb = null;
int index = 0;
try {
rwb = jxl.Workbook.getWorkbook(new File("test.xls"));
Sheet[] sheet = rwb.getSheets();
for (int i = 0; i < sheet.length; i++) {
int rs = sheet[i].getRows();
array = new Vector[rs - 1];
for (int j = 1; j < rs; j++) {
Cell[] cell = sheet[i].getRow(j);
Vector info = new Vector();
for (int k = 0; k < cell.length; k++) {
info.add(cell[k].getContents());
}
array[index] = info;
index++;
v.add(info);
}
Cell[] titleCell = sheet[i].getRow(0);
for (int j = 0; j < titleCell.length; j++) {
title.add(titleCell[j].getContents());
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
rwb.close();
}
return v;
}

public Vector getXlsInfoToCredit() {
Vector v = new Vector();
jxl.Workbook rwb = null;
try {
rwb = jxl.Workbook.getWorkbook(new File("d:/test/信科0821(南遷).xls"));
Sheet[] sheet = rwb.getSheets();
for (int i = 0; i < sheet.length; i++) {
int rs = sheet[i].getRows();
array = new Vector[rs - 1];
for (int j = 1; j < rs; j++) {
Cell[] cell = sheet[i].getRow(j);
Vector info = new Vector();
for (int k = 0; k < cell.length; k++) {
// if(){
Pattern p = Pattern.compile("[0-9]{1,}");
Matcher m = p.matcher(cell[k].getContents());
if (m.matches()) {
int score = Integer.valueOf(cell[k].getContents());
float result = getScore(score);
info.add(result);
} else {
info.add(cell[k].getContents());
}
}
v.add(info);
}
Cell[] titleCell = sheet[i].getRow(0);
for (int j = 0; j < titleCell.length; j++) {
title.add(titleCell[j].getContents());
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
rwb.close();
}
return v;
}

public float getScore(int n) {
float score = n;
if (n < 60) {
score = 0;
return score;
} else {
if (n >= 60 && n <= 63) {
score = (float) 1.0;
} else if (n >= 64 && n <= 67) {
score = (float) 1.3;
} else if (n >= 68 && n <= 71) {
score = (float) 1.7;
} else if (n >= 72 && n <= 75) {
score = (float) 2.0;
} else if (n >= 76 && n <= 79) {
score = (float) 2.3;
} else if (n >= 80 && n <= 83) {
score = (float) 2.7;
} else if (n >= 84 && n <= 87) {
score = (float) 3.0;
} else if (n >= 88 && n <= 91) {
score = (float) 3.3;
} else if (n >= 92 && n <= 95) {
score = (float) 3.7;
} else if (n >= 96 && n <= 100) {
score = (float) 4.0;
}
return score;
}

}

public Vector getTitle() {
// getXlsInfo();
return title;
}

public Vector[] getArray() {
getXlsInfo();
return array;
}

}

因為時間問題就沒有再寫了,上面是我以前做的,不懂就q我

Ⅱ 怎麼用JAVA把Mysql資料庫中的表的數據輸出至文本文檔中

首先導入 mysql-connector-java-5.1.45-bin.jar 包

代碼如下:

importjava.io.*;
importjava.sql.*;

publicclassApp{

publicstaticvoidmain(String[]args){

try{

Class.forName("com.mysql.jdbc.Driver");

//資料庫用戶
Stringuser="root";

//資料庫密碼
Stringpassword="";

Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_sale",user,password);

Statementstmt=conn.createStatement();

//查詢,從資料庫db_sale的proct表中查詢id,name,qty欄位
ResultSetrs=stmt.executeQuery("SELECTid,name,qtyFROMproct");

//創建輸出文件result.txt
Filefile=newFile("d://result.txt");
OutputStreamWriterwriter=newOutputStreamWriter(newFileOutputStream(file));

while(rs.next()){

writer.write(String.valueOf(rs.getLong(1))+" ");
writer.write(rs.getString(2)+" ");
writer.write(String.valueOf(rs.getInt(3)));
writer.write(" ");

//System.out.println(rs.getLong(1));
//System.out.println(rs.getString(2));
//System.out.println(rs.getLong(3));
}

writer.flush();
writer.close();

rs.close();
stmt.close();
conn.close();

}catch(Exceptione){
e.printStackTrace();
}
}
}

Ⅲ 用java代碼把txt文檔中資料導入到資料庫

BufferedReader input;
try {
String s = new String();
input = new BufferedReader(new FileReader("f:\\123.txt"));
while ((s = input.readLine()) != null) { // 判斷是否讀到了最後一行
String info[] = s.split(" ");
System.out.println( info[0] + " " + info[1] + " " + info[2] );
}
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
把info[0] + " " + info[1] + " " + info[2] 這三個值放在insert語句里就行了 經過測試

Ⅳ java如何從資料庫讀取數據並寫入txt文件

寫Java程序時經常碰到要讀如txt或寫入txt文件的情況,但是由於要定義好多變數,經常記不住,每次都要查,特此整理一下,簡單易用,方便好懂!

[java]viewplain
packagee.thu.keyword.test;

importjava.io.File;
importjava.io.InputStreamReader;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileInputStream;
importjava.io.FileWriter;

publicclasscin_txt{
staticvoidmain(Stringargs[]){
try{//防止文件建立或讀取失敗,用catch捕捉錯誤並列印,也可以throw

/*讀入TXT文件*/
Stringpathname="D:\twitter\13_9_6\dataset\en\input.txt";//絕對路徑或相對路徑都可以,這里是絕對路徑,寫入文件時演示相對路徑
Filefilename=newFile(pathname);//要讀取以上路徑的input。txt文件
InputStreamReaderreader=newInputStreamReader(
newFileInputStream(filename));//建立一個輸入流對象reader
BufferedReaderbr=newBufferedReader(reader);//建立一個對象,它把文件內容轉成計算機能讀懂的語言
Stringline="";
line=br.readLine();
while(line!=null){
line=br.readLine();//一次讀入一行數據
}

/*寫入Txt文件*/
Filewritename=newFile(".\result\en\output.txt");//相對路徑,如果沒有則要建立一個新的output。txt文件
writename.createNewFile();//創建新文件
BufferedWriterout=newBufferedWriter(newFileWriter(writename));
out.write("我會寫入文件啦 ");// 即為換行
out.flush();//把緩存區內容壓入文件
out.close();//最後記得關閉文件

}catch(Exceptione){
e.printStackTrace();
}
}
}

Ⅳ JAVA查詢資料庫結果怎麼用緩沖區輸出到txt中

結合PrintStream 可以的,如下例子
OutputStream outputStream = new FileOutputStream(」「);
PrintStream ps=new PrintStream(outputStream);
ps.printf("%1$s\t %2$15s\t %3$15s\r\n", "ID", "中文", "英文");

Ⅵ java 從資料庫取出數據並保存到本地文本中

先看資料庫表, 我裡面有46條記錄,其中有三條重復,我就拿其中一條emp_id 為"

DWR65030M" 做例子

裡面有兩條記錄 ,實現了

Ⅶ java 通過流將從資料庫中查出來的數據生成一個txt文件,如何實現數據 列像表格一樣對齊

用java 1.5以上PrintWriter或者PrintStream新加的printf方法吧,可以像c語言的printf格式化每個欄位的長度的
比如:
public static void main(String[] args) {
for (int i = 0; i < 20; i += 3) {
System.out.printf("i=%5d\n", i);
}
}

要是寫文件的話,就用PrintWriter套到Writer上,或者PrintStream套到OutputStream上,比如:
public static void main(String[] args) {
try{
FileOutputStream fos=new FileOutputStream("text.txt");
PrintStream ps=new PrintStream(fos);

for (int i = 0; i < 20; i += 3) {
ps.printf("i=%5d\n", i);
}
}catch(IOException e){
e.printStackTrace();
}
}

Ⅷ Java怎麼導出資料庫數據以,為分隔符到TXT文件

什麼意思不大清楚。如果是要把資料庫的數據保存到txt文件中,可以先讀取資料庫的信息,然後用文件的方式將數據按照指定的格式寫入到txt文件。

Ⅸ 如何通過java將查詢出的數據通過資料庫直接導出到txt文件

//查詢數據獲取resultSet
while(rs.next()){
//直接寫到文件
}

Ⅹ 如何用java實現mysql資料庫的導入導出

MySql導出資料庫的命令如下:
Sql代碼
mysqlmp -uusername -ppassword -hhost -Pport exportDatabaseName > exportPath
mysqlmp -uusername -ppassword -hhost -Pport exportDatabaseName > exportPath

利用Java調用命令窗口執行命令來進行MySql導入資料庫一般分三步走:
第一步:登錄Mysql資料庫,在登錄資料庫的時候也可以指定登錄到哪個資料庫,如果指定了則可以跳過第二步;
第二步:切換資料庫到需要導入的目標資料庫
第三步:利用命令開始導入

在進行導出的時候,需要注意命令語句的運行環境,如果已經將mysql安裝路徑下的bin加入到
系統的path變數中,那麼在導出的時候可以直接使用命令語句,否則,就需要在執行命令語句的
時候加上命令所在位置的路徑,即mysql安裝路徑想的bin下的mysqlmp命令。