-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSQLtoJSON_Java.java
More file actions
68 lines (51 loc) · 2.07 KB
/
SQLtoJSON_Java.java
File metadata and controls
68 lines (51 loc) · 2.07 KB
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
67
68
import java.sql.*;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.*;
public class SQLtoJSON_Java {
public static void main(String[] args) {
// CREATE JSON OBJECTS
JsonArray jsondata = new JsonArray();
JsonObject jo = new JsonObject();
try {
String currentDir = new File("").getAbsolutePath();
String database = currentDir + "/CLData.db";
// CONNECT TO DATABASE
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+database);
conn.setAutoCommit(false);
// QUERY DATABASE
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM cldata");
ResultSetMetaData rsmd = rs.getMetaData();
int rs_column_count = rsmd.getColumnCount();
// DATA ROWS
while (rs.next()) {
jo = new JsonObject();
for(int i=1; i <= rs_column_count; i++) {
jo.addProperty(rsmd.getColumnName(i), rs.getString(i));
}
jsondata.add(jo);
}
rs.close();
stmt.close();
conn.close();
// SAVE JSON
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJsonString = gson.toJson(jsondata);
FileWriter file = new FileWriter(currentDir + "/CLData_Java.json");
file.write(prettyJsonString);
file.flush();
file.close();
System.out.println("Successfully migrated SQL data to JSON!");
} catch (FileNotFoundException ffe) {
System.out.println(ffe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} catch (SQLException sqe) {
System.out.println(sqe.getMessage());
}
}
}