當前位置:首頁 » 操作系統 » 資料庫導出txt文件

資料庫導出txt文件

發布時間: 2024-03-27 09:14:54

sql Server資料庫如何將多行查詢代碼執行結果導出到txt文件中

1、將上述SQL語句,保存到一個文本文件
s.sql

2、建立一個批處理文件runsql.bat,內容如下:
osql
-S資料庫名
-Usql用戶名
-Psql用戶密碼
-i
s.sql
-o
result.txt
(如果安裝的是
SQL2000,可將上面的osql,改為
isql)
3、運行runsql.bat後,檢查result.txt,這里就是SQL語句執行後的結果

Ⅱ 怎麼用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();
}
}
}
熱點內容
長虹安卓電視關閉網路在哪裡 發布:2025-05-10 14:37:04 瀏覽:142
ubuntuhttp伺服器的搭建 發布:2025-05-10 14:33:06 瀏覽:37
微信找回密碼申訴要多少時間 發布:2025-05-10 14:14:05 瀏覽:435
大眾寶來速騰選哪個配置 發布:2025-05-10 14:10:53 瀏覽:128
數字機頂盒密碼是多少 發布:2025-05-10 14:10:06 瀏覽:334
取消訪問網路需要密碼 發布:2025-05-10 13:44:20 瀏覽:64
shell編程運行 發布:2025-05-10 13:37:54 瀏覽:640
win7訪問xp共享需要密碼 發布:2025-05-10 13:34:10 瀏覽:344
飯團看書為什麼緩存不了小說 發布:2025-05-10 13:17:03 瀏覽:13
如何配置登錄源地址限制 發布:2025-05-10 13:12:52 瀏覽:591