使用JDBC连接 将多个JSON数据导出到一个文件夹
2017-10-22 13:09
267 查看
数据库:oracle:
jar包:ojdbc14.jar
json-lib-2.4-jdk1.5.jar
json-parser_fat.jar
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(); } }
相关文章推荐
- 使用Boost.PropertyTree处理XML、JSON和INI数据 - 做一个优雅的程序员 - 博客频道 - CSDN.NET
- [ACE程序员教程笔记]ACE_Connector使用一个连接多个线程发送数据
- [Sqlite]-->Java使用jdbc连接Sqlite数据库进行各种数据操作的详细过程
- Tiled2Unity —— 一个让Unity3D可以使用Tiled地图编辑器导出数据和素材的工具
- java JDBC 连接数据库查询数据与直接使用sql的疑问
- [Socket网络编程]由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
- 通过JDBC连接mysql数据库,得到一个list,包含的是每一条数据的HashMap
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- JAVA 简单使用JDBC实现连接数据
- 客户端使用Ajax访问struts2 , struts2如何返回一个json数据!json-lib与flexjson
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- 使用net.sf.json包生成一个pojo的json数据出现的问题
- JDBC连接数据的基本使用
- 使用JDBC连接MySQL数据库--典型案例分析(一)---员工数据读取与操作
- java使用jdbc连接数据库工具类和jdbc连接mysql数据示例
- oracle12c JSON数据 使用SQL语句实现多表左外连接 显示无效数据 分页查询
- Ajax使用Action中返回的Json数据的完整实例(二):action内将一个实体对象转化为json并返回
- 使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据
- 久违的json使用大法 (通常Ajax希望返回多个键值对,可以用将要返回的数据先封装到一个对象中,再将这个对象转为json格式)
- JDBC连接oracle 12c,数据表转JSON格式