Dijkstra30
Programmer
I want to save a DefaultStyledDocument in mysql db.
Some say that can be saved directly in a blob column, others transform it to ObjectOutputStream.
I managed to save something in the database both ways but i don't know if it is the right way because i can't read it.
For reading i've tried the following
and don't know further...
Some say that can be saved directly in a blob column, others transform it to ObjectOutputStream.
I managed to save something in the database both ways but i don't know if it is the right way because i can't read it.
Code:
DefaultStyledDocument m_doc = new DefaultStyleDocument();
// modify the document
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection "jdbc:mysql://localhost/mydb",
"root", "root");
if(!con.isClosed()) {
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(m_doc);
oout.flush();
oout.close();
System.out.println("OOT:" + oout.toString());
String request = "UPDATE `test` SET `content` = '"+ oout + "' WHERE `Id` = '1'";
// or m_doc instead of oout to save the object directly
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
int rs = stmt.executeUpdate(request);
con.close();
}
For reading i've tried the following
Code:
String request = "SELECT `content` FROM `test` WHERE `Id` = '1'";
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
java.sql.ResultSet rs = stmt.executeQuery(request);
Blob blob = rs.getBlob(0);
and don't know further...