thingamablog-api/ExportTool.java

68 lines
2.6 KiB
Java

import java.sql.*;
import java.util.*;
import org.hsqldb.jdbcDriver;
public class ExportTool {
public static void main(String[] args) {
Connection conn = null;
try {
// Load the HSQLDB 1.8 driver
Class.forName("org.hsqldb.jdbcDriver");
// Connect to the database
// Note: path is relative to where we run it, or absolute
String url = "jdbc:hsqldb:file:/home/paulh/.openclaw/workspace/docs/pauls-blogs/Paul/data/database";
conn = DriverManager.getConnection(url, "SA", "");
// Query all posts
Statement stmt = conn.createStatement();
// Based on database.script, the table is ENTRY_TABLE_1096292361887
// Columns: ID, TIMESTAMP, TITLE, CATEGORIES, ENTRY, DRAFT, MODIFIED, AUTHOR
ResultSet rs = stmt.executeQuery("SELECT * FROM ENTRY_TABLE_1096292361887");
System.out.println("[");
boolean first = true;
while (rs.next()) {
if (!first) {
System.out.println(",");
}
first = false;
int id = rs.getInt("ID");
String title = escape(rs.getString("TITLE"));
Timestamp ts = rs.getTimestamp("TIMESTAMP");
String date = (ts != null) ? ts.toString() : "";
String content = escape(rs.getString("ENTRY"));
String categories = escape(rs.getString("CATEGORIES"));
String author = escape(rs.getString("AUTHOR"));
System.out.print(" {");
System.out.print("\"id\": " + id + ", ");
System.out.print("\"title\": \"" + title + "\", ");
System.out.print("\"date\": \"" + date + "\", ");
System.out.print("\"content\": \"" + content + "\", ");
System.out.print("\"categories\": \"" + categories + "\", ");
System.out.print("\"author\": \"" + author + "\"");
System.out.print("}");
}
System.out.println("\n]");
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (conn != null) conn.close(); } catch (SQLException e) {}
}
}
// Simple JSON escaping
private static String escape(String s) {
if (s == null) return "";
return s.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
}
}