Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.Date problem

 
oghenekaro EFEKODO
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need help urgently,i have been trying to input a date value via JTextField which is the date of transaction and i have been experiencing alot of problem.please somebody show how to insert date into my database from a text field.below are the methods i have tried and didn't work.thanks in advance.

1. i used this and recieve exception message
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbcdbc:sparkletransaction","","");


java.sql.Date date =java.sql.Date.valueOf(dateF.getText());

String insert="INSERT INTO personaltransaction(date,name,phone,addres,deposit,balance) VALUES(?,?,?,?,?,?)";
//insert=insert+;
stat=con.prepareStatement(insert);

stat.setDate(1,date);
stat.setString(2,fullName);
stat.setString(3,phoneF.getText());
stat.setString(4,addressF.getText());
stat.setString(5,ss);
stat.setString(6,bb);

stat.executeUpdate();
stat.close();
}
catch(Exception e){
System.out.println(e);
}
}
2.also tried this in my code and recieve an exception.
Date date= new Date();
SimpleDateFormat template = new SimpleDateFormat("dd-mm-yyyy");
Date enddate = new Date(datefield.getText());
java.sql.Date date = java.sql.Date.valueOf(template.format(enddate));
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. i used this and recieve exception message
...
2.also tried this in my code and recieve an exception.

What does the exception message say? Where is it being thrown from?
 
oghenekaro EFEKODO
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i forgot.thanks for telling to include the kind of exception.
exception for
1. syntax error in INSERT INTO statement.
2.compile with -Xlint:deprecation
when i did,it tells me that java.util.Date(String s) is deprecated.so what i used to replace it.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

syntax error in INSERT INTO statement.

So, this error is telling you you SQL is invalid. If you read the whole error message it should tell you why.


2.compile with -Xlint:deprecation

This is not an error message, it is part of a compiler warning. So it is not waht is stopping your code from working it is just informing you that you have choosen to use code that is very out of date. If you look at the JavaDocs for DateFormat you will notice there are a number of methods that parse string representations of dates and return them as Date objects. Use one of them instead.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One other thing, why are you using the JDBC-ODBC bridge? Is Access your database?
 
oghenekaro EFEKODO
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,my database is access
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic