Insert date time into oracle database from jsp

pls tell me ,from jsp how can I insert datetime values into oracle database .I am using oracle 9i .here is codethat i have tried
html--code
<select name="date">
<option selected>dd</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<select name="month">
<option selected>dd</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<select name="year">
<option selected>dd</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
</select>
here the jsp code
<% date= request.getParameter("date"); %>
<% month= request.getParameter("month"); %>
<% year= request.getParameter("year"); %>
try
{ Class.forName("oracle.jdbc.driver.OracleDriver"); }
catch (ClassNotFoundException exception)
try
     Connection connection = null;
     out.println("connectiong the database");
connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcsid","scott","tiger");
out.println("connection getted");
     int rows = 0;
     String query_2 = "insert into mrdetails values(?)";
     String dob = date+month+year;
     prepstat = connection.prepareStatement(query_2);
     prepstat.setTimestamp(4,dob);
     rows = prepstat.executeUpdate();
     out.println("data updated");
catch (Exception exception3)
out.println("Exception raised"+exception3.toString());
}

To insert date values into a database, you should use java.sql.Date. If it also has a time component, then java.sql.TimeStamp.
Your use of prepared statements is good.
You just need to convert the parameters into a date.
One way to do this is using java.text.SimpleDateFormat.
int rows = 0;
String query_2 = "insert into mrdetails values(?)";
String dob = date+"/" + month+ "/" + year;
SimpleDateFormat sdf = new SImpleDateFormat("dd/MM/yyyy");
java.util.Date javaDate = sdf.parse(dob);
java.sql.Date sqlDate = new java.sql.Date(javaDate .getTime);
prepstat = connection.prepareStatement(query_2);
prepstat.setTimestamp(4,sqlDate);
rows = prepstat.executeUpdate();
out.println("data updated");Cheers,
evnafets

