This is a simple JDBC example that shows you how to run a program from command-line. A Java IDE is not required.
In general, when you are using JDBC from command-line, you have to make sure you have the JDBC driver in your classpath. For example, if i downloaded the MySQL JDBC driver to c:\foobar\mysql-connector-java-5.1.29-bin.jar. Then you can set the classpath with the following command in the MS-DOS window
Then from there you can compile and run your program.
Below, I provide step-by-step instructions on how to run a simple JDBC demo from command-line.
Step 0. Download the files and extract to a directory on your computer
– Download simple-jdbc,zip
Step 1. Create table and load sample data
The first thing you have to do is create the database table and load sample data.
Using your MySQL tool, execute the SQL file: data.sql
This script does the following
– Creates a database schema: demo
– Creates a table: employees
– Inserts three rows of sample data
Step 2: Review Driver.java and the database connection information
– The program attempts to connect to the demo database running on localhost
— You may need to adjust this for your local environment
– The program make use of databse user id “student” and password “student”
– You may need to adjust this for your local environment
Step 3. Compile the program
In a command-line window, type:
Step 4. Run the program
— (If you are using Linux: run.sh is available)
The batch file will set the classpath for the MySQL driver and then execute the program.
Step 5: Confirm the results
The program will display the following output