How to insert into database with java form controls?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 10878 Views - Last Post: 14 March 2011 - 08:19 AM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

How to insert into database with java form controls?

Posted 12 March 2011 - 08:50 PM

Hello,

I am trying to create a form where the program will take the values from the fields on the form and input them into a database. I know how to do this in the Visual Studios environment but I am unsure of how to do this in Java.

Connection conn = getConnection();
         Statement stmt = conn.createStatement();

        stmt.executeUpdate("create table Customers (Customerid int, FirstName CHAR(15), LastName CHAR(15), MI CHAR(1), Address CHAR(15), Phone CHAR(15), City CHAR(15), State CHAR(15), Zip CHAR(10));");



I am unsure of how to get each separate text field to correspond with each value to input into the database. I've looked into the netbeans tutorials however they only walk you through how to do that on web forms. Would this be the same?

Any information on this would be great! :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to insert into database with java form controls?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: How to insert into database with java form controls?

Posted 12 March 2011 - 09:52 PM

NetBeans is just a fancy editor. Just make sure to stay away from the visual GUI Builder as it produces horrid, unmaintainable code.

Should be as simple as just getting the text from your JTextFields, and plugging them into your queries (after sanitizing your inputs first, of course).
String col1 = col1Field.getText();
col1 = sanitize(col1); //you define sanitize(String)

String query = "INSERT INTO table(col1) VALUES('" + col1 + "');";


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to insert into database with java form controls?

Posted 12 March 2011 - 10:10 PM

Your GUI stuff and your database access should be as separated as possible.

This is quite difficult to achieve with VB because you start by designing the GUI and then plug the modified code in place.

Here you need a kind of controller that push and pull objects to your GUI that displays it. This program can also call a DB class that store/retrive objects built in the GUI to the DB.
Was This Post Helpful? 1
  • +
  • -

#4 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 04:35 PM

Hello,

I am having a syntax error with my code, what is the problem here?

 sql =   ("INSERT INTO Customers (id, firstName, lastName, mi, address, phone, city, state, zip")
                    + "VALUES('"+id+"','"+firstName+"','"+lastName+"','"+mi+"','"+address+"','"+phone+"','"+city+"','"+state+"','"+zip+"')";
                
            Statement st = con.createStatement();
            rows = st.executeUpdate(sql);
            st.close();



Is it because my id and zip fields are number instead of text? If so, can I just convert them to text or is there a special syntax for those inputs.

Thanks for your replies.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 06:03 PM

Please post your specific error message from your compiler.
Was This Post Helpful? 0
  • +
  • -

#6 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 07:29 PM

View Postmacosxnerd101, on 13 March 2011 - 06:03 PM, said:

Please post your specific error message from your compiler.


Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 07:47 PM

I guess we'll have to match/compare your database columns type and your datatype in the Java program itself
Was This Post Helpful? 0
  • +
  • -

#8 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 08:12 PM

Is the syntax looking OK? I made all text data types in my database text just to see if that was a problem and I'm still getting the issue.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 08:31 PM

View PostCriel, on 13 March 2011 - 10:12 PM, said:

Is the syntax looking OK?

Depends of the datatype of your variables (inside the Java program)
Was This Post Helpful? 0
  • +
  • -

#10 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 08:39 PM

This is how I declared my variables:

String id = idTextField.getText();
        String firstName = firstNameTextField.getText();
        String lastName = lastNameTextField.getText();
        String mi = miTextField.getText();
        String address = adressTextField.getText();
        String phone = phoneTextField.getText();
        String city = phoneTextField.getText();
        String state = stateTextField.getText();
        String zip = zipTextField.getText();



Did I not declare them the right data type?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 08:50 PM

View PostCriel, on 13 March 2011 - 10:39 PM, said:

This is how I declared my variables:

String id = idTextField.getText();
        String firstName = firstNameTextField.getText();
        String lastName = lastNameTextField.getText();
        String mi = miTextField.getText();
        String address = adressTextField.getText();
        String phone = phoneTextField.getText();
        String city = phoneTextField.getText();
        String state = stateTextField.getText();
        String zip = zipTextField.getText();


And the column definitions in your database table ?

I can't beleive how much we have to beg for information to fix your problem !!!
Did I not declare them the right data type?


Sorry I lost my temper
You posted you table definition
This one contains int
all you put in are Strings :)
Was This Post Helpful? 0
  • +
  • -

#12 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 09:01 PM

Not trying to get you frustrated. Just trying to keep up. Still trying to learn this. :)

Could you clarify what you meant by what contains an int?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 09:16 PM

Look at your table definition. Customerid is an int. You cannot insert a String into an int field.
create table Customers (Customerid int


Was This Post Helpful? 0
  • +
  • -

#14 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 11-March 11

Re: How to insert into database with java form controls?

Posted 13 March 2011 - 11:16 PM

View Postmacosxnerd101, on 13 March 2011 - 09:16 PM, said:

Look at your table definition. Customerid is an int. You cannot insert a String into an int field.
create table Customers (Customerid int



I have changed the customerID data type from an int to a string and it's still giving me the same error. Any ideas?

Thanks for the help so far, guys. :)

This post has been edited by Criel: 13 March 2011 - 11:22 PM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How to insert into database with java form controls?

Posted 14 March 2011 - 05:02 AM

Ack, no:
String query = "INSERT INTO table(col1) VALUES('" + col1 + "');";



Prepare the statment, bind the vars, do it right the first time:
PreparedStatement prep = conn.prepareStatement("INSERT INTO table(col1) VALUES(?);");
prep.setString(1, col1Field.getText());
prep.execute();



For "int", what database? Some use integer. Often integer is just an alias for number.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2