Java LogoRecently, I was asked by a reader to help him write a simple application that connects to a database using JDBC and query a table. It only took me a few minutes to come up with the code and the reader found it to be exactly what he was looking for. Since he found the code useful (and because there’s nothing proprietary about it), I thought I would share it with anyone else looking to do something similar. The code is very short and pretty much self explanatory. So, I don’t think there’s much need for an explanation of what’s going on in it. However, one thing I will point out is that the code below uses the MySQL JDBC driver which you will need to obtain before using the code. I have uploaded the driver I used in the example application to http://www.prodigyproductionsllc.com/downloads/mysql-connector-java-5.1.6-bin.jar if you would like to use it for testing. Also, below the source code I have added some commands you can use to compile and run the application from the command line. And, as always, if you have any questions, feel free to leave them in the comments below in case anyone else might have the same questions or would like to offer up suggestions of their own.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTest {
        public static void main(String[] args) throws SQLException {
                Connection conn = null;
                Statement stmt = null;
                ResultSet rs = null;
				
                String url = "jdbc:mysql://localhost:3306/";
                String dbName = "testdb";
                String driver = "com.mysql.jdbc.Driver";
                String userName = "root";
                String password = "root";
                try {
                        Class.forName(driver);
                        conn = DriverManager.getConnection(url + dbName, userName, password);
                        System.out.println("Connected to the database");
                        
                        stmt = conn.createStatement();
                        
                        String query = "SELECT id, name FROM users";
                        rs = stmt.executeQuery(query);
                        while (rs.next()) {
                            System.out.println("ID: " + rs.getInt("id") + " - Name: " + rs.getString("name"));
                        }
                } catch (ClassNotFoundException e) {
                        System.out.println("Class Not found Exception cought");
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        conn.close();
                        stmt.close();
                        rs.close();
                        System.out.println("Disconnected from database");
                }
        }
}

To compile the application, launch a command prompt and run the following command:
# javac JDBCTest.java

To run the application, launch a command prompt and run the following command (be sure to substitute in the driver you are using):
# java -cp .;mysql-connector-java-5.1.6-bin.jar; JDBCTest

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply