将数据库的内容导出成为.txt.文本的java代码

已经用java链接到数据库,并可以读出数据库里面的内容,需要一段可以把数据库内容导出成为.txt文本的代码

4个回答

package webtest;
import java.sql.*;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

public class mysqlchtxt {// JDBC 驱动名及数据库 URL

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://localhost:3306/gumysql";

// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "ggx";
static final String PASS = "123456";

public static void wf(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
out.write(conent+"\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName("com.mysql.jdbc.Driver");

    // 打开链接
    System.out.println("连接数据库...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);

    // 执行查询
    System.out.println(" 实例化Statement对象...");
    stmt = conn.createStatement();
    String sql;
    sql = "SELECT id, name, url FROM websites";
    ResultSet rs = stmt.executeQuery(sql);

    // 展开结果集数据库
    while(rs.next()){
        // 通过字段检索
        int id  = rs.getInt("id");
        String name = rs.getString("name");
        String url = rs.getString("url");

        // 输出数据
                    String fn = "c://1.txt"; //文件名
        System.out.print("ID: " + id);
                    wf(fn, "ID: " + id); //你要写什么,这里就加上什么
        System.out.print(", 站点名称: " + name);
                    wf(fn, "站点名称: " + name);
        System.out.print(", 站点 URL: " + url);
                    wf(fn, "站点 URL: " + url);
        System.out.print("\n");
    }
    // 完成后关闭
    rs.close();
    stmt.close();
    conn.close();
}catch(SQLException se){
    // 处理 JDBC 错误
    se.printStackTrace();
}catch(Exception e){
    // 处理 Class.forName 错误
    e.printStackTrace();
}finally{
    // 关闭资源
    try{
        if(stmt!=null) stmt.close();
    }catch(SQLException se2){
    }// 什么都不做
    try{
        if(conn!=null) conn.close();
    }catch(SQLException se){
        se.printStackTrace();
    }
}
System.out.println("Goodbye!");

}

}

public static void main(String[] args) {
// TODO Auto-generated method stub
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://";
String user = "";
String password = "";

    try {
        Class.forName(driver);
        Connection conn = (Connection) DriverManager.getConnection(url, user, password);
        Statement statement = (Statement) conn.createStatement();
        String sql = "select * from zs_lvrobot";
        ResultSet rs = statement.executeQuery(sql);
        StringBuilder sds = new StringBuilder();
        while (rs.next()) {
                        sds.append(rs.getString("id"));
        }
        rs.close();
        conn.close();
        WriteStringToFile2(sds.toString());
    } catch (ClassNotFoundException e) {
        System.out.println("Sorry,can't find the Driver!");
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void WriteStringToFile2(String str) {
    try {
        String filePath = "d:\\123.txt";
        FileWriter fw = new FileWriter(filePath, true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(str);
        bw.close();
        fw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

package webtest;
import java.sql.*;
public class mysqlchtxt {// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

static final String DB_URL = "jdbc:mysql://localhost:3306/gumysql";

// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "ggx";
static final String PASS = "123456";

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{
        // 注册 JDBC 驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 打开链接
        System.out.println("连接数据库...");
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        // 执行查询
        System.out.println(" 实例化Statement对象...");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT id, name, url FROM websites";
        ResultSet rs = stmt.executeQuery(sql);

        // 展开结果集数据库
        while(rs.next()){
            // 通过字段检索
            int id  = rs.getInt("id");
            String name = rs.getString("name");
            String url = rs.getString("url");

            // 输出数据
            System.out.print("ID: " + id);
            System.out.print(", 站点名称: " + name);
            System.out.print(", 站点 URL: " + url);
            System.out.print("\n");
        }
        // 完成后关闭
        rs.close();
        stmt.close();
        conn.close();
    }catch(SQLException se){
        // 处理 JDBC 错误
        se.printStackTrace();
    }catch(Exception e){
        // 处理 Class.forName 错误
        e.printStackTrace();
    }finally{
        // 关闭资源
        try{
            if(stmt!=null) stmt.close();
        }catch(SQLException se2){
        }// 什么都不做
        try{
            if(conn!=null) conn.close();
        }catch(SQLException se){
            se.printStackTrace();
        }
    }
    System.out.println("Goodbye!");
}

} 

链接的代码如上,现在需要导出成为.txt的代码

// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}

立即提问
相关内容推荐