File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Do I insert my data from a swing Input GUI

 
jonas okwara
Ranch Hand
Posts: 58
  • 0
  • 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());
}
}
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • 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
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • 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
  • 0
  • 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
Sheriff
Pie
Posts: 47232
52
  • 0
  • 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