您的位置:首页 > Web前端 > JavaScript

使用JDBC连接 将多个JSON数据导出到一个文件夹

2017-10-22 13:09 267 查看
数据库:oracle:

jar包:ojdbc14.jar

json-lib-2.4-jdk1.5.jar

json-parser_fat.jar

package exprot;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class export {

//数据库连接
public static Connection getConnection() throws SQLException,ClassNotFoundException
{
Connection con = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
System.out.println("开始尝试连接数据库!");
String url = "jdbc:oracle:" + "thin:@192.168.1.95:1521:QTWZ";// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
String user = "MIS_SYS";// 用户名,系统默认的账户名
String password = "MIS_SYS";// 你安装时选设置的密码
con = DriverManager.getConnection(url, user, password);// 获取连接
System.out.println("连接成功!");
return con;
}catch (Exception e)
{
e.printStackTrace();
}

return null;
}

//导出项目物资
public void saveprojectwz(){
JSONArray jsonArray = new JSONArray();
Connection con = null;
ResultSet result = null;// 创建一个结果集对象
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
String sql = "select * from MIS_QTWZ.PROJECTWZ";// 预编译语句,“?”代表参数
try {
pre = getConnection().prepareStatement(sql);// 实例化预编译语句
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
System.out.println(result);
while (result.next()){
JSONObject json = new JSONObject();
json.put("rid", result.getString("RID"));
if(result.getString("PROJECT") == null){
json.put("project", "0");
}else{
json.put("project", result.getString("PROJECT"));
}

json.put("name", result.getString("NAME"));
json.put("type", result.getString("TYPE"));
if(result.getString("NUM") == null){
json.put("num", "0");
}else{
json.put("num", result.getString("NUM"));
}

if(result.getString("NUMLY") == null){
json.put("numly", "0");
}else{
json.put("numly", result.getString("NUMLY"));
}

if(result.getString("NUMYY") == null){
json.put("numyy", "0");
}else{
json.put("numyy", result.getString("NUMYY"));
}
json.put("unit", result.getString("UNIT"));
if(result.getString("NUMTL") == null){
json.put("numtl", "0");
}else{
json.put("numtl", result.getString("NUMTL"));
}
jsonArray.add(json);
}

} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
savefile( "f://QTWZ_WEB//projectwz.json", jsonArray);
}

//导出项目表
public void saveproject(){
JSONArray jsonArray = new JSONArray();
Connection con = null;
ResultSet result = null;// 创建一个结果集对象
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
String sql = "select * from MIS_QTWZ.PROJECT";// 预编译语句,“?”代表参数
try {
pre = getConnection().prepareStatement(sql);// 实例化预编译语句
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
System.out.println(result);
while (result.next()){
JSONObject json = new JSONObject();
json.put("rid", result.getString("RID"));
json.put("name", result.getString("NAME"));
json.put("code", result.getString("CODE"));
if(result.getString("BEGINTIME") == null){
json.put("begintime", "0");
}else{
json.put("begintime", result.getString("BEGINTIME").substring(0,10));
}
if(result.getString("ENDTIME") == null){
json.put("endtime", "0");
}else{
json.put("endtime", result.getString("ENDTIME").substring(0,10));
}
json.put("fzoprt",result.getString("FZOPRT"));
json.put("phone", result.getString("PHONE"));
json.put("remark", result.getString("REMARK"));
if(result.getString("FLAG") == null){
json.put("flag", "0");
}else{
json.put("flag", result.getString("FLAG"));
}
jsonArray.add(json);

}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
System.out.println(jsonArray);
savefile( "f://QTWZ_WEB//project.json", jsonArray);

}

//获取主目录
public String mkdir() throws IOException{
String path = "f://QTWZ_WEB";
File file =new File(path);
//if file doesnt exists, then create it
if(file.exists()){
String[] content = file.list();//取得当前目录下所有文件和文件夹
for(String name : content){
File temp = new File("f://QTWZ_WEB", name);

if(temp.exists()){
temp.delete();
System.out.println("删除成功"+ name);
}
}
System.out.println("目录以清空");
}else{
file.mkdir();
System.out.println("创建目录成功");
}
return path;
}

//导出json文件
public static void savefile(String path,JSONArray jsonArray){
try{

File file =new File(path);
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}

//true = append file
FileWriter fileWritter = new FileWriter(file);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(jsonArray.toString());
bufferWritter.close();
System.out.println("Done");

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

public static void main(String[] args) {
try {
new export().mkdir();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
export exp = new export();
exp.saveproject();
exp.saveprojectwz();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