java Simple Java JDBC Program To Execute SQL Select Query

Below is a sample Java JDBC program which takes database connection string as command line arguments and returns the SQL select query output


Replace the SQL Select query in the below code with the desired select query and modify the variables depending upon the result you wish to display in the output.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Run_SQL_Select {
    static Connection sqlConn = null;
    static Statement sqlStmt = null;
    static ResultSet sqlResultset = null;
    public static void main(String[] args) throws SQLException {
        try {
        } catch (ClassNotFoundException exception) {
            System.out.println("Oracle Driver Class Not found Exception: " + exception.toString());
        try {
            String db_host=args[0];
            String db_serviceName=args[1];
            String db_username=args[2];
            String db_password=args[3];
            String jdbc_conn_string="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST="+db_host+")(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME="+db_serviceName+")))";
            sqlConn = DriverManager.getConnection(jdbc_conn_string, db_username, db_password);
        catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
        sqlStmt = sqlConn.createStatement();
        sqlResultset = sqlStmt.executeQuery("SELECT ABC AS COLUMN1, PQR AS COLUMN2, XYZ AS COLUMN3 from SAMPLE_TABLE");

         System.out.println("COLUMN1_Title COLUMN2_Title COLUMN3_Title");

         String var1 = sqlResultset.getString("COLUMN1");
         String var2 = sqlResultset.getString("COLUMN2");
         String var3 = sqlResultset.getString("COLUMN3");      
         System.out.println(var1 + " " + var2 + " " + var3);

Copy paste the above code in a file having same name as the class name.


Replace the SQL select query with the query of your choice.


Compile the code:


Execute the code with the class name:

java Run_SQL_Select ${DB_HOST} ${DB_SID} ${DB_USER} ${DB_PASSWORD}
0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Display basic information about Physical Volumes
Viewed 3126 times since Sun, Jun 3, 2018
Transform XML to CSV Format | Unix String Pattern Manipulation The Ugly Way
Viewed 4750 times since Sun, Jan 9, 2022
Convert JSON to CSV with bash script
Viewed 11993 times since Mon, Jan 20, 2020
To do a quick check on the number of path present (does not mean all are Enabled] using for loop
Viewed 3579 times since Fri, Jun 8, 2018
java Simple Java JDBC Program To Execute SQL Update Statement
Viewed 11768 times since Sun, Jan 9, 2022
Unix - Examples for grep command
Viewed 2784 times since Fri, Jun 8, 2018
Convert CSV to JSON with jq
Viewed 26737 times since Mon, Jan 20, 2020
Convert CSV to JSON with bash script
Viewed 4895 times since Mon, Jan 20, 2020
Epoch & Unix Timestamp Conversion Tools
Viewed 46586 times since Fri, Jun 22, 2018
O’Reilly’s CD bookshelf
Viewed 11684 times since Wed, Jun 27, 2018