簡體   English   中英

如何插入 (int) 和 (date) 類型?

[英]How to insert (int) and (date) type?

我想將 ID(int)、title(varchar)、plot(varchar)、release_date(date)、target(varchar) 插入現有數據庫。

這是根本沒有顯示數據的代碼。

如何將日期類型值添加到連接的數據庫中?

else if (e.getSource() == this.insert2) {

    String n = this.IDField.getInt();
    String m = this.titleField.getText();
    String f = this.plotField.getText();
    String p = this.release_dateField.getText();
    String t = this.targetField.getText();

    String[] name = {"MOVIE_ID","title","plot","release_date","main_target"};
    this.dt = new DefaultTableModel(name, 0);
    /**try~catch*/
    try{
        stmt.executeUpdate("insert into DB2020_MOVIEINFO values('" + n + "', '" + m + "', '" + f + "', '" + p + "', '" + t + "')");

        ResultSet rset_mv = stmt.executeQuery("SELECT * FROM DB2020_MOVIEINFO");
        while (rset_mv.next()) {
            Object[] data = {rset_mv.getString(1), rset_mv.getString(2), rset_mv.getString(3), rset_mv.getString(4), rset_mv.getString(5)};
            dt.addRow(data);
        }

    } 
    catch(SQLException se) {
        se.printStackTrace();
    }

    this.jt = new JTable(dt);
    jt.getTableHeader().setBackground(Color.decode("#b76e79"));
    jt.setSelectionBackground(Color.decode("#f7f6f0"));
    this.jsp = new JScrollPane(jt);
    jt.setPreferredScrollableViewportSize(new Dimension(800,600));
    this.add(centerPanel,BorderLayout.CENTER);
    this.centerPanel.removeAll();
    this.centerPanel.updateUI();
    this.centerPanel.add(jsp);
    this.centerPanel.setVisible(true);
}

我在 Mureinik 的建議后應用的新代碼。 (我提取了日期部分)

 else if (e.getSource() == this.insert2) {


            Int id = this.IDField.getInt();
            String title = this.titleField.getText();
            String plot = this.plotField.getText();
            String target = this.targetField.getText();

            String[] name = {"MOVIE_ID","title","plot","main_target"};
            this.dt = new DefaultTableModel(name, 0);
            /**try~catch*/
            try (PreparedStatement ps = 
                     conn.prepareStatement("insert into DB2020_MOVIEINFO values(?, ?, ?, ?)") {
                    ps.setInt(1, id); // id should be an int
                    ps.setString(2, title);
                    ps.setString(3, plot);
                    ps.setString(4, target);
                }
            catch(SQLException se) {
                se.printStackTrace();
            }


            this.jt = new JTable(dt);
            jt.getTableHeader().setBackground(Color.decode("#b76e79"));
            jt.setSelectionBackground(Color.decode("#f7f6f0"));
            this.jsp = new JScrollPane(jt);
            jt.setPreferredScrollableViewportSize(new Dimension(800,600));
            this.add(centerPanel,BorderLayout.CENTER);
            this.centerPanel.removeAll();
            this.centerPanel.updateUI();
            this.centerPanel.add(jsp);
            this.centerPanel.setVisible(true);
        }

您可以使用PreparedStatement並讓 JDBC 驅動程序為您完成繁重的工作,而不是嘗試將這些數據類型強制為字符串。 作為副作用,這也將有助於保護您的應用程序免受 SQL 注入攻擊:

try (PreparedStatement ps = 
     conn.prepareStatement("insert into DB2020_MOVIEINFO values(?, ?, ?, ?, ?)) {
    ps.setInt(1, id); // id should be an int
    ps.setString(2, title);
    ps.setString(3, plot);
    ps.setDate(4, releaseDate); // releaseDate should be java.sql.Date
    ps.setString(5, target);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM