JDBC连接ORCALE并把查询结果保存到txt文本中

// main 方法
//test.java
public class test {

/**
 * @param args
 */


public static void main(String[] args) {


    String sql = " SELECT PK_ID FROM PMS_MONTH_PLAN_T ";
    String url  = "G://Json.txt";      //写文件地址 不存在会自动创建
    String Json = SqlUtil.sqlSelect(sql);
    System.out.println(Json);
    FileUtil.FlieUtil(url, Json);
}

}

//SqlUtil.java

import java.sql.*;

/*
* jbdc 连接
*/
public class SqlUtil {
// 创建静态全局变量
private static Connection conn;

private static Connection getConnection() {
    Connection con = null;
    String driver = "oracle.jdbc.OracleDriver";
    String url = "jdbc:oracle:thin:@XXXXXX:1521:wxmtdb";     //数据库地址
    String user = "v4_pms";                              //用户名   
    String password = "v4_pms";                          //用户密码
    try {
        Class.forName(driver);
        System.out.println("驱动加载成功");
        con = DriverManager.getConnection(url, user, password);


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return con;
}


public static String sqlSelect(String sql) {
    conn = getConnection();
    String result = null;
    PreparedStatement pstm;
    ResultSet rs = null;
    try {
        pstm = conn.prepareStatement(sql);
        rs = pstm.executeQuery();
        StringBuffer sb = new StringBuffer();
        while(rs.next()){
            String pk_id = rs.getString("PK_ID");
            sb.append("["+pk_id+"],");
        }
        result = "[" + sb.substring(0, sb.length()-1) + "]";
        close(pstm,rs,conn);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return result;
}

private static void  close(PreparedStatement pstm ,ResultSet rs,Connection conn   ) {
    try {
        System.out.println("关闭流");
        pstm.close();
        rs.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

//FileUtil.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtil {
/*
* 文件写入 并读取文件内容
*/
public static void FlieUtil(String url , String content) {

    File file = new File(url);
    try {
        FileOutputStream out = new FileOutputStream(file);

        byte  buy[] = content.getBytes();
        out.write(buy);
        out.close();
    }  catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        FileInputStream in = new FileInputStream(file);

        byte buy2[] = new byte[1024];
        int len= in.read(buy2);

        System.out.println("文件中内容是" + new String(buy2,0,len) );
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值