I am very beginner to Spring - JDBC .
I am trying to retrieve the employee_id from a table using the query having bind variables and also with IN condition in it .
I'm getting SQLException that
" invalid column type" - Caused by:
org.springframework.jdbc.UncategorizedSQLException:
PreparedStatementCallback; uncategorized SQLException for SQL [select
employee_id from table_employee where age=:varTwo and marks in
(:varOne) and name =:varThree]; SQL state [99999]; error code [17004];
Invalid column type; nested exception is java.sql.SQLException:
Invalid column type
Can you please tell , where I'm wrong .
I have tried using the types as Long , Integer , String but still i'm getting "invalid column type"
age is - NUMBER
marks is - NUMBER
name is - VARCHAR
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("varOne", varOne);
parameters.addValue("varTwo", Long.parseLong(varTwo));
parameters.addValue("varThree", varThree);
Long employeeId = jdbcTemplate.queryForObject("select employee_id from table_employee where age=:varTwo and marks in (:varOne) and name =:varThree" , Long.class , parameters);
I should be getting the result of this SQL as the "employee id".
I think the type of varOne may be Collection.
When you want to use a variable in SQL query especially with IN, you should make sure the variable is a correct type.
Thanks for your responses. I was able to proceed further by making the list to string using join method.
Related
SQL state [99999]; error code [17004]; Invalid column type; nested
exception is java.sql.SQLException: Invalid column
type\",\"error\":\"UncategorizedSQLException\"}"}
My case:
In our DB one user will have one Id but this one Id can can have multiple values. for example I have one userId, for my userID I have 10 health records. so I have to delete these 10 health records in one shot. so I'm passing userId and List for values(data type is Number). raw query works but when I go through java code it is giving Invalid column exception. any suggestions?
Java Implementation method is to make this call is
#Override
public void deleteSampleValue(BiometricPkDTO biometricPkDTO){
update(deleteSampleValueSql,log,biometricPkDTO.getSeriesPk(),biometricPkDTO.getSamplePks());
}
and the SQL query I added in resource folder is
delete from bio_sample
where BIO_SERIES_PK = ?
and BIO_SAMPLE_PK in (?)
Thanks.
As I'm performing multiple deletes action for one specific Id, it is suggested to use NamedParameterJdbcTemplate(getJdbcTemplate().getDataSource()). Instantiating MapSqlParameterSource object and by using the reference variable pass the inputs to sql query. The final Dao implementation method looks like:
#Override
public void deleteSample(BiometricPkDTO biometricPkDTO){
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
parameterSource.addValue("seriesPk",biometricPkDTO.getSeriesPk());
parameterSource.addValue("samplePks", biometricPkDTO.getSamplePks());
namedParameterJdbcTemplate.update(deleteSampleSql,parameterSource);
}
I am writting junit for my project using HSQLBD + Mybatis + Spring.
I have sql script for create and insert.
All fields in select are present in create script still I am facing below error.
What might be the reason?
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: FIELD
### The error may exist in cTbl.xml
### The error may involve x.getyzOrders
### The error occurred while executing a query
### SQL: SELECT fields FROM Table WHERE field1 IN (?,?,?,?,?,?) ORDER BY FIELD(field1 , ? , ? , ? , ? , ? , ? )
### Cause: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: FIELD
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: FIELD
The SQL statement is incorrect.
If the table is called FIELDS and the columns are called FIELD1, FIELD2 etc., then a SELECT statement should look like this:
SELECT fields.* FROM fields WHERE field1 IN (?,?,?,?,?,?) ORDER BY field1, field2, field3, field4, field5
You cannot use the question symbol AFTER ORDER BY
I am trying to use Spring getJdbcTemplate().queryForList(sql,params) to fetch list of records from the oracle db.
Searched SO as well as google but could not get a proper solution.
The problem is that if i run the same query in the SQL Developer it runs without any issue but if i try to call the same query by passing parameters from my Java code JDBC template its throwing exception.
Dao Code:
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("CODE", Code);
paramMap.put("NAME", Name);
List statusCodesDetails = super.getJdbcTemplate().queryForList(selectSQL,paramMap);
SQL:
SELECT STATUS FROM PROCESS P,TRANSACTION R WHERE P.FILE_ID = R.FILE_ID and P.CODE = :CODE AND R.NAME = :NAME
In the query above both the P.CODE and R.NAME are Varchar.
Exception:
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [
SELECT STATUS FROM PROCESS P,TRANSACTION R WHERE P.FILE_ID = R.FILE_ID and P.CODE = :CODE AND R.NAME = :NAME
]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
Can anybody please let me know what is the issue here and what i am doing wrong.
Update:
I was able to get it working by using the ? instead for the named parameters since i was using simplejdbctemplate. I think there is some problem while using named parameters in jdbctemplate.
Modified SQL:
SELECT STATUS FROM PROCESS P,TRANSACTION R WHERE P.FILE_ID = R.FILE_ID and P.CODE = ? AND R.NAME = ?
Modified Code:
List statusCodesDetails = super.getJdbcTemplate().queryForList(selectSQL,new Object[]{CODE , NAME});
Thanks everyone for your help.
Thanks
Vikeng21
please check the datatype in DB and datatype in java compatibility for each attribute you are retreiving.
sometimes varChar(1) will not work so use varchar(2) like this..if possible you can post both datatypes DB and Java
Spring JdbcTemplate will guess the sql type , it seems it not working correctly for your data types.
Instead of using Map<String, Object> use MapSqlParameterSource , specify parameter types using addValue(String paramName, Object value, int sqlType).
Hi am trying to do a QueryForInt using spring jbdc.The query returns a simple count.On execution i get springframework.jdbc.UncategorizedSQLException:java.sql.SQLException: Invalid column type.
I did find similar posts and tried the suggestions but i am still stuck:(...
Any help is appreciated.
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue(DAOConstants.PROD_ID, custVo.getProdId(),OracleTypes.NUMBER);
params.addValue(DAOConstants.REQ_IND, DAOConstants.FLAG_Y,OracleTypes.VARCHAR);
The query:
select count(1) from prod where prod_id = :PROD_ID and req_ind =:REQ_IND
Table definition
Name Null Type
----------------- -------- ------------
PROD_ID NOT NULL NUMBER(5)
REQ_IND VARCHAR2(10)
The Logs..
Caused by: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback;
uncategorized SQLException for SQL [select count(1) from prod where prod_id = :PROD_ID and req_ind = :REQ_IND ]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:728)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:744)
at org.springframework.jdbc.core.JdbcTemplate.queryForInt(JdbcTemplate.java:775)
... 45 more
Caused by: java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:445)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:7937)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:7517)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8174)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8155)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:230)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.setObject(WrappedPreparedStatement.java:724)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:641)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
... 56 more
Based on the stacktrace it looks like you are using the JdbcTemplate class. Since you are using the named parameter style place holders and providing a MapSqlParameterSource for the parameters you need to use the NamedParameterJdbcTemplate.
So, why are you getting this strange error message? Well, your SQL query actually works using the Oracle JDBC driver. It does allow you to use either "?" or a named parameter style using a ":" as the prefix fro the placeholders. You still need to provide the parameter values in the correct order which the MapSqlParameterSource in your case does not. You can test this by using an Object array instead like:
new Object[] {custVo.getProdId(), DAOConstants.FLAG_Y}
The error is based on the values coming in the wrong order from the params.getValues() method, with the String value being passed in for the :PROD_ID.
I have a sql as below in my java program:
String sql = "Select * from mySchema.myTable where product in (?) and myDate = ?";
I have my query params as:
Object[] params = {"\'abc\',\'pqr\',\'lmn\'",'2013-07-18'};
And I am trying to execute as:
List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, params);
where jdbcTemplate is a org.springframework.jdbc.core.JdbcTemplate object.
However, I am getting error as:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [Select * from mySchema.myTable where product in (?) and myDate = ?]; DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81; nested exception is com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:796)
Further down the stack trace:
Caused by: com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81
at com.ibm.db2.jcc.am.dd.a(dd.java:668)
at com.ibm.db2.jcc.am.dd.a(dd.java:60)
at com.ibm.db2.jcc.am.dd.a(dd.java:127)
at com.ibm.db2.jcc.am.bn.c(bn.java:2546)
at com.ibm.db2.jcc.am.bn.a(bn.java:2053)
at com.ibm.db2.jcc.t4.cb.n(cb.java:802)
at com.ibm.db2.jcc.t4.cb.i(cb.java:259)
at com.ibm.db2.jcc.t4.cb.c(cb.java:54)
at com.ibm.db2.jcc.t4.q.c(q.java:44)
at com.ibm.db2.jcc.t4.rb.j(rb.java:147)
at com.ibm.db2.jcc.am.bn.ib(bn.java:2048)
at com.ibm.db2.jcc.am.cn.b(cn.java:3845)
at com.ibm.db2.jcc.am.cn.b(cn.java:3975)
at com.ibm.db2.jcc.am.cn.bc(cn.java:678)
at com.ibm.db2.jcc.am.cn.executeQuery(cn.java:652)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:643)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
How can I pass a string as a parameter to my sql where my string is of type 'abc','pqr','xyz'
Thanks for reading!
I suggest you use a better ORM like MyBatis.
Also it looks like you're passing a string parameter as a date.
Your solution to pass a list of strings to IN statement won't work either, your
"'abc','cde'" will be treated as a single string in the IN statement since all SQL characters like , are ignored in parameters of parametrized statements (it's the feature that prevents SQL injection).