Similar Messages

  • Problem while inserting Date/Time in Oracle Database

    Hai,
    i want to insert the date and time in a date column. here, i am using java.sql.Date and java.sql.Time to assign the date in Prepared Statement.
    PreparedStatement psmt=con.prepareStatement("insert into test(ex_date) values(?)");
    java.util.Calendar c=Calendar.getInstance();
    c.set(2002,2,21,10,10,00);
    java.sql.Date d=new Date(c.getTime().getTime());
    java.sql.Time t=new Time(c.getTime().getTime());
    psmt.clearParameters();
    psmt.setDate(1,d);
    psmt.setDate(1,t);
    psmt.executeUpdate();
    Above program is inserted the Date and time in the database as the following "1900/2/21 10:10 AM"
    but i am giving the year as 2002. it inserted the year as 1900. what is the problem with the code?
    please let me know
    Thanks in advance.
    Regards
    sankarjune14

    Hai Franco,
    i put the wrong code. Here, is the Original Code.
    PreparedStatement psmt=con.prepareStatement("insert into test(ex_date) values(?)");
    java.util.Calendar c=Calendar.getInstance();
    c.set(2002,2,21,10,10,00);
    java.sql.Date d=new Date(c.getTime().getTime());
    java.sql.Time t=new Time(c.getTime().getTime());
    psmt.clearParameters();
    psmt.setDate(1,d);
    psmt.setTime(1,t); // Last time i put psmt.setDate(1,t);
    psmt.executeUpdate();
    and, the getTimeInMillis() method is a protected method in java.util.Calendar class. how can we use it
    please give me some other idea to insert the date and time in oracle database.
    Thanks in advance
    sankarjune14

  • Issue of inserting greek characters into Oracle database using ICAN505

    Hi All,
    We are currently facing an issue of inserting greek characters into Oracle database using ICAN505.
    We receive a file containing greek characters.The values from the file should be inserted into the database.We are reading the file using file OTD with default encoding.
    The file can contain english characters too other than greek characters.
    The database NLS_CHARACTERSET is AL32UTF8.
    When I insert using an insert statement directly ,the values get inserted properly into the DB table.
    Inserting the same values using code results in improper characters getting inserted into the table in the database.
    Please help....
    Thanks in advance !!

    Globalization forum?
    Globalization Support
    It works for SQL Developer, which does not depend on NLS_LANG, so I suspect a problem with your NLS settings.

  • Insert a value of type Date/Time into a database

    I am trying to insert a value of type date time into an access database. What is the format needed to insert the date/time.
    Thanks,
    -Amos

    I had all kinds of problems getting a datetime inserted into a SQl Server DB. Probably similar so this is what I do:
    String date = new String("01/01/97 12:00:00");
    stmt.setObject(1, date);
    I tried using TimeStamp's but found I got an occasional Fractional Truncation exception back from the driver. Never had a problem inserting a String using the setObject() method though.
    Hope this helps

  • How to put date&time into a variable from a database?

    In an Oracle table, myDateColumn is 2002-06-25 17:40:55. I can't put it into a variable.
    java.text.SimpleDateFormat dF=new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    rs=stmt.executeQuery("select myDateColumn from myTable");
    rs.next()
    System.out.println(rs.getString(1)); // 2002-06-25 17:40:55 is output
    java.util.Date myDate=rs.getDate(1);
    out.println(dF.format(myDate)); // 25/06/2002 00:00:00 is output
    java.util.Date myDate=rs.getTime(1);
    out.println(dF.format(myDate)); // 01/01/1970 17:40:55 is output
    My question is: How should I put this date (both date and time) into a variable, myDate?

    There are three JDBC types relating to time:
    1] The JDBC DATE type represents a date consisting of day, month, and year. The corresponding SQL DATE type is defined in SQL-92, but it is implemented by only a subset of the major databases. Some databases offer alternative SQL types that support similar semantics.
    2] The JDBC TIME type represents a time consisting of hours, minutes, and seconds. The corresponding SQL TIME type is defined in SQL-92, but it is implemented by only a subset of the major databases. As with DATE, some databases offer alternative SQL types that support similar semantics.
    3] The JDBC TIMESTAMP type represents DATE plus TIME plus a nanosecond field. The corresponding SQL TIMESTAMP type is defined in SQL-92, but it is implemented by only a very small number of databases.
    In ur case pls use:
    java.sql.Timestamp
    good luck !
    ...san :--)

  • Insert statement with Date, time into Oracle

    Hi,
    I've got the following statement that I'm trying to insert into Oracle. Actually had to change up the format to dd-mm-yy to get it to insert the date correctly, not sure why, but I've got it "supposedly" formatted to also insert the hours, min., seconds into the db record, but it's not catching it.
    My code:
    INSERT INTO VOTETBL
    (CHANGE_CTRL_ID,BUSVP,BRANCH,VOTE,VOTE_TIMESTAMP)
    VALUES (?, ?,?,?,to_date(sysdate, 'dd-mm-yy hh24:mi:ss'));Shouldn't the sysdate capture all those elements sufficiently? The day, month, year get inserted correctly with this, but not so for the time portion.
    Any suggestions is welcomed.
    Thx.

    ok, thanks.
    I tried making a result set with the following:
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rst = stmt.executeQuery("select to_char(sysdate) from dual");
    Date dualtimestamp = (rst.getDate("sysdate"));But it makes the servlet blow up! Not sure why either, because if I enter that exact statement in Oracle, it pulls back the date and time just like I aim to. But not embedded in the result set statement within the servlet.
    It breaks up somehow and doesn't get that result!

  • Inserting Multiple Images into oracle database using JDBC

    I wanted to insert multiple images into database using JDBC by reading it from the file... and i am passing photos.txt(my text file) as an input parameter... I have inserted all the values into the database except for the image part... this is my content of photos.txt file and i have copied all the images into the folder
    *" C:\\photos "*
    *1,in1.jpg,108,19,in-n-out*
    *2,in2.jpg,187,21,in-n-out*
    *3,in3.jpg,308,41,in-n-out*
    *4,in4.jpg,477,52,in-n-out*
    *5,in5.jpg,530,50,in-n-out*
    and i want to store in1.jpg,in2.jpg,in3.jpg,in4.jpg,in5.jpg into the oracle databse using JDBC.... i have tried a lot using BLOB column.... and i have created my table as
    CREATE TABLE PHOTO(
    ID NUMBER NOT NULL PRIMARY KEY ,
    Name BLOB,
    X DOUBLE PRECISION,
    Y DOUBLE PRECISION,
    Tags VARCHAR2(40)
      try {                 // for restaurant System.out.println();System.out.println();System.out.println(); System.out.print("  Creating Statement for Photo...\n");             stmt2 = con.createStatement ();                       stmt2.executeUpdate("delete from PHOTO"); stmt2.executeUpdate("commit"); PreparedStatement stmt3 = con.prepareStatement ("INSERT INTO PHOTO VALUES (?, ?, ?, ?, ?)");             System.out.print("  Create FileReader Object for file: " + inputFileName1 + "...\n");             FileReader inputFileReader2 = new FileReader(inputFileName1);             System.out.print("  Create BufferedReader Object for FileReader Object...\n");             BufferedReader inputStream2  = new BufferedReader(inputFileReader2);             String inLine2 = null;                         String[] tokens; //            String[] imageFilenames = {"c:\\photos\\in1.jpg","c:\\photos\\in2.jpg","c:\\photos\\in3.jpg","c:\\photos\\in4.jpg","c:\\photos\\in5.jpg", //  "c:\\photos\\in6.jpg","c:\\photos\\in7.jpg","c:\\photos\\in8.jpg","c:\\photos\\in9.jpg","c:\\photos\\in10.jpg","c:\\photos\\arb1.jpg","c:\\photos\\arb2.jpg", //  "c:\\photos\\arb3.jpg","c:\\photos\\arb4.jpg","c:\\photos\\arb5.jpg","c:\\photos\\den1.jpg","c:\\photos\\den2.jpg","c:\\photos\\den3.jpg", //  "c:\\photos\\den4.jpg","c:\\photos\\den5.jpg","c:\\photos\\hop1.jpg","c:\\photos\\hop2.jpg","c:\\photos\\hop3.jpg","c:\\photos\\hop4.jpg","c:\\photos\\hop5.jpg"};               File file = new File("C:\\photos\\in1.jpg");            \\ ( Just for example  )           FileInputStream fs = new FileInputStream(file);                         while ((inLine2 = inputStream2.readLine()) != null) {               tokens= inLine2.split(",");             st2 = new StringTokenizer(inLine2, DELIM);                                                             stmt3.setString(1, tokens[0]);               stmt3.setBinaryStream(2, fs, (int)(file.length()));               stmt3.setString(3, tokens[2]);               stmt3.setString(4, tokens[3]);               stmt3.setString(5, tokens[4]);               stmt3.execute(); //execute the prepared statement               stmt3.clearParameters(); 
    As i am able to enter one image file by above code in1.jpg in to the oracle database.... but i am not able to insert all the image file in to the database.....do tell me what should i do.... and can you give me the example on the basis of the above code of mine...
    do reply as soon as possible..

    jwenting wrote:
    that depends. Putting the images in BLOBs prevents the file locations stored in the database from getting out of synch with the filesystem when sysadmins decide to reorganise directory structures or "archive" "old" files that noone uses anyway.True, but it really comes down to a business decision (cost-benefit analysis). If you have the bucks, the expertise, and the time, go with the Blobs, otherwise go with the flat files.

  • How to insert date/time to sql database

    Hi,
    I would like to insert date and time alongside with my data receiving from the sensor.
    Right now i can insert data into sql table everytime when the data changes but i also wanted to insert the date and time as well.
    Thank you in advance!

    I create a field that is a datetime and set the default value to getdate().  So everytime i make an entry into this database, it automatically adds a datestamp.

  • How to insert date value into oracle?

    hi,
    iam reading date from xml using vb.net and inserting it into oracle table.
    the date value in xml file is in the format of "01012003".
    i want to insert into oracle table.but iam getting an error "ORA-1843: not a valid month".
    how to solve this problem.
    Any suggestions or examples...

    That's not a date. It's just a string. Convert it to a date using,
    DateTime d = DateTime.ParseExact("01022003", "MMddyyyy", null);
    or
    Dim d As DateTime = DateTime.ParseExact("01022003", "MMddyyyy", Nothing)
    and then bind it to a parameter. ODP.NET will take it from there.
    You should never be hard-coding the date literal into your SQL query.
    David

  • Inserting multiple records into a database from a table

    I have a dynamic table with rows upto 10. i am trying to insert all the records in these rows into a database ( msaccess)
    table name: Table1 ( in my form )
    Database table : movies_comments ( as posted in stefan's forums ).
    table columns: username, movieid, comments
    i am using the following code.
    Database.ExecSQL() function is as posted in stefcamerons forums.
    var tlength = xfa.resolveNode("form1.#subform[0].Table1.Row1[*]").length;
    xfa.host.messageBox("tlength is :" +tlength);
    for ( var i = 1; i <= tlength; i++)
    var username = xfa.resolveNode("form1.#subform[0].Table1.Row1["+i+"].user_name").rawValue;
    var movieID = xfa.resolveNode("form1.#subform[0].Table1.Row1["+i+"].movie_id").rawValue;
    var commentS = xfa.resolveNode("form1.#subform[0].Table1.Row1["+i+"].comment_").rawValue;
    Database.ExecSQL("INSERT INTO movie_comments (username, movieId, comment) VALUES ('username','movieID', 'commentS');");
    i am trying to insert multiple records using the code above. But i am getting error
    GeneralError: Operation failed.
    XFAObject.resolveNode:1:XFA:form1[0]:#subform[0]:Button1[0]:click
    SOM expression returned list when single result was expected
    suggest me an alternate way to insert multiple records from a table into a database.
    thank Q

    figured it out. I changed the ...OR (alternative names = colname) in the recordset to ...OR (alternative names LIKE %colname%). Works like a charm now- the result of a good night's sleep.

  • How to insert new record into oracle table from GridView in VS2005.

    I need to insert records into Oracle10g table from VS2005 GridView using the Insert Method in Business Logic Class. The Update and Delete Methods are working fine. What's the best way out?

    How is it "not possible"?
    Either modify the Class for the new fields, or give the new fields default values (if applicable). If the type of an existing column has changed, then only the first option is available.
    Where's the problem?

  • Inserting a record into ORACLE table from a webdynpro application

    Hi,
    From My webdypro code I have 10 attributes which are need to  be inserted into a ORACLE DB table. Can anyone suggest me how to achieve it or Can you please point me a tutorrial which can help on it.
    All the attributes will be inserted in a single record.
    Thanks,
    James

    check this code
    Created on Oct 18, 2007
    To change the template for this generated file go to
    Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
    package com.psft;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
    @author BALU
    To change the template for this generated type comment go to
    Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
    public class DataBaseDAO
         ResultSet rs;
         int qID = 1;
         int noOfChoices=0;
         Connection con;
         Statement stmt;
         public DataBaseDAO()
              try{
                   /*Class.forName("oracle.jdbc.driver.OracleDriver");
               con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","bduvvuri","dbsub516");
               stmt = con.createStatement();*/
               DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
               Class.forName("oracle.jdbc.driver.OracleDriver");
               String s ="jdbc:oracle:thin:@192.168.9.34:1527:ep7";
               con=DriverManager.getConnection(s,"user1","welcome1");
               stmt = con.createStatement();
              }catch(Exception e){}
         public int register(String name,String pass,String emailid){
              int i =0;
              try{
                   PreparedStatement ps = con.prepareStatement ("Insert into USERS(username,password,email) values (?,?,?)");
                   ps.setString( 1, name);
                   ps.setString( 2, pass);
                   ps.setString( 3, emailid);
                   i = ps.executeUpdate();
                   }catch(Exception e){
                   System.out.println("ee"+e.getMessage());
              return i;
    xxxxxx (please read forum rules)
    Edited by: Armin Reichert on Jan 17, 2008 10:17 AM

  • How to insert date into oracle database

    Hi, there,
    I want to insert date information to oracle database in a jsp page using JSTL. but always got wrong message:
    javax.servlet.jsp.JspException:
    INSERT INTO DATE_TEST
    (date_default,date_short,date_medium)
    values(?,?,?)
    : Invalid column type
    I don't know how to convert java date type to oracle date type or vice versa. the following is the source code(all the fields of DATE_DEFAULT,DATE_SHORT,DATE_MEDIUM are oracle date type. and even I want to insert d instead d1, I got the same wrong message)
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="java.util.*" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
    <%
    Calendar now;
    Calendar rightNow = Calendar.getInstance();
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>
    Hello World
    </title>
    </head>
    <body>
    <h2>
    The current time is:
    </h2>
    <p>
    <%= new java.util.Date() %></p>
    <%
    java.util.Date d=new java.util.Date();
    java.sql.Date d1=new java.sql.Date(d.getYear(),d.getMonth(),d.getDate());
    out.print(d1.toString());
    %>
    <sql:update>
    INSERT INTO DATE_TEST
    (DATE_DEFAULT,DATE_SHORT,DATE_MEDIUM)
    VALUES(?,?,?)
    <sql:dateParam value="${d}" type="date" />
    <sql:dateParam value="${d}" type="date" />
    <sql:dateParam value="${d}" type="date" />
    </sql:update>
    </body>
    </html>
    thank you very much for the great help!!

    Hi, there,
    I want to insert date information to oracle database in a jsp page using JSTL. but always got wrong message:
    javax.servlet.jsp.JspException:
    INSERT INTO DATE_TEST
    (date_default,date_short,date_medium)
    values(?,?,?)
    : Invalid column type
    I don't know how to convert java date type to oracle date type or vice versa. the following is the source code(all the fields of DATE_DEFAULT,DATE_SHORT,DATE_MEDIUM are oracle date type. and even I want to insert d instead d1, I got the same wrong message)
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="java.util.*" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
    <%
    Calendar now;
    Calendar rightNow = Calendar.getInstance();
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>
    Hello World
    </title>
    </head>
    <body>
    <h2>
    The current time is:
    </h2>
    <p>
    <%= new java.util.Date() %></p>
    <%
    java.util.Date d=new java.util.Date();
    java.sql.Date d1=new java.sql.Date(d.getYear(),d.getMonth(),d.getDate());
    out.print(d1.toString());
    %>
    <sql:update>
    INSERT INTO DATE_TEST
    (DATE_DEFAULT,DATE_SHORT,DATE_MEDIUM)
    VALUES(?,?,?)
    <sql:dateParam value="${d}" type="date" />
    <sql:dateParam value="${d}" type="date" />
    <sql:dateParam value="${d}" type="date" />
    </sql:update>
    </body>
    </html>
    thank you very much for the great help!!

  • Uploading Data from a Flat file into Oracle Database

    Hi,
    I am a novice to Java . SO, please bear with me. I have a reqiurement where I have a flat file or excel file from which I need to read the data and insert into Oracle Database. The falt file will exist on the client machine. I have been reading and I see this can be done through I/O Streams. Correct me if I am wrong. I am looking for a sample code to get started. Any Java expert has an answer for it, I will appreciate it.
    Thanks

    Try UploadBean. It allows to upload files (from a browser) in Oracle.
    http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
    You will find JSP and servlet samples.

  • How to insert data into Oracle db from MySQL db through PHP?

    Hi,
    I want to insert my MySQL data into Oracle database through PHP.
    I can access Mysql database using mysql_conect() & Oracle database using oci_connect() through PHP.
    Now How can I insert my data which is in MySQL into Oracle table. Both table structure are exactly same.
    So I can use
    insert into Oracle_Table(Oracle_columns....) values(Select * from MySQL_Table);
    But again problem is I can't open both connections at the same time.
    So has anyone done this before or anyone having any other idea..
    Plz guide me...

    You can do it if you setup a ODBC Gateway between MYSQL and Oracle DB. Then you can directly read from MySQL table using DB links and insert into Oracle table in one single SQL Statement.
    Otherwise you need to fetch the data from MySQL Into variables in your PHP Program and then insert into Oracle after binding the variables to the insert statement for Oracle. Hope that is clear enough.
    Regards
    Prakash

Maybe you are looking for

  • New Mac Book Pro & Logic Pro vs New 8 Core Mac

    I think this question may have been answered. a question, but here goes anyway. I am using a new Macbook Pro duel core and running Logic Pro on it. I get "system overload" every time I start playback on multiple tracks. Then if I restart the playback

  • JSF 2.0 view handler

    JDeveloper documentation has an error in configuring the view handler. http://download.oracle.com/docs/cd/E16162_01/user.1112/e17455/dev_wpt.htm#OJDUG3574 Example 11-4 Facelet Code Added to Your faces-config.xml <application> <view-handler>com.sun.Fa

  • Macbook osx does not see the printer on my xp pc

    After hours I managed to connect my macbook to my PC (wireless ethernet network)and share folders bothways. But now I have another obstacle to overcome. I cannot print from macbook to a canon ip8500 printer connected (via USB) to my PC (Xp pro sp2).

  • Macbook pro 15" stutter/lag in photoshop

    I just bought my 15" macbook pro and installed Photoshop on it. It stutters when moving pictures, when moving zommed in canvas etc. The picture im moving is a 2mb small picture and there is a noticable lag when moving it. I've changed the performance

  • US elimiation

    HI friends, i'm working on US elimination. Dim: ENTITY , is it mandatory  to maintain INTCO property for each of entity. Eg: two entities E_US,E_UK in ENTITY Dim and I_US,I_UK are ids of INTCO dim. Usually in INTCO dim, for each intco member id (I_US