Showing posts with label SQLCODE. Show all posts
Showing posts with label SQLCODE. Show all posts

Tuesday, January 21, 2014

A parent row cannot be deleted because the relationship "R_1" restricts the deletion

Error during Execute
 23504(-532)[IBM][CLI Driver][DB2] SQL0532N  A parent row cannot be deleted because the relationship "R_1" restricts the deletion.  SQLSTATE=23504


You are trying to delete a specified row in the parent table which has dependencies on child table(s). First delete the record in the child table(s) and then try to delete the record in the parent table.

Monday, December 24, 2012

JDBC Example – Batch Update using PreparedStatement


Using Batch Update feature of PreparedStatement you can Insert, Update or Delete multiple records with single database hit. For example if you want to insert multiple records into database using Batch Update you will just need to conect to database once. You can loop through records and add it to bach and once done you can execute the bach i.e insert all records in a single shot.


Below is a code snippet which shows how to insert few records in batch process, via JDBC PreparedStatement

PreparedStatement ps=con.prepareStatement("INSERT INTO BS_Books (cat_id,book_title,book_details,book_price,book_author) values (?,?,?,?,?)");

ps.setInt(1, 1);
ps.setString(2, "Book1");
ps.setString(3, "Details of book1");
ps.setDouble(4, 100.0);
ps.setString(5, "Author1");
ps.addBatch();//Adding record to the batch

ps.setInt(1, 1);
ps.setString(2, "Book2");
ps.setString(3, "Details of book2");
ps.setDouble(4, 150.0);
ps.setString(5, "Author2");
ps.addBatch();//Adding record to the batch

ps.executeBatch();//Executing the batch

Please see the complete self explanatory java code below


Hibernate Examples - Native SQL Queries


You can also express queries in the native SQL dialect of your database. This is useful if you want to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. It also provides a clean migration path from a direct SQL/JDBC based application to Hibernate.

Hibernate3 allows you to specify handwritten SQL, including stored procedures, for all create, update, delete, and load operations.

To know more, click here.

For example - we can have methods in DAO for executing Native SQL as shown below.

public List executeNativeQuery(String query) {
return getSession().createSQLQuery(query).list();
}

public List executeQuery(String query) {
return getSession().createQuery(query).list();
}

You can pass native SQL query as shown below -

List allObjects = empDao.executeNativeQuery("Select emp_id, emp_name  from EMPLOYEE");

Iterator it = allObjects.iterator();
while(it.hasNext())
{
Object row[] = (Object[])it.next();
for(Object eachRow : row) {
System.out.println(eachRow.toString());
}
}

Below are the known errors and exception -

1. Exception in thread "main" org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
at org.hibernate.dialect.TypeNames.get(TypeNames.java:79)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:104)
at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:393)
at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:582)
at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:508)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:524)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1821)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1723)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)


Solution - Above exception occurs if you pass * to fetch all columns in SQL like - "Select * from EMPLOYEE". this should be avoided.


Thursday, December 15, 2011

CLI0611E Invalid column name. SQLSTATE=S0022

COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0611E  Invalid column name. SQLSTATE=S0022
    at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.rsException(Unknown Source)
    at COM.ibm.db2.jdbc.app.DB2ResultSet.findColumn(Unknown Source)
    at COM.ibm.db2.jdbc.app.DB2ResultSet.getString(Unknown Source)

As the get above error suggest, The column name specified in the Query or in resultSet is invalid.

resultSet.getString("COLUMN_NAME");

Please re-check all the column names against table name and make sure it is correct.

Friday, December 9, 2011

SQL0206N, SQL0180N & SQL0408N

I observed below mentioned error while working with DB2 query.

Error :
 S0022(-206)[IBM][CLI Driver][DB2/SUN64] SQL0206N  "2" is not valid in the context where it is used.  SQLSTATE=42703
 (0.58 secs)

Solution :
This error occurs when u try to compare OR assign values of different datatypes. For example COUNT = '2' will throw above error because COUNT is INT and you are trying to assing STRING to it. To avoid above error make sure datatypes are correct.

Error :

  22007(-180)[IBM][CLI Driver][DB2/SUN64] SQL0180N  The syntax of the string representation of a datetime value is incorrect.  SQLSTATE=22007
 (0.73 secs)

Solution :

This error occurs when working with DATETIME datatypes. Usually when assigning or comparing STRING to a DATETIME value.

If you need to compare / assign a STRING to DATETIME use below syntax.

DATETIME_VARIABLE = TIMESTAMP ('2011-12-08-15.49.26.000625')

Error :
  22005(-408)[IBM][CLI Driver][DB2/SUN64] SQL0408N  A value is not compatible with the data type of its assignment target.  Target name is "DATETIME_VARIABLE".  SQLSTATE=42821
 (0.48 secs)


To convert a character string to a date or time value, you can use:

TIMESTAMP ('2002-10-20 12:00:00')
DATE ('2002-10-20')
DATE ('10/20/2002')
TIME ('12:00:00')
TIME ('12.00.00')

The TIMESTAMP(), DATE() and TIME() functions accept many other formats.

Wednesday, December 7, 2011

SQLSTATE=42501

