Skip to main content

Source Code for Simple Database Program

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

Popular posts from this blog

Suicidal Ideation

 Over the years I've had to deal with suicidal ideation. Those are thoughts of being dead, some more extreme than others. It causes issues for me a lot of the time. It's not an easy thing to talk about at all. Here's what it is: Suicidal ideation ( suicidal thoughts )  are thoughts or ideas centered around death or suicide . Experiencing suicidal ideation doesn’t mean you’re going to kill yourself, but it can be a warning sign.

Ever Wonder

 Ever just sit and wonder about the things to wonder about? There are so many things that you can enjoy and think about or wonder about. It's just possible that you would be able to do such a thing. I'm not certain how it would go for you, as I am only a person. I don't know how you feel. So here we are, just waiting for something amazing to happen. It doesn't happen all the time unfortunately. It can be annoying at best.

Multidimensional Arrays

Ah Multidimensional Arrays. Nothing too crazy in JavaScript. Just a little bit of this and that. So typical arrays are fun and easy. You create them like so: var colors = new Array(); colors[0] = "Red"; colors[1] = "Yellow"; colors[2] = "Blue"; Well that's fine and dandy, but what if you want to associate something with each of those? For example, what if you wanted to create a menu? You'll need at minimum a link name and a target. We could do something like this: var colors = new Array(); colors[0] = new Array(); colors[0][0] = "red.htm"; colors[0][1] = "Red"; colors[1] = new Array(); colors[1][0] = "yellow.htm"; colors[1][1] = "Yellow"; colors[2] = new Array(); colors[2][0] = "blue.htm"; colors[2][1] = "Blue"; function createColors() { document.write('<ul id="colors">'); for (var i = 0; i < colors.length; i++) { var link = colors[i][0]; ...