Showing posts with label SQLException. Show all posts
Showing posts with label SQLException. Show all posts

Wednesday, January 22, 2014

SQL0803N / SQLSTATE=23505

Error during Execute
 23505(-803)[IBM][CLI Driver][DB2] SQL0803N  One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "" constrains table "" from having duplicate values for the index key.  SQLSTATE=23505



Probably you are trying to insert a value in primary key / unique key which is already present and thus violating primary key, unique key constraints. Please make sure you are not entering duplicate values in primary key / unique key.

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.

Friday, November 11, 2011

java.sql.SQLException: No suitable driver found

If you get below mentioned error

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:db2://localhost:3306/Test
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)


There could be two possible reasons.

1. Correct driver is not loaded, If this is the case make sure you pass correct Driver for getConnection() method.

2. URL passed is not properly constructed, If DriverManager do not understands the URL sent it throws above error.

In my case this was the issue, It was working well in my local machine but when I deploy the code on my production server (linux server) it used to throw above error. It was working on local machine because IBM client was installed on it.

You can verify on your local machine, probably on below mentioned path
C:\Program Files\IBM\SQLLIB\java

I was using App JDBC Driver :

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
con=DriverManager.getConnection("jdbc:db2:Test","uername","passowrd");

Jar file : db2java.jar

Here local machine is able to understand alias "Test" because IBM client was installed as mentioned above. But server was not able to understand the alias hence it was throwing above error.

I solved the problem by using DB2 Universal Driver

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

Jar file : db2jcc.jar


To download Jar files : Click here


Aslo there is one more solution for this.

Possibly there could be IBMdb2 installed on your server / local machine but while running program it is not able get the driver.

Solution is load the profile, in a .bat or .sh file as shown below.


#!/bin/ksh -x
. /apps/IBMdb2/db2v8/sqllib/db2profile 

java -classpath db2java.jar:. TestDB2

Here TestDB2 is .class file of JAVA program.

Thursday, October 13, 2011

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

if you get below mentioned exceptions while connecting My SQL using JDBC, Just make sure that password and other login credentials are provided correctly.

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894)
        at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3808)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1256)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
        at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:729)
        at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)


Solution :

Just make sure that password you provide is correct, Cross check the password you have provided.

Below is the simple JDBC test program, To run this program make sure you add Jar file like mysql-connector-java-5.1.5.jar to classpath.

Make sure you provide all correct login credentials in Step 2. i.e. port, database name and password.
/* Test.java */

import java.sql.*;
public class Test
{
public static void main(String[] args) throws Exception
{
Connection con;
Statement stat;
Class.forName("com.mysql.jdbc.Driver");//Step 1: Loading Drivers

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Test","root","password");//Step 2: Making Connection

stat=con.createStatement();//Step 3: Creating JDBC Statement

String query = "select * from table_name;";

ResultSet rset=stat.executeQuery(query);//Step 4: Execute the Ststement

while(rset.next())//Step 5: Looping through the ResultSet
{
    System.out.println(rset.getString(1));
}
stat.close();//step 6: Close the Connection and Statement
con.close();
}
}