1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Fre{
public static final String driver = "com.mysql.jdbc.Driver";
public static final String uri = "jdbc:mysql://127.0.0.1:3306/solo";
public static final String user = "root";
public static final String passcode = "123456";
final String sqlScript = "select * from b3_solo_article";
Connection connection= null;
public ResultSet StartCollection() throws SQLException {
try{
Class.forName(driver);
connection = DriverManager.getConnection(uri,user,passcode);
if(!connection.isClosed())
System.out.println("connection successfully");
Statement statement = connection.createStatement();
ResultSet replyString = statement.executeQuery(sqlScript);
return replyString;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static String toTheJson(List<Map> list){
return list.toString().replace("{", "{\"").replace("}", "\"}").replace("=","\":\"").replace(", ", "\",\"").replace("}\",\"{", "},{").replace("{", "{\"").replace("}", "\"}").replace("=","\":\"").replace(", ", "\",\"").replace("}\",\"{", "},{").replace("\"\"","\"");
}
public void close(){
try{
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void Solomon() throws SQLException, IOException {
Map<String,Object> outer = new HashMap<String, Object>();
List<Map> list = new ArrayList<Map>();
Fre solo = new Fre();
ResultSet so = solo.StartCollection();
while(so.next()){
Map<String,Object> map = new HashMap<String, Object>();
map.put("User_ID",so.getString(1));
map.put("User_Name",so.getString(2));
map.put("User_Mod",so.getString(3));
map.put("User_IDE",so.getString(4));
list.add(map);
}
String Solo = toJson(list);
System.out.println(Solo);
FileOutputStream fos = new FileOutputStream("solo.json",true);
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
osw.write(Solo);
System.out.println("Write Over");
osw.close();
}
}
}
|