java读取数据库的内容_用java将数据库表中内容读取出来并存入txt中

/*

* DesktopApplication1.java

*/

package desktopapplication1;

import org.jdesktop.application.Application;

import org.jdesktop.application.SingleFrameApplication;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.*;

import java.sql.*;

import java.util.*;

/**

* The main class of the application.

*/

public class DesktopApplication1 extends SingleFrameApplication {

/**

* At startup create and show the main frame of the application.

*/

@Override protected void startup() {

show(new DesktopApplication1View(this));

}

/**

* This method is to initialize the specified window by injecting resources.

* Windows shown in our application come fully initialized from the GUI

* builder, so this additional configuration is not needed.

*/

@Override protected void configureWindow(java.awt.Window root) {

}

/**

* A convenient static getter for the application instance.

* @return the instance of DesktopApplication1

*/

public static DesktopApplication1 getApplication() {

return Application.getInstance(DesktopApplication1.class);

}

/**

* Main method launching the application.

*/

public static void main(String[] args) {

launch(DesktopApplication1.class, args);

FileWriter fw=null;

Connection con=null;

PreparedStatement pstm=null;

ResultSet result = null;

//     String line = null;

try {

fw=new FileWriter("E:\\test\\testforjava\\out.txt");

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

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testforjava","root","root");

pstm = con.prepareStatement("select * from whp");

result = pstm.executeQuery();

while(result.next()){

String id=result.getString(1);

String name=result.getString(2);

fw.write(id);

//   fw.flush();

fw.write(name);

fw.write("\r\n");

}

}catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}  catch(IOException e) {

}finally{

if(fw!=null)

try{

fw.close();

}catch(IOException e){

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值