java

recent posts related to java, jdbc, spring, etc.

A simple Java JDBC example that shows SQL insert, update, delete, and select alvin August 10, 2016 - 10:53am

Here’s a short Java/JDBC example program where I show how to perform SQL SELECT, INSERT, UPDATE, and DELETE statements with JDBC:

How to determine the current monitor size with multiple displays - Java Swing, JavaFX alvin February 9, 2016 - 10:53am

Note: This code is currently a work in progress. I know of possible approaches, but I don’t know of a perfect working solution yet.

I’m currently trying to find the right way to find the current monitor size, when you’re writing a Java Swing application to work in a multiple-monitor configuration. I always use three monitors, so I can test this pretty easily.

A Java FileFilter example for image files alvin February 8, 2016 - 7:13pm

If you need some source code for a Java FileFilter for image files, this code can get you started:

A Java TimerTask, Timer, and scheduleAtFixedRate example alvin February 8, 2016 - 7:02pm

As a quick note, if you ever need to use a Java TimerTask, you can define one like this:

class BrightnessTimerTask extends TimerTask {
    @Override
    public void run() {
        // your custom code here ...
    }        
}

and you can then instantiate it, create a Timer, and schedule the task like this:

How to cast a null value in Java alvin February 4, 2016 - 2:12pm

Until a little while ago I don’t think I had ever thought about intentionally casting a null value in Java, but then I ran into a problem and realized that the solution was to cast a null value, like this:

FileDialog d = new FileDialog((java.awt.Frame) null);

You have to do that in this case because FileDialog has several one-argument constructors, including one that takes a JFrame and another that takes a JDialog. If you just put null in the constructor the Java compiler or your favorite IDE will complain, so you have to cast the null value to one of those specific types, and this syntax shows how to do this. (My app uses multiple frames, and at the moment I’d rather put null in the FileDialog constructor than try to determine which frame is currently in the foreground.)

Byte Buddy - runtime code generation for the JVM alvin January 19, 2016 - 10:50am

Byte Buddy is a code generation library for creating Java classes during the runtime of a Java application and without the help of a compiler.” Cool.

A Java FIFO queue class alvin January 18, 2016 - 6:18pm

In my spare time lately I’ve been writing this Android football game, and for the game I needed a simple FIFO queue. I haven’t used Java much lately, and after a quick Google search showed things that were more complicated than what I had in mind, I wrote my own FIFO queue.

A Java tuple class alvin January 18, 2016 - 3:35pm

After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple classes.

If you haven’t used them before, a Scala Tuple class lets you write code like this:

A Java method that converts seconds to minutes and seconds alvin January 17, 2016 - 12:48pm

If you happen to need something like this, here’s some source code for a Java method that converts seconds in time to a String formatted as minutes and seconds:

A Java method to calculate the NFL Passer Rating alvin January 16, 2016 - 3:58pm

Note: The code below is based on the Wikipedia formula, which is not correct. The correct algorithm seems to be at this page. I’ll update the code below once I verify this for sure.

In working on my “XO Play” Android football game, I just created this Java method to calculate the NFL Passer Rating for my quarterbacks: