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
539 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
null output 3 53
ejb mdb examples 1 35
Notify sent to other threads in Java 9 52
junit example issue 2 49
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

742 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