MySQL datatime NULL - java

It's written here [MySQL, how to insert null dates ]
that MySQL support NULL in datetime field. I am doing this with Java and get next error:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'NULL' for column 'lastvisited' at row 1
If I run in phpmyadmin:
UPDATE `linksbase` SET `lastvisited`=NULL WHERE 1
It is updated.
What is the problemw ith Java code?
prepareStatement("INSERT INTO `linksbase` (`lastvisited`) VALUES ('NULL')");
If I change statement to
prepareStatement("INSERT INTO `linksbase` (`lastvisited`) VALUES (NULL)");
Error is
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'lastvisited' cannot be null

Related

Error inserting null or empty value in DATE column mapped to LocalDate type in java

I am using MS SQL as my DB and I have a DATE column called 'START_DATE' in one of my tables. This is a non mandatory column.
In my java layer I am have mapped this to the LocalDate . When I dont have any value set for START_DATE, then I set it to null or leave it empty.
In both cases I get the error that
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Implicit conversion from data type varbinary to date is not allowed. Use the CONVERT function to run this query.
How do I fix this, please advise?
Are you using 2012 version or newer? The problem is in the way SQL 2012 interprets null values for a datetime time. Insert query from Java should have an explicit convert function to forcefully insert it as datetime to avoid this unexpected error.
Something like:
convert(DATETIME,START_DATE,21)
More info
I had the same issue with SQL Server. I fixed it by changing the insert value from just myValue to
CONVERT(DATE, myValue)

select max of date with query

I want to select max of date in hibernate but I get this error:
java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes:
expected TIMESTAMP got NUMBER
The query is :
select coalesce(max (rc.dateTransactionReceipt),0 ) from MAMReceiptTransactions rc where rc.mamItems.id =m.id ) as lastDateOfCharge
and the database is oracle.
Type of this field in database is TIMESTAMP(6)
Trying to get 0 when timestamp doesn't make sense apart from being syntactically incorrect (The datatypes of the coalesce parameters must be compatible). Null itself sounds reasonable.
select max(rc.dateTransaction) from your_table rc
If you want to have a default timestamp returned, you can use that in the coalesce instead. Perhaps you want current timestamp returned in case the above returns null.
select coalesce(max(rc.dateTransaction), systimestamp) from your_table rc;

Java MySQL BigDecimal Column cannot be null

I have a problem inserting BigDecimal into MySQL table. The MySQL column is type decimal(10,2) NOT NULL DEFAULT 0.00.
The MYSQL Query
UPDATE `table` SET `Cost` = ? WHERE `Id` = 1
The JAVA code:
stmt.setBigDecimal(0, BigDecimal.valueOf("0.00"));
stmt.executeUpdate();
I get an error that says com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'Cost' cannot be null
Why is this happening?
Bound parameter indexes are 1-based, not 0-based in JDBC.
In other words, the first bound parameter has the index "1", not "0".
Replacing "0" with "1" in the following line will fix your issue:
stmt.setBigDecimal(1, new BigDecimal("0.00"));

Mysql&MSAccess: insert into table which have an incremented field

I am sorry if there is a duplicate but I tried all ways still I can't do the insertion.
I have a table with only two fields ( ID , Name )
When I run this SQL code it must be insert a new record and increment the ID field automatically
because it's auto increment but unfortunately don't work .
See the trail and errors :
MYSQL :
PreparedStatement pr = con.prepareStatement("insert into names(name) values(?)");
pr.setString(2,"Azad");
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
insert into names(id,name) values(?,?)
java.sql.SQLException: No value specified for parameter 1
MS Access :
insert into names(name) values(?)
java.lang.ArrayIndexOutOfBoundsException: 1
insert into names(id,name) values (?,?)
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
What's the reason of those errors ? and how to solve it ?
Thanks for suggestions and answers.
change pr.setString(2,"Azad"); to pr.setString(1,"Azad");
The first parameter is related to the position of the ? in the prepared statement, not the column index in the table.
java.sql.SQLException: No value specified for parameter 1. This is down to the fact that you have specified two parameters for the query. But have only specified one value, for the second parameter. If "ID" is an auto incremented column then you don't need to pass in a value. If its not then
pr.setString(1,IDVALUE);
pr.setString(2,"Azad");

Inserting values in to oracle table using jsp

I am using the following code to insert values in to a table.
String sql = "INSERT INTO APPLICATION VALUES (?,?,?,?,?,?,TO_DATE(?,'DD/MMYYYY'),?,TO_DATE(?,'DD/MM/YYYY'),?,?,?,?,?,SYSDATE,'X',?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,Integer.parseInt(sr));
pstmt.setString(2,nm);
pstmt.setString(3,(String)session.getValue("ITSGTYP"));
pstmt.setString(4,pst);
pstmt.setString(5,dox);
pstmt.setString(6,zo);
pstmt.setString(7,dob);
pstmt.setString(8,cdr);
pstmt.setString(9,cdrdt);
pstmt.setString(10,qual);
pstmt.setString(11,mail);
pstmt.setString(12,bond);
pstmt.setInt(13,Integer.parseInt((String)session.getValue("USER")));
pstmt.setString(14,request.getRemoteAddr());
pstmt.setString(17,place);
The description of the table into which the values are inserted is as follows
EMP_ID NOT NULL NUMBER(6)
NAME VARCHAR2(25)
APPLN_TYP VARCHAR2(10)
POST VARCHAR2(100)
DIV VARCHAR2(25)
ZONE VARCHAR2(5)
DOB DATE
CADRE VARCHAR2(5)
CADRE_DATE DATE
QUALIFICATION VARCHAR2(100)
EMAIL_ID VARCHAR2(70)
BOND VARCHAR2(3)
SUBMITTED_BY NUMBER(6)
SUBMIT_IP VARCHAR2(30)
SUBMIT_DATE DATE
FLAG VARCHAR2(1)
PLACE VARCHAR2(20)
While executing the above code I am getting the following error
Error: java.sql.SQLException: Invalid column index
This query was working fine before.
My previous table didn't have the PLACE column. I had to insert it at a later point.
It's safer to include the columns names you want to insert into the SQL statement like:
String sql = "INSERT INTO APPLICATION VALUES (EMP_ID,NAME, ....) // etc
(?,?,?,?,?,?,TO_DATE(?,'DD/MMYYYY'),?,TO_DATE(?,'DD/MM/YYYY'),?,?,?,?,?,SYSDATE,'X',?)";
In this way, you have more control of the indexes and the columns you are using in your statement.
Replace pstmt.setString(17,place);
with
pstmt.setString(15,place);
The reason for error you get is :
You don't have 17 ? symbols in your query for prepared statement, you only have 15 ? symbols that means you can only set 15 values (for 15 columns) for that prepared statement.
Now what you were doing is you were setting a parameter at 17 th index and you don't have any column specified at index 17 in your query, you only have 15 columns and 15 ? symbols for the values to be inserted at respective 15 columns.
So replace it with what I mentioned above and it will work.
In your query u have 15 parameters to set. And you are trying to give value of 17th index. You should change it to 15 instead of 17.
i.e
pstmt.setString(15,place);

Categories