Patrick
Patrick

Reputation: 1

difficult time inserting data with textfield( jdbc swing)

My insert into is giving me issues.

Im pretty sure the Jtextfield is the problem.

If anyone can give me a nudge in the right direction I would greatly appreciate it.

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  

    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");

        String url = "jdbc:oracle:thin:@//localhost:1521/xe";

        Connection conn = DriverManager.getConnection(url, "system", "oracle");

        conn.setAutoCommit(false);
        String query1 = "INSERT INTO WIN(NAME) VALUES('"+nameTextField.getSelectedText()+"')";


      Statement stm = conn.createStatement();
      stm.execute(query1);

        JOptionPane.showMessageDialog(null, "Record Added Succesfully.", "Record Added",
                JOptionPane.INFORMATION_MESSAGE);

    } catch (Exception e) {
        System.out.println(e);

    }


}                      

Upvotes: 0

Views: 119

Answers (1)

camickr
camickr

Reputation: 324118

nameTextField.getSelectedText()

I'm guessing that should be

nameTextField.getText()

Also, use a PreparedStatement for the SQL. You will less likely to make SQL syntax errors. Check out: geting data from multiple table when selecting a row then clicking a button for a basic example.

Im pretty sure the Jtextfield is the problem.

Well, learn how to do some basic debugging instead of guessing.

String query1 = "INSERT INTO WIN(NAME) VALUES('"+nameTextField.getSelectedText()+"')";

The above code can be written like:

String name = textField.getSelectedText();
// now you can verify the value of the name
System.out.println(name);

String query1 = "INSERT INTO WIN(NAME) VALUES('"+ name +"')";
// verify SQL syntax
System.out.println( query1);

Of course my first suggestion to use the PreparedStatement is easier, but learn how to display the value of variables when you do debugging.

Upvotes: 2

Related Questions