将用户首选项保存到数据库并检索它

时间:2014-01-01 12:32:09

标签: java database swing

我正在开发一个用于文本编辑的java应用程序,我面临的问题是:

我有JTextArea并且用户更改了一些设置,例如更改文本区域中文本的字体或颜色。所以我想将这些设置保存到数据库中,并在再次打开文件时检索它。

是另一种方式!如何知道用户更改了设置以及如何将其作为字符串或确切地保存到数据库中!它与Java中的属性库有关吗?

我正在使用Microsoft访问和数据库,这里是我的应用程序示例。

package texteditor;


public class TextEditor extends JFrame implements ActionListener, ItemListener {


    String s1;
    File ff;
    final static String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    static Statement stmt = null;
    static Connection con = null;
    static ResultSet rs = null;
    static String filename = "C:\\Users\Seth\\Desktop\\Settings.accdb";
    static String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + filename;



    @Override
    public void actionPerformed(ActionEvent e) {



        if (e.getSource() == Open) {

            JFileChooser f = new JFileChooser();
            f.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int x = f.showOpenDialog(null);

            if (x != JFileChooser.CANCEL_OPTION) {
                try {

                    ff = f.getSelectedFile();
                    Scanner s = new Scanner(ff);
                    while (s.hasNext()) {
                        String name = s.nextLine();
                        Text1.append(name + "\n");

                    }
                    String y = ff.getName();
                    setTitle(y);

                    s.close();

                } catch (FileNotFoundException eee) {
                    JOptionPane.showMessageDialog(rootPane, "File not found ! \n" + eee.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
            }




            try {
                try {
                    Class.forName(DRIVER);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
                }
                con = DriverManager.getConnection(url, "", "");
                stmt = con.createStatement();
                rs = stmt.executeQuery("select * from Pref");
                while (rs.next()) {
                    s1 = rs.getString("FileName");


                    if (s1.contentEquals(ff.getName())) {

                        System.out.println("it is exists");


                    } else {


                    }
                }

            } catch (SQLException ex) {
                Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
            }





        }

        if (e.getSource() == Save) {

            JFileChooser g = new JFileChooser();
            g.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int y = g.showSaveDialog(null);

            if (y != JFileChooser.CANCEL_OPTION) {
                try {

                    File t = g.getSelectedFile();
                    String name = t.getCanonicalPath();
                    if (!name.endsWith(".txt")) {
                        t = new File(name + ".txt");
                    }

                    PrintWriter w = new PrintWriter(new FileOutputStream(t, true));
                    StringTokenizer st = new StringTokenizer(Text1.getText(), "\n");
                    while (st.hasMoreTokens()) {
                        w.println(st.nextToken());

                    }
                    String r = t.getName();
                    setTitle(r);

                    w.close();

                } catch (FileNotFoundException ee) {
                    JOptionPane.showMessageDialog(rootPane, "File not found ! \n" + ee.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }

            }


        }

    }



    public static void main(String[] args) {
        TextEditor t1 = new TextEditor();
        t1.show();

        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            con = DriverManager.getConnection(url, " ", " ");
            System.out.println("Connection Established Succesfully");


        } catch (SQLException ex) {
            Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (!(stmt == null)) {
                try {
                    con.close();
                    stmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

将首选项存储在某个可序列化对象(例如POJO)中。序列化对象(无论如何你喜欢)并将其存储在单个DB字段(例如LONGBINARY类型)中。相反,加载数据库字段,反序列化您的对象,然后再次使用您的对象。

当应用程序正在运行时,您可以跟踪用户是否更改了至少一个偏好设置。如果是这样 - 将对象保存到数据库,例如当用户退出应用程序时(当应用程序关闭时)。

好处是您可以将该DB字段与用户帐户相关联。 这样,任何用户都可以拥有自己的一组首选项,这些首选项可以很好地保存在数据库中,并且可以随时从那里加载(例如,当应用程序启动时或用户登录时)。

答案 1 :(得分:1)

我认为数据库应该使用扩展类java.util.prefs.Preferences的类。这更容易存储在文件中。设置更改时,请通知侦听器。

相关问题