How to catch specific exceptions in JDBC? Examples: primary key exception or foreign key exception. Answer SQLException contains some database-specific info related to the exception. From the doc: Each SQLException provides several kinds of information: 1) a string describing the error. This is used as the Ja…
What causes java.lang.IncompatibleClassChangeError?
I’m packaging a Java library as a JAR, and it’s throwing many java.lang.IncompatibleClassChangeErrors when I try to invoke methods from it. These errors seem to appear at random. What kinds of problems could be causing this error? Answer This means that you have made some incompatible binary chang…
How to find second largest number using Scanner and for loop(no array)
So I can easily accomplish task to find largest number and then if can be divided by three, print out. But do not know how to find second largest number from users sequence. Thanks for any hints! Answer
Pick a random value from an enum?
If I have an enum like this: What is the best way to pick one randomly? It doesn’t need to be production quality bulletproof, but a fairly even distribution would be nice. I could do something like this But is there a better way? I feel like this is something that’s been solved before. Answer The …
Connect to remote MySQL database through SSH using Java
How can I connect to remote MySQL database through SSH from java application? Small code example is helpful for me and I’d appreciate this. Answer My understanding is that you want to access a mysql server running on a remote machine and listening on let’s say port 3306 through a SSH tunnel. To cr…
Is there a fixed sized queue which removes excessive elements?
I need a queue with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element. Is there an existing implementation for this in Java? Answer There is no existing implementation in the Java Language and Runtime. All Queues extend AbstractQueue, and its doc clea…
Listen to a shoutcast with Android
since quite some time I’m trying to listen to .pls files (shoutcasts). I have to say that I failed horrible. Since StreamFurious can do it it must be possible. First I tried to connect to the shoutcast via sockets (TCP and UDP) –> failed. I couldn’t even receive one byte from the server. …
Eclipse debugging “source not found”
I just started using Eclipse so go easy on me ;). But when trying to debug a JUnit test case I get a dialog that states the the source is not found when I get to this line in the code in my test method: I know I should probably go and try and download the source from somewhere, but
What is Eclipse’s Ctrl+O (Show Outline) shortcut equivalent in IntelliJ IDEA?
I like to use Eclipse’s shortcut Ctrl + O which outlines the current source. Is there an equivalent shortcut in IntelliJ IDEA? It opens a dialog which allows for quick search of methods and fields in …
Get generic type of java.util.List
I have; List stringList = new ArrayList(); List integerList = new ArrayList(); Is there a (easy) way to retrieve the generic type of the …