aspose file tools*
The moose likes JDBC and the fly likes How Do  I  insert  my data from a swing Input GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How Do  I  insert  my data from a swing Input GUI" Watch "How Do  I  insert  my data from a swing Input GUI" New topic
Author

How Do I insert my data from a swing Input GUI

jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
Please I really would appreciate any hints on what I am doing wrong. I want to insert data into a mysql database through a swing GUI, but each time I end up inserting string objects instead of the actual data. I really frustrated at this point. My code snippets are below:

if( isNameCorrect( nameInput) & isAgeCorrect(ageInput) & isStateCorrect(stateInput) & isLocationCorrect(locationInput)& isDateCorrect(dateInput) ){

InsertData(nameField.getText(), ageField.getText(), stateField.getText(), locationField.getText(), dateField.getText());
== =======================================================================

The Insert Method:
=========================================================================

public void InsertData(String nameInput, String ageInput, String stateInput, String locationInput, String dateInput){

Connection ThisSqlConnection = mysqlConnection();
try {

Statement statement = ThisSqlConnection.createStatement();

String sqlStatement = ("insert Into Test Values('nameInput', "+" 'ageInput', "+" 'stateInput',"+" 'locationInput',"+" 'dateInput')");

statement.executeUpdate(sqlStatement);
} catch (SQLException ex) {
System.out.println("Exception:" + ex.getMessage());
}
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
All your inputs are in the form of text, so you have to convert them to something else first.

eg
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And yes, you are inserting string constants into the database. (String constants that happen to be the same as the names of the variables which you really wanted to insert.)

Let me recommend to you the JDBC tutorial. In particular read the part about PreparedStatements, which allow you to insert data into parameterized SQL strings.
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
I am grateful to you guys - Paul and Campbel - for the hints.

Thanks
Jonas
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
You're welcome
[ August 18, 2008: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Do I insert my data from a swing Input GUI