Ever stop to wonder what’s in a simple day like today? There’s no guarantee that today will bring about anything substantial. In fact, today could be a big flop on the ground and no one would notice. I don’t know how any of that tends to work out though. It’s life right? Oh life, what are you even on about? I don’t understand you at times. It would be nice to be able to grasp something, even if it’s out of thin air, to understand and realize what that’s all about. But alas I cannot do that. It is life after all, and we cannot really understand anything that comes our way. If I had the ability to realize my own potential, I think I could benefit from it all. But I don’t know how to do that. It’s a shame really, to want to be able to do something with this life and then to be stuck without any reason for it? Yeah, no bueno.
This database program is simple, it uses the sqlite jar for connecting to the SQLite database.
It can store a file's text in a database, update that text, and extract that text back out to it's original file name.
It's pretty quick and dirty. There might be better ways to handle some of it, but I just wanted to make sure it would do what I wanted it to do.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Sample {
private Connection conn = null;
public Sample() {
}
public void init() {
try {
String url = "jdbc:sqlite:sample.db";
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void saveData(String filename) {
try {
String fileContent = new String(Files.readAllBytes(Paths.get(filename)));
String sql = "insert into files(name, content) values(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, filename);
pstmt.setString(2, fileContent);
pstmt.executeUpdate();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateData(String filename) {
try {
String fileContent = new String(Files.readAllBytes(Paths.get(filename)));
String sql = "update files set content = ? where name = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fileContent);
pstmt.setString(2, filename);
pstmt.executeUpdate();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void exportData(String id) {
StringBuilder sb = new StringBuilder();
String sql = "select * from files where id = ?";
ResultSet rs = null;
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, id);
rs = pst.executeQuery();
while (rs.next()) {
Path path = Paths.get(rs.getString("name"));
Files.writeString(path, rs.getString("content"), StandardCharsets.UTF_8);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("ERROR: You must provide two arguments.");
System.err.println("insert or update");
System.err.println("filename to insert or update");
return;
}
Sample sample = new Sample();
sample.init();
if (args[0].equals("insert")) {
sample.saveData(args[1]);
} else if (args[0].equals("update")) {
sample.updateData(args[1]);
} else if (args[0].equals("export")) {
sample.exportData(args[1]);
}
}
}
Comments
Post a Comment