在serverImpl拿到数据库数据并转json格式且写入本地文件夹

package com.server.android.service.impl;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.ResultSetMetaData;
import com.server.android.DBUtil;
import com.server.android.service.FileJsonDao;


/**
 * @author 作者 YeYouWang:
 * @date 创建时间:2017年12月27日 上午10:55:50
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class FileJsonImpl implements FileJsonDao {


@Override
public List> json(String category, String author_name) {


String sql = "select title,author_name,uniquekey,date,category,url,thumbnail_pic_s from Articles where category=? and author_name=?;";


List> list = new ArrayList>();


DBUtil util = new DBUtil();
Connection conn = util.openConnection();


try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, category);
pstmt.setString(2, author_name);
ResultSetMetaData metaData = (ResultSetMetaData) pstmt.getMetaData();
ResultSet rs = pstmt.executeQuery();


JSONArray array = new JSONArray();


while (rs.next()) {
HashMap map = new HashMap();
map.put("title", rs.getString("title"));
map.put("author_name", rs.getString("author_name"));
map.put("uniquekey", rs.getString("uniquekey"));
map.put("date", rs.getString("date"));
map.put("category", rs.getString("category"));
map.put("url", rs.getString("url"));
map.put("thumbnail_pic_s", rs.getString("thumbnail_pic_s"));
list.add(map);


JSONObject jsonObject = new JSONObject();


for (int i = 1; i < map.size(); i++) {
String column = metaData.getColumnLabel(i);
String value = rs.getString(column);


jsonObject.put(column, value);
}


array.add(jsonObject);


BufferedWriter writer = null;


File file = new File("E:\\IO");
                                File file2 = null;
// 如果文件不存在则新建一个
//if (file.exists()) {


try {

file2 = file.createTempFile("jbx", ".json", file);
//file.createNewFile();
//file.createNewFile();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//}
// 写入


try {


writer = new BufferedWriter(new FileWriter(file2));
writer.write(array.toString());


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {


try {


if (writer != null) {
writer.close();
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


System.out.println("文件写入成功!");


}


System.out.println("转换JSON数据:");
System.out.println(array.toString());
array.toString();
conn.close();


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


}


/*
* public User File(String category, String author_name) {

* String sql =
* "select title,author_name,uniquekey,date,category,url,thumbnail_pic_s from Articles where category=? and author_name=?;"
* ; DBUtil util = new DBUtil(); Connection conn = util.openConnection(); try {
* PreparedStatement pstmt = conn.prepareStatement(sql);

* pstmt.setString(1, category); pstmt.setString(2, author_name);

* ResultSet rs = pstmt.executeQuery(); if (rs.next()) { User u = new User();
* u.setAuthor_name(rs.getString(2)); u.setUniquekey(rs.getString(3));
* u.setTitle(rs.getString(1)); u.setDate(rs.getString(4));
* u.setUrl(rs.getString(6)); u.setThumbnail_pic_s(rs.getString(7));
* u.setCategory(rs.getString(5)); return u; } } catch (SQLException e) {
* e.printStackTrace(); } finally { util.closeConn(conn); } return null; }
*/
}

你可能感兴趣的:(转)