Solved

How to convert string value of date time into date time so that it can be inserted into datetime field

Posted on 2013-06-20
7
519 Views
Last Modified: 2013-06-20
class sqlsave
{
      public static void main(String args[]) throws Exception
      {
            Connection con;

            Statement  stmt;
            ResultSet res;
            
            String driver="sun.jdbc.odbc.JdbcOdbcDriver";
            String url="jdbc:odbc:mydsn";
            String userid="";
            String password="";
            
            int teno = 6;
            String tename="Kapil";
            String tcity="Fazilka";
      
      try
      {
            Class.forName(driver);      
            con = DriverManager.getConnection(url,userid, password);

            String s;
            s = "insert into emp(eno,name,city,doj) values("+teno+",'"+tename+"','"+tcity+"')";
            stmt = con.createStatement();
            stmt.executeUpdate(s);
            stmt.close();
            con.close();
      }
      catch(SQLException ex)
      {
      System.out.println("Error");
      }
      }
}


How to insert date time value from textbox into database having datetime data type in sql server.
0
Comment
Question by:searchsanjaysharma
7 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39262868
For SQL server, I would send the data as ODBC canonical format, which is 'yyyy-mm-dd hh:mi:ss' in SQL terms. In Java, you can achieve this format using "yyyy-MM-dd HH:mm:ss".

For example:
java.text.Format sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date_for_sql = sdf.format(new java.util.Date("3/12/13 13:00"));

The Date constructor with string parameter is deprecated, but it was an easy way to get my point across. You convert the string you have to a date object in Java, but pass a formatted string to SQL. I just find it easier, but you can use a similar approach to get a java.sql.Date object using valueOf(). The key is to pass date like "2013-06-20".
http://docs.oracle.com/javase/6/docs/api/java/sql/Date.html#valueOf(java.lang.String)
0
 

Author Comment

by:searchsanjaysharma
ID: 39263156
java.util.Date now=new java.util.Date();
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss" );
System.out.println(formatter.format(now));

How to insert this kind of value in table.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39263201
Use a PreparedStatement ps and call ps.setTimestamp or ps.setDate

As has been stated above by  mwvisa1,java.sql.Date.valueOf can be used, or use java.sql.Timestamp.valueOf
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39263235
1. you have to convert the string from text box to date. You have to use parse, not format!
see example:

String time = "Sun Jul 15 2012 12:22:00 GMT+03:00 (FLE Daylight Time)";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
Date date = sdf.parse(time);
You have only to change the format string, according to your needs, but I'm sure your case is much more simpler :-)

2. You have to use java.sql.Timestamp in order to insert the date into a DB field, which is of type Datetime.
see example here /it's short and easy/ :
http://www.mkyong.com/jdbc/how-to-insert-timestamp-value-in-preparedstatement/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39263292
You have to use parse, not format!
Yes, that was just a typo on  mwvisa1's part most probably.  searchsanjaysharma knows which way the conversion is to go, as evidenced by the title of the question
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39264141
TX
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39264288
?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
Java passing arguments (type error) 2 48
Error with Java/Cache JDBC Classpath 2 31
difference of if loops 23 41
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now