Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

save DefaultStyledDocument in mysql db

Status
Not open for further replies.

Dijkstra30

Programmer
Oct 24, 2005
13
RO
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.
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...
 
A little warmup:

a) Is mysql really using backticks - not apostroph?
"UPDATE `test` SET `content` = ... "
"UPDATE 'test' SET 'content' = ... "

b) Is it recommendet to use the database as root?
It's discouraged for postgresql, oracle - I don't know mysql - just wonder.

On topic:
Use PreparedStatement instead of Statement.
It will handle masking of metacharacters, prevents sql-injection attacks and errors, and is more performant in most cases.


don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top