insert current date in oracle database using jdbc - java

I am trying to insert current date into Oracle sql.I had set the datatype to be DATE in oracle sql and changed the date format to DD-MM-YYYY.Now I am trying to insert current date as follows.But I am getting some error as shown below.Don't know what is wrong
String sql6 = "insert into account(acc_no,acc_type,primary_phone_number,people_in_plan,acc_activated_date,acc_deactivated_date) values('"+n1+"','"+acctype+"','"+primaryphoneno+"','"+number_of_people+"',?,?)";
Stmt = connection.prepareStatement(sql6);
SimpleDateFormat sdf=new SimpleDateFormat("DD-MM-YYYY");
Date date1 = new Date(System.currentTimeMillis());
Stmt.setString(5, sdf.format(date1));
Stmt.setNull(6, java.sql.Types.DATE);
Stmt.executeUpdate();
java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5386)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5374)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:282)

It looks like your column indexes are incorrect. They should be 1 and 2 respective.
Stmt.setString(1, sdf.format(date1));
Stmt.setNull(2, java.sql.Types.DATE);

please user statement.setObject(1, new java.sql.Date());instead

Related

How to insert sql date as 04/05/2017 am trying with this

java.util.Date date = new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("dob"));
System.out.println(date);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
PreparedStatement ps=cn.prepareStatement("insert into lol values(?,?)");
ps.setString(1, name);
ps.setDate(2, sqlDate);
But output is 05-MAY-17 this, i want store 04-05-2017 in oracle db. Can any one help me
Make the varchar column in db . Store it as a String .
When you want to do some operation at Application Level , convert the string into date .
For that you can use SimpleDateFormat

preparedStatement with TimeStamp compare two date

i have this code
preparedStatement = jdbcManager.getConnection().prepareStatement(query);
Date start; /*get from postgres column type-> Timestamp without time zone*/
java.sql.Timestamp timestamp = new java.sql.Timestamp(start.getTime());
log.debug("Parametro d'ingresso query: "+timestamp);
preparedStatement.setTimestamp(1, timestamp);
and i have this query
SELECT
DATA
FROM MY_TABLE
WHERE DATA > ?
the column DATA is DATA_TYPE = DATE in a Oracle db
the compare in the query not working,
what am I doing wrong?
You're comparing date with timestamp. Instead of fetching time stamp, get java.sql.Date. And also in prepared statement, use setDate instead of setTimeStamp.

Date specific format in mysql

Is there any way to store the below date in mysql table
Date = 2017-01-05T00:00:00+05:30
//Table
create table test(dob DATETIME);
//Insert
insert into test(dob) values ('2017-01-05T00:00:00+05:30') // Throws error saying Incorrect datetime
Is there way I can insert the below date in mysql db from java code.
If you have a Date object, good. Otherwise, parse the string into a Date object using SimpleDateFormat like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date d = sdf.parse("2017-01-05T00:00:00+05:30");
Then you can make a PreparedStatement to do the date insert like this:
PreparedStatement ps = conn.prepareStatement("insert into test(dob) values (?)");
ps.setDate(1, new java.sql.Date(d.getTime()));
ps.executeUpdate();

Getting java sql time and inserting it to a table

Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Time currentTimestamp = new java.sql.Time(now.getTime());
Date date = new Date(currentTimestamp.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(date);
java.sql.Date.valueOf(formattedDate);
System.out.println(formattedDate);
How to get the current formatted System time(yyyy-MM-dd) and insert it to the table? I already tried my best and search for the answer but its no good I get different error messages like when I insert java.sql.Date.valueOf(formattedDate);I always get java.lang.IllegalArgumentException but when I remove java.sql.Date.valueOf(formattedDate) and change this line "+pcList.get(j).getid()+",0,0 into "+pcList.get(j).getid()+",0,2014-07-07 I always get an error saying Data truncation: Incorrect date value: '2000' for column 'date' at row 1
if(ifLoggedIn){
for(int j=0;j<b;j++){
try {
Connection conn = DriverManager.getConnection(url, "root", "");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MAX(transactionID) from transaction");
while(rs.next()){
tID = rs.getInt("MAX(transactionID)");
}
tID= tID+1;
stmt.executeUpdate("Insert into ccpdb.transaction(membershipNum,productID,time,date,paymentOption,amount,transactionID,quantity) values ("+currentuser.getMembershipNum()+","+pcList.get(j).getid()+",0,0,\"west\","+tf1.getText()+","+tID+","+pcList.get(j).f3.getText()+")"
+ "");
conn.close();
} catch (Exception g) {
System.err.println("Got an exception! ");
System.err.println(g.getMessage());
}
}
}
Please help me!!
Just use the database time, rather than the java time. You can get it using the function now() or CURRENT_DATE. I can't quite tell where you want to put this in the insert, but it can go in the list of values.
Try this:
java.util.Date now = new java.util.Date();
java.sql.Date insertDate = new java.sql.Date(now.getTime());
You can insert this date directly.
You are getting this error as you are trying to save String in date column.

How to insert Date into mysql?

I am trying to insert a Date into mySql db . I accept date as a string and parse it to util.Date then convert it to sql.Date and then I am inserting into database . Here is my code
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String mydate=dsd.getDoa();
Date mydate2=format.parse(mydate);
java.sql.Date sqlDate;
sqlDate = new java.sql.Date(mydate2.getTime());
System.out.println("Date"+sqlDate.getDate()+"Month"+sqlDate.getMonth()+"Year"+sqlDate.getYear());
....
int i=st.executeUpdate("insert into device_sales_details values("+sqlDate+"));
The error is as follow ,
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '1980' for column 'device_sales_details_DOA' at row 1
The Sysout prints as follow for input 22/12/2014
Date22Month11Year114 How to solve this problem , can any one help me in this please.
Use a PreparedStatement. It's almost never correct to use string concatenation to create SQL code to send through JDBC.
PreparedStatement ps = connection.prepareStatement("insert into device_sales_details values(?)");
ps.setDate(1, sqlDate);
int i = ps.executeUpdate();
More about not using string concatenation here: http://bobby-tables.com
Separately, it's generally best to specify the columns when doing an insert, e.g.: insert into device_sales_details (columnName) values (?)

Categories