If you get below mentioned error for DB2

42501(-551)[IBM][CLI Driver][DB2/SUN64] SQL0551N  "USER" does not have the privilege to perform operation "SELECT" on object "SCHEMA.TABLE_NAME".  SQLSTATE=42501 (0.48 secs)

As the error suggest your SQL administrator has not given privilege to perform SELECT operation on the particular table / View. Contact your SQL administrator and ask to grant privilege to perform operations on the table.

Invalid SQL syntax. SQLSTATE=37000

If you get below mentioned error for DB2

37000(-99999)[IBM][CLI Driver] CLI0118E  Invalid SQL syntax. SQLSTATE=37000 (0.03 secs)

As the error suggest, There could be a syntax error in the SQL. Please make sure that all the syntax for the SQL query is correct. Cross check for the syntax error.

Friday, November 25, 2011

SQLSTATE=42903

 I was in need to pass a max value in where condition. My folly, what i did is :

Incorrect : Select * from table_name where field = MAX(field);




Got below mentioned error in DB2

  42903(-120)[IBM][CLI Driver][DB2/SUN64] SQL0120N  Invalid use of an aggregate function or OLAP function.  SQLSTATE=42903
 (0.47 secs)

Correct :

Correct way is Inline Query

Select * from table_name where files = (Select MAX(filed) from table_name);

Tuesday, November 15, 2011

DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704

If you get below mentioned error


Exception in thread "main" com.ibm.db2.jcc.b.eo: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=SCHEMA.TABLE_NAME, DRIVER=3.53.95
        at com.ibm.db2.jcc.b.bd.a(bd.java:676)
        at com.ibm.db2.jcc.b.bd.a(bd.java:60)
        at com.ibm.db2.jcc.b.bd.a(bd.java:127)
        at com.ibm.db2.jcc.b.gm.c(gm.java:2484)
        at com.ibm.db2.jcc.b.gm.d(gm.java:2461)
        at com.ibm.db2.jcc.b.gm.a(gm.java:1962)
        at com.ibm.db2.jcc.t4.db.g(db.java:138)
        at com.ibm.db2.jcc.t4.db.a(db.java:38)
        at com.ibm.db2.jcc.t4.t.a(t.java:32)
        at com.ibm.db2.jcc.t4.sb.h(sb.java:141)
        at com.ibm.db2.jcc.b.gm.bb(gm.java:1933)
        at com.ibm.db2.jcc.b.gm.a(gm.java:2799)
        at com.ibm.db2.jcc.b.gm.a(gm.java:604)
        at com.ibm.db2.jcc.b.gm.executeQuery(gm.java:588)

Possible reason could be Table do not present in that schema. Please make sure that you provide correct schema name and table name. Also check for typo in Table name or Schema name.

I got the above error when i mistakenly passed wrong table name for the schema. Hope it resolved your error.

DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;

If you get below mentioned error


Exception in thread "main" com.ibm.db2.jcc.b.eo: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;SCHEMA.TABLE_NAME;END-OF-STATEMENT, DRIVER=3.53.95
        at com.ibm.db2.jcc.b.bd.a(bd.java:676)
        at com.ibm.db2.jcc.b.bd.a(bd.java:60)
        at com.ibm.db2.jcc.b.bd.a(bd.java:127)
        at com.ibm.db2.jcc.b.gm.c(gm.java:2484)
        at com.ibm.db2.jcc.b.gm.d(gm.java:2461)
        at com.ibm.db2.jcc.b.gm.a(gm.java:1962)
        at com.ibm.db2.jcc.t4.db.g(db.java:138)
        at com.ibm.db2.jcc.t4.db.a(db.java:38)
        at com.ibm.db2.jcc.t4.t.a(t.java:32)
        at com.ibm.db2.jcc.t4.sb.h(sb.java:141)
        at com.ibm.db2.jcc.b.gm.bb(gm.java:1933)
        at com.ibm.db2.jcc.b.gm.a(gm.java:2799)
        at com.ibm.db2.jcc.b.gm.a(gm.java:604)
        at com.ibm.db2.jcc.b.gm.executeQuery(gm.java:588)




Possible reason could be SYNTAX error. Please make sure all syntax are correct upto DB2 standard. Verify the syntax of your query. Even don't add  semi-colon (;) at the end of the query.

In this case if you are using DB2 Universal Driver. 

Syntax :

Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection con=DriverManager.getConnection("jdbc:db2://hostname:port/Schema","username","password");


Jar file : db2jcc.jar

To download Jar files : Click here

Friday, November 11, 2011

SQL0204N "SCHEMA.TABLE_NAME" is an undefined name

If you get below mentioned error

Exception in thread "main" COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/SUN64] SQL0204N  "SCHEMA.TABLE_NAME" is an undefined name.  SQLSTATE=42704
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(UnknownSource)
        at COM.ibm.db2.jdbc.app.DB2Statement.execute2(Unknown Source)
        at COM.ibm.db2.jdbc.app.DB2Statement.executeQuery(Unknown Source)
        at TestDB2.main(TestDB2.java:18)


As the error suggest, Possible reason could be Table name you are trying to access is not valid for that particular schema. Please check for proper Schema and Table name combination.