Today is Monday, 18th March 2019

Archive for the ‘MySQL’ Category


Java JDBC Tutorial – Inserting Data with User Input

In this blog post, I’ll show you how to insert data into the database with user input. When you run the program, it will prompt you to enter your last name, first name and email address. Here’s a sample run of the app:

Enter your last name: Davis

Enter your first name: Sammy

Enter your email: sammy@test.com

Insert complete.

To accomplish this, you’ll make use of the Scanner class that is defined in the java.util package. This class can read input as Strings and Java primitives. In this example, we’ll just use it to read in the last name, first name and email. Here’s the code snippet:

Now, we can use this in our JDBC code to insert data supplied by the user. We could just perform a String concatenation with the values, but that would open us up to SQL injection attacks. Instead, we’ll make use of Prepared Statements that is explained in detail in another blog post here: Java JDBC Prepared Statements.

So here’s the code for setting up the prepared statement and setting the input values from the user.

That’s it. Those are the steps for inserting data into the database with user input.

Download Source Code



Java JDBC Tutorial – Inserting Date and Time

In this blog post, I’ll show you how to insert a date and time value into the database.

The date and time is collectively known as a timestamp. JDBC has builtin support for saving and reading timestamps. This support is provided by java.sql.Timestamp.

Let’s assume we have this database table schema below. Make note of the timestamp column called checkout_time.

Saving a Timestamp

You can save a timestamp value with the following code:

Reading a Timestamp

Likewise, you can read a timestamp value with the following code:



Java JDBC Tutorial – Part 12.x: Creating Runnable JAR file with Eclipse

In this blog post, I’ll show you how to create an runnable JAR file. This will use the code from video 12.10.

A runnable JAR file allows you to have a single JAR file that includes all Java class files and supporting libraries. In our case, this includes the JAR files for the MySQL JDBC driver (client api code only) and the JAR file for the GUI library JGoodies. Once the JAR file is created, then you’ll be able to double-click the JAR file in the file manager and the application will start.

Follow these steps.

  1. Download the source code from video 12.10 from this link
  2. Unzip the zip file to a directory on your computer. I’ll refer to this directory later on as as ${jdbc-demo-code}
  3. In Eclipse, import the code using:
    1. Select File > Import > General > Existing Projects into Workspace
    2. Select root directory: Browse to the directory where you unzipped the code: ${jdbc-demo-code}
    3. Click Finish
  4. Run your project first to make sure it works
    1. Open the file: EmployeeeSearchApp.java in the package: com.luv2code.jdbc.employeesearch.ui
    2. Right click in the file and select Run As > Java Application
  5. Create a new folder under your current project
    1. Select the root folder of your project: jdbc-swing-gui-check-password
    2. Right click and select New > Folder
    3. For folder name, enter: target
    4. Click Finish.
  6. Copy the props file to the target folder
    1. Select the demo.properties file and copy it to the folder: target
    2. Note: Your JAR file will use this properties file
  7. Export to a Runnable JAR file
    1. Select the root folder of your project: jdbc-swing-gui-check-password
    2. Right click and select Export…
    3. Select Java > Runnable JAR file
    4. Click Next
  8. Set the target options
    1. For Launch configuration, choose the one that says: EmployeeSearchApp …
    2. For Export destination, click the Browse… button
    3. Select the target directory you created in a previous step
    4. For the file name Save As, enter: jdbc-demo-app.jar
    5. For Library handling, select: Extract required libraries into generated JAR
    6. Click Finish
    7. Note: the export will finish with warnings. You can safely ignore them.
  9. If everything worked properly then you will have the following directory structure:
    • jdbc-swing-gui-check-password/target
      • jdbc-demo-app.jar
      • demo.properties
  10. You can run the program using your Windows Explorer or Mac Finder.
    • Move to the directory where your jdbc-demo-app.jar is located
    • Double-click the file and it should run and connect to your database.

* Troubleshooting.

  1. If you have problems then you should run from command-line to see details of the stacktrace.
  2. Open a command-line window and navigate to where the jdbc-demo-app.jar is located
  3. Run from command-line with: java -jar jdbc-demo-app.jar
  4. View the stacktrace and it will tell you what the problem is.