简体   繁体   English

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

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

I'd like to insert ID(int), title(varchar), plot(varchar), release_date(date), target(varchar) into exsiting database.我想将 ID(int)、title(varchar)、plot(varchar)、release_date(date)、target(varchar) 插入现有数据库。

Here is the code which showed no data at all.这是根本没有显示数据的代码。

How can I add a date type value into a connected database?如何将日期类型值添加到连接的数据库中?

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);
}

new code I applied after Mureinik's advise.我在 Mureinik 的建议后应用的新代码。 (I extracted date-part) (我提取了日期部分)

 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);
        }

Instead of trying to force these datatypes to strings, you could use a PreparedStatement and let the JDBC driver do the heavy lifting for you.您可以使用PreparedStatement并让 JDBC 驱动程序为您完成繁重的工作,而不是尝试将这些数据类型强制为字符串。 As a side effect, this will also help protect you application from SQL Injection attacks:作为副作用,这也将有助于保护您的应用程序免受 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