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
514 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
TX
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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:
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 …

762 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

13 Experts available now in Live!

Get 1:1 Help Now