有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想在现有数据库中插入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的建议下我应用了新代码。(I摘录日期部分)

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

共 (1) 个答案

  1. # 1 楼答案

    您可以使用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);
    }