I have MSSQL procedure:
my_proc
I get data from java
String sql = "exec my_proc '2016-01-01','2016-01-20','2016-01-01'";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
and get all data fine!
but if I use
String msisdn, String startDate, String endDate
String sql = "exec my_proc ?,?,?";
stmt = connection.prepareStatement(sql);
stmt.setString(1, msisdn);
stmt.setString(2, startDate);
stmt.setString(3, endDate);
rs = stmt.executeQuery();
I get error:
com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type varchar to datetime.
In procedure I have variables:
#Startdtin datetime,
#Enddtin datetime,
#msisdnin varchar(18)
I tried use
stmt.setTimestamp(2, startDate); //startDate - convert to Timestamp
and
stmt.setDate(2, startDate); //startDate - convert to Date (sql and util)
It is not helped. How pass date to PreparedStatementcorrectly`?
Your startDate is of type String. So convert it into Date type.
java.util.Date myDate = new java.util.Date(startDate);
java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
-------------------------------
stmt.setDate(5, sqlDate);
Use CallableStatement to execute procedures.
Once corrected the format of your data you have to change the code.
To execute a procedure you need to use a CallableStatement, not a PreparedStatement.
If you have your date in string format, first convert it to Date in java using a SimpleDateFormat.
Than simply set it as Date on the callableStatement.
String dateString = ...;
Date date = ...;
myCallableStatement.setDate(1, date);
Related
In the database server, the format of the date column is MM/DD/YYYY HH:MM:SS AM
How can I create the SQL date object to send to database in the same format mentioned above?
Type of the database column is DATE. I am using Oracle Database 12C : SQL.
DATE or TIMESTAMP columns do not have "a format".
As your value apparently contains a time, you should use LocalDateTime or java.sql.Timestamp to retrieve that value.
Something like:
String sql = "select the_timstamp_column from the_table";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
LocalDateTime tsValue = rs.getObject(1, LocalDateTime.class);
}
But still not all JDBC drivers support that, so the second best solution is to use java.sql.Timestamp:
String sql = "select the_timstamp_column from the_table";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
java.sql.Timestamp tsValue = rs.getTimestamp(1);
}
In both cases you can format the value of tsValue anyway you like in your Java code.
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
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();
Here is my string: 2016-07-29T17:15:46.838Z
I want to insert this into a MySQL DATETIME(6) column.
Here is the method I created to convert the string to a java.sql.Timestamp
private java.sql.Timestamp convertToJavaSqlTimeStamp(String p_dateTimeString) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss:SSS");
java.sql.Timestamp _timeStamp = new java.sql.Timestamp(formatter.parseDateTime(p_dateTimeString).getMillis());
return _timeStamp;
}
.
.
.
SQL Insert logic...
PreparedStatement preparedStatement ;
preparedStatement = _mysqlConn.prepareStatement("INSERT INTO myTable (my_date_time) VALUES (?)");
preparedStatement.setTimestamp(1, convertJodaDateTimeStringToJavaSqlTimeStamp("2016-07-29T17:15:46.838Z"));
preparedStatement.executeUpdate();
Error Message: Invalid format: "2016-07-29T17:15:46.432Z" is malformed at "-07-29T17:15:46.432Z"
Going off Uueerdo's comment. The following did the trick
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Thank you sir.
I have a column in database having datatype DATETIME. I want to set this column value to current date and time using `PreparedStatement. How do I do that?
Use PreparedStatement#setTimestamp() wherein you pass a java.sql.Timestamp which is constructed with System#currentTimeMillis().
preparedStatement.setTimestamp(index, new Timestamp(System.currentTimeMillis()));
// ...
Alternativaly, if the DB supports it, you could also call a DB specific function to set it with the current timestamp. For example MySQL supports now() for this. E.g.
String sql = "INSERT INTO user (email, creationdate) VALUES (?, now())";
Or if the DB supports it, change the field type to one which automatically sets the insert/update timestamp, such as TIMESTAMP instead of DATETIME in MySQL.
conn = getConnection();
String query = "insert into your_table(id, date_column) values(?, ?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, "0001");
java.sql.Date date = getCurrentDatetime();
pstmt.setDate(2, date);
Where the function getCurrentDatetime() does the following:
public java.sql.Date getCurrentDatetime() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}