• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help handling SQL Exceptions - duplicate data  RSS feed

 
Bob Hoskins
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone!

I'm a newbie with Java....


I'm reading a list of commands from a  text file into a MySQL database. They all work

The issue is, if I modify the text file I want java to cycle through all the commands and avoid duplicate data.

The easiest way of me doing this is simply to rely on the MySQL database. Make the fields "unique constrained" and MySQL will reply with an SQLException if there's any duplicate data.

My issue is handling this SQLException. I want the loop to continue through the other commands in the text file if the SQLException is given.

I have a Main Class which calls a InputFile Class, which in turn calls a MySQL_Connector Class.

The SQLException happens in the MySQL Connector Class, but for some reason it's being handled in the Main Class.














 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the logs you are getting from this?
That is, the output from those println calls.

Once thing, you will leak connections when you have exceptions as the connection isn't closed, either in a finally block or (preferably) using a try-with-resources structure.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your DatabaseConnection() method doesn't throw an SQLException.  Try this:
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the CodeRanch!  A few notes about your post...

First, thank you for using code tags.  you should also include any import statements and correct your formatting and indentation.

Some other notes about your coding style: a method name should always start with a lowercase letter.  Don't use underscores in your identifiers (class names, method name, variable name, etc.)  The only exception is if you create "constants", which are final and static:
The continue in the for-loop doesn't do anything.  It continues to the next x, which it would do anyway.

You don't need the Class.forName("com.mysql.jdbc.Driver") line; modern JDBC drivers don't require it.
 
Carey Brown
Saloon Keeper
Posts: 3324
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If each line consists of 3 pieces of data then there's no need for the for() loop. You can just use...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your code is "eating" the exception:
it's impossible to know what the actual problem is. Your string says it's a duplicate issue, but how do you know? You should be, at minimum, logging the actual exception message and its stack trace. And, showing it to us to get help.

So instead of hard-coding a message that could very well be bogus, use e.getMessage() and other exception methods that give you the actual info of the exception.

Bob Hoskins wrote:but for some reason it's being handled in the Main Class.

Well, yeah. That is, after all, where you put the handler.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!