好的,我在向 mysql db 添加值的代码方面遇到了问题,有一个帖子(db 表,在 Eclipse 中创建的 java 表单)

好的,我在向 mysql db 添加值的代码方面遇到了问题,有一个帖子(db 表,在 Eclipse 中创建的 java 表单)

包装 sokic;

导入 java.awt.EventQueue;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 java.sql.Connection;导入 java.sql.DriverManager;导入 java.sql.ResultSet;导入 java.sql.SQLException;导入 java.util.logging.Level;导入 java.util.logging.Logger;

导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.JPanel;导入 javax.swing.JTextField;导入 javax.swing.border.EmptyBorder;

导入 com.mysql.jdbc.Statement;

公共类 Nastavnici 扩展了 JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tx_id_nastavnika;
private JTextField txt_ime;
private JTextField txt_prezime;
private JTextField txt_staz;
private JTextField txt_email;
private JTextField txt_telefon;
private JTextField txt_oblast;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Nastavnici frame = new Nastavnici();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
 public static void main1(String[] args) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/studije";
        String user = "rootr";
        String password = "root";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = (Statement) con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Nastavnici.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Nastavnici.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
 }
public Nastavnici() {
    setTitle("Nastavnici");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNewButton = new JButton("Unesi");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            NOW HOW ????
                }
            });
    btnNewButton.setBounds(12, 12, 131, 25);
    contentPane.add(btnNewButton);

    JLabel lblIdNastavnika = new JLabel("id nastavnika");
    lblIdNastavnika.setBounds(12, 49, 102, 15);
    contentPane.add(lblIdNastavnika);

    JLabel lblIme = new JLabel("ime");
    lblIme.setBounds(12, 76, 70, 15);
    contentPane.add(lblIme);

    JLabel lblPrezime = new JLabel("prezime");
    lblPrezime.setBounds(12, 103, 70, 15);
    contentPane.add(lblPrezime);

    JLabel lblStaz = new JLabel("staz");
    lblStaz.setBounds(12, 130, 70, 15);
    contentPane.add(lblStaz);

    JLabel lblEmail = new JLabel("email");
    lblEmail.setBounds(12, 157, 70, 15);
    contentPane.add(lblEmail);

    JLabel lblTelefon = new JLabel("telefon");
    lblTelefon.setBounds(12, 184, 70, 15);
    contentPane.add(lblTelefon);

    JLabel lblOblast = new JLabel("oblast");
    lblOblast.setBounds(12, 211, 70, 15);
    contentPane.add(lblOblast);

    tx_id_nastavnika = new JTextField();
    tx_id_nastavnika.setBounds(132, 47, 114, 19);
    contentPane.add(tx_id_nastavnika);
    tx_id_nastavnika.setColumns(10);

    txt_ime = new JTextField();
    txt_ime.setBounds(69, 74, 177, 19);
    contentPane.add(txt_ime);
    txt_ime.setColumns(10);

    txt_prezime = new JTextField();
    txt_prezime.setBounds(100, 101, 146, 19);
    contentPane.add(txt_prezime);
    txt_prezime.setColumns(10);

    txt_staz = new JTextField();
    txt_staz.setBounds(69, 128, 145, 19);
    contentPane.add(txt_staz);
    txt_staz.setColumns(10);

    txt_email = new JTextField();
    txt_email.setBounds(69, 155, 214, 19);
    contentPane.add(txt_email);
    txt_email.setColumns(10);

    txt_telefon = new JTextField();
    txt_telefon.setBounds(100, 182, 167, 19);
    contentPane.add(txt_telefon);
    txt_telefon.setColumns(10);

    txt_oblast = new JTextField();
    txt_oblast.setBounds(100, 209, 167, 19);
    contentPane.add(txt_oblast);
    txt_oblast.setColumns(10);
}

}

答案1

这绝对不是回答您问题的正确社区。说实话,我不确定我是否理解了……但无论如何,如果您想在此处插入数据库:

public void actionPerformed(ActionEvent e) {
            NOW HOW ????
                }
            });

您必须使 JFrame 可以访问数据库连接。然后创建准备好的语句并编写插入查询(参见此处的示例:例子

而且...它会很丑陋。考虑重构代码以创建负责与数据库通信的服务层。

相关内容