• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How Do I insert my data from a swing Input GUI

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All your inputs are in the form of text, so you have to convert them to something else first.

eg
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am grateful to you guys - Paul and Campbel - for the hints.

Thanks
Jonas
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
[ August 18, 2008: Message edited by: Campbell Ritchie ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic