• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Help handling SQL Exceptions - duplicate data  RSS feed

 
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.














 
Master Rancher
Posts: 4076
47
  • 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.
 
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu 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
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu 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.
 
Bartender
Posts: 5838
56
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...
 
Sheriff
Posts: 67259
170
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.
 
Men call me Jim. Women look past me to this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!