Note that there are some explanatory texts on larger screens.

plurals
  1. POJava/Sql GUI PreparedStatement
    primarykey
    data
    text
    <p>I cant see why i am getting errors, I believe i did everything correct. I only seem to to get errors when i try to get the text out the combobx. Can someone please hep me do this well. I know how to insert data from a text field to a data with microsoft access . But not when it comes to combo boxes. </p> <p>Theses errors</p> <p>java.lang.NullPointerException at delete_me1.connection(delete_me1.java:66) at delete_me1$2.actionPerformed(delete_me1.java:133) at java.awt.EventDispatchThread.run(Unknown Source) java.lang.NullPointerException at delete_me1.connection(delete_me1.java:90) at delete_me1$2.actionPerformed(delete_me1.java:133)</p> <p>Thanks :) </p> <pre><code>import java.awt.EventQueue; public class delete_me1 { private JFrame frame; public JComboBox&lt;?&gt; comboBox; String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim(); String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb"; Connection con = null; Statement st = null; ResultSet rs; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { delete_me1 window = new delete_me1(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public void connection() throws Exception { /* String sql = "INSERT INTO animal (first_name, last_name) VALUES(?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,animal); preparedStatemnt.setString(2,animalType); preparedStatemnt.executeUpdate();*/ try { String com2 = comboBox.getSelectedItem().toString(); String com = textField.getText(); String sql = "INSERT INTO client (first_name, last_name) VALUES (?,?)"; PreparedStatement preparedStatement = con.prepareStatement(sql); preparedStatement.setString(1,com); preparedStatement.setString(2,com2); preparedStatement.executeUpdate(); Class.forName(MSAccessDriver); String db = "jdbc:odbc:Food"; // db = database string stored // in the database con = DriverManager.getConnection(db); st = con.createStatement(); st.execute(sql); } catch (Exception ex) { ex.printStackTrace(); } finally { try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } JOptionPane.showMessageDialog(null, "Save Complete Successfully"); } } /** * Create the application. */ public delete_me1() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); final JComboBox comboBox = new JComboBox(); comboBox.setModel(new DefaultComboBoxModel(new String[] { "Dog", "Cat", "Cow", "Sheep" })); comboBox.setBounds(57, 54, 245, 46); frame.getContentPane().add(comboBox); JButton btnNewButton = new JButton("New button"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { connection(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btnNewButton.setBounds(57, 143, 89, 23); frame.getContentPane().add(btnNewButton); textField = new JTextField(); textField.setBounds(172, 144, 118, 20); frame.getContentPane().add(textField); textField.setColumns(10); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload