?
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
Medium Priority
?
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 60

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 16

Accepted Solution

by:
Valeri earned 1500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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