Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How Do I insert my data from a swing Input GUI

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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: 64642
225
  • Mark post as helpful
  • send pies
  • 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: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I am grateful to you guys - Paul and Campbel - for the hints.

Thanks
Jonas
 
Campbell Ritchie
Marshal
Posts: 64642
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
[ August 18, 2008: Message edited by: Campbell Ritchie ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!