Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

writing my own Exception for SQLException and IOException

807591Apr 22 2008 — edited Apr 23 2008
hi all,
i have been trying to write my own exception by extending SQLException and IOException but i dont know what i am doing wrong ,i have tried many ways but its not working for me ..please have a look at my code specially the section putData method
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.NoSuchElementException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
/**class to read CSV file :
 * @author Ashish K
 */
public class FileParser
{ //readCSV class starts here 
	
	public static void main(String args[]) 
	{ //main method starts
		String fName[]=new String[10];
		String choice[]=new String[10];
		String Delim[]=new String[10];
		ResourceBundle a = ResourceBundle.getBundle("input");
		String n=a.getString("NOF");
		try
		{
			for(int i=0;i<Integer.parseInt(n);i++)
			{
				fName=a.getString("PATH"+(i+1));
choice[i]=a.getString("HEADER"+(i+1));
Delim[i]=a.getString("DELIMITER"+(i+1));
putData(fName[i],choice[i],Delim[i]);//to put data in database
}
}
catch (NumberFormatException e)
{
System.out.println("Number of files are invalid");
}
String show=a.getString("CHECKRECORDS");
if(show.equalsIgnoreCase("yes"))
{
showData(); //to show data in database
}
} //main method ends


//method to feed DATA into database :
public static void putData(String fName,String choice,String Delim) //throws DataCollisionException
{
Connection conn = null;
PreparedStatement ps=null;
int i=1; //variable used. for batch insert and commit
try
{
FileInputStream fis = new FileInputStream(fName);
//A FileInputStream obtains input bytes from a file in a file system
DataInputStream myInput = new DataInputStream(fis);
/*data input stream lets an application read primitive Java data types
from an underlying input stream in a machine-independent way*/
conn=ConnSingleton.getConnection();
conn.setAutoCommit(false);
int field1;
String thisLine,field2;
ps=conn.prepareStatement("INSERT INTO (SELECT EMP_NO , EMP_NAME FROM EMPDATA WHERE EMP_NO=?) VALUES(?,?)");
while ((thisLine = myInput.readLine()) != null)
{
//int flag =0;
StringTokenizer st =new StringTokenizer(thisLine,Delim);
if(choice.equalsIgnoreCase("yes"))//this statement will exclude header if any
{
choice="No";
continue;
}
try //to ignore escape sequences
{
field1=Integer.parseInt(st.nextToken().trim());
field2 = st.nextToken();
field2=field2.trim();
}
catch (NumberFormatException e)
{
System.out.println("Field is Invalid");
continue;
}
catch (NoSuchElementException e)
{
System.out.println("Field Not Found");
continue;
}
ps.setInt(1,field1);// Assign value to input parameter
ps.setInt(2,field1);
ps.setString(3,field2);
ps.addBatch();
// submit the batch for execution
if(i==500){
try
{
ps.executeBatch();
}
catch (SQLException b)
{
System.out.println("Field value matched ,unable to Feed");
}
conn.commit();
}
}
if(i<500)
{
// submit the batch for execution
try
{
ps.executeBatch();
}
catch (SQLException b)
{
System.out.println("Field value matched ,unable to Feed");
}
conn.commit();
}
}
catch(SQLException e)
{
e.printStackTrace();
try
{
conn.rollback();
}
catch (SQLException a)
{
System.out.println("Can't Rollback");
}
}
catch(IOException e)
{
System.out.println("File Not Found");
}
finally
{
try
{
if(ps!=null)
{
System.out.println("Closing Prepared Statement...");
ps.close();
System.out.println("Closed");
}
if(conn!=null)
{
System.out.println("Closing with Database... \nClosed");
conn.close();
}
}
catch(SQLException e){
System.out.println("Closing Failed");
}
}
}
// method to show DATA from a database :
public static void showData()
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet r=null;
try{
conn=ConnSingleton.getConnection();
ps = conn.prepareStatement("SELECT * FROM EMPDATA");
r = ps.executeQuery();
r=ps.getResultSet();
while(r.next())
{
System.out.println(r.getString("EMP_NO")+"\t|\t"+r.getString("EMP_NAME"));
}

}
catch(SQLException e){
e.printStackTrace();
}
finally
{
try
{
if(r!=null)
{
System.out.println("Closing Result Set...");
r.close();
System.out.println("Closed");
}
if(ps!=null)
{
System.out.println("Closing Prepared Statement...");
ps.close();
System.out.println("Closed");
}
if(conn!=null)
{
System.out.println("Closing with Database... \nClosed");
conn.close();
}
}
catch(SQLException e){
System.out.println("Closing Failed");
}
}
}
}
i have written my own exception like this :
import java.sql.SQLException;
public class DuplicateValueException extends SQLException{
public DuplicateValueException(){

}
public DuplicateValueException(String s){
System.out.println(s);
}
}
and replaced the SQLException which was :
try
{
ps.executeBatch();
}
catch (SQLException b)
{
System.out.println("Field value matched ,unable to Feed");
}
to this : 
try
{
ps.executeBatch();
}
catch (DuplicateValueException b)
{
b.toString();
}
but its not working and i am not figuring out why ....i am not that good in java so you can expect some stupid or silly mistake too.Please tell me whats wrong in the code .Thanks :)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 20 2008
Added on Apr 22 2008
9 comments
759 views