How can I save the content from a jtextpane in derby? I am writing a software which has a jtetxpane and derby db , when i save the text as String, at the moment of read it, it lose all fonts styles like bold or italic. i save the string whit a preparedStatemend.so how can i save the text in derby? i am thinking in a blob, so how can save a blob from the jtextpane and read it? or how can i save it? how can i fix it? sorry if my english is bad.

Insert:

        try {
        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        String dbName = "bdprueba";
        String dbParam = "create=true"; //la base de datos se creará si no existe todavía
        String connectionURL = "jdbc:derby:" + dbName + ";" + dbParam;
        String sql="insert into persona (texto) values(?)";

        Connection conn = DriverManager.getConnection(connectionURL);
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, texto.getText());
        st.executeUpdate();
        conn.close();
    } catch (SQLException ex) {
        Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadLocationException ex) {
        Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
    }

Select:

    try {
        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        String dbName = "bdprueba";
        String dbParam = "create=true"; 
        String connectionURL = "jdbc:derby:" + dbName +"" ;
        Class.forName(driver);
        Connection conn =  DriverManager.getConnection(connectionURL); 
        Statement st = conn.createStatement();
        ResultSet rs =  st.executeQuery("select texto from persona");
        while(rs.next())
        {      
            texto.setText(rs.getString("Texto"));            
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
    }

Grasias a ambos

thank you both

有帮助吗?

解决方案

Could you save the string then read it just after saving and compare both strings? If they are the same then problem is not DB part.

I would check editor kit of the destination JtextPane. It must be the same as in the source JTextPane. If strings are different then it's DB problem.

Look at the http://www.java2s.com/Code/Java/Database-SQL-JDBC/Saveimagetoderbydatabase.htm example. You can store your text (bytes of the string) rather than image.

其他提示

Try working with the document instead of the getText()

jtextpane.getDocument()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top