Java - JDBC Thin to Oracle

tnowacoski
tnowacoski used Ask the Experts™
on
I found a simple Java program that connects to an Oracle database and returns the result of a simple select.  When I execute this the only console output I get is...What am I doing wrong?:

<Console>
testing oracle-character-set-1 against <abc>
    PASSED LOSSY
testing oracle-character-set-1 against <ab?c>
    PASSED LOSSY
testing oracle-character-set-1 against <XYZ>
    PASSED LOSSY
testing oracle-character-set-1 against <longlonglonglong...>
    PASSED LOSSY
testing oracle-character-set-31 against <abc>
    PASSED LOSSY
testing oracle-character-set-31 against <ab?c>
    PASSED LOSSY
testing oracle-character-set-31 against <XYZ>
    PASSED LOSSY
testing oracle-character-set-31 against <longlonglonglong...>
    PASSED LOSSY
testing oracle-character-set-870 against <abc>
    PASSED
testing oracle-character-set-870 against <ab?c>
    PASSED
testing oracle-character-set-870 against <XYZ>
    PASSED
testing oracle-character-set-870 against <longlonglonglong...>
    PASSED
testing oracle-character-set-871 against <abc>
    PASSED
testing oracle-character-set-871 against <ab?c>
    PASSED
testing oracle-character-set-871 against <XYZ>
    PASSED
testing oracle-character-set-871 against <longlonglonglong...>
    PASSED


<IDE>
 Eclipse 3.5

<External References>
ojdbc6.jar
<CODE>

package en.cmi.jdbccheckup;

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

import oracle.jdbc.OracleDriver;

public class TestOra {
    public TestOra() {
        System.setProperty("oracle.net.tns_admin","$ORACLE_HOME\\network\\admin");
    }

    public void doit () throws SQLException {
        String usr = "IFSAPP";
        String pwd = "*****"; //intentionally blanked
        String url = "jdbc:oracle:thin:@IFSTest";

        DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(url,usr,pwd);

        String sql = "select count(*) from customer_order_tab;" ;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
             System.out.println("results: " + rs.getString(1));
        conn.close();
    }

    public static void main(String[] args){
        TestOra test = new TestOra();
        try {
            test.doit();
            System.out.println("Done..");
        }
        catch (SQLException e) {
              System.out.println("Error");
            e.printStackTrace();
        }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try

System.out.println("results: " + rs.next().getString(1));

Author

Commented:
I get a compile error: Cannot invoke getString(int) on the primitive type boolean
try
rs.next().getInt(1)
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

sorry it should be

rs.next();
rs.getInt(1);

these 2 lines should be seperate

Author

Commented:
This is what I have now.....same results:

.
.
.
        DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(url,usr,pwd);

        String sql = "select count(*) from customer_order_tab;" ;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        rs.getInt(1);
        System.out.println("results: " + rs.getString(1));
        conn.close();
It should be

DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(url,usr,pwd);

        String sql = "select count(*) from customer_order_tab;" ;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        System.out.println("results: " +  rs.getInt(1););
        conn.close();

Author

Commented:
Actually, I just did a "Hello World" println and that didn't even show up:

package en.cmi.jdbccheckup;

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

import oracle.jdbc.OracleDriver;

public class TestOra {
    public TestOra() {
        System.setProperty("oracle.net.tns_admin","$ORACLE_HOME\\network\\admin");
    }

    public void doit () throws SQLException {
        String usr = "IFSAPP";
        String pwd = "wysox";
        String url = "jdbc:oracle:thin:@IFSTest";

        DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(url,usr,pwd);

        String sql = "select count(*) from customer_order_tab;" ;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        rs.getInt(1);
        System.out.println("Hello World");
        //System.out.println("results: " + rs.getString(0));
        conn.close();
    }

    public static void main(String[] args){
        TestOra test = new TestOra();
        try {
            test.doit();
            System.out.println("Done..");
        }
        catch (SQLException e) {
              System.out.println("Error");
            e.printStackTrace();
        }
    }
}
Top Expert 2016

Commented:
>>
rs.next();
        rs.getInt(1);
        System.out.println("Hello World");
        //System.out.println("results: " + rs.getString(0));
>>

should be
while (rs.next()) {
       System.out.println("results: " + rs.getInt(1));
}

Open in new window

Author

Commented:
It only returns on value? "Select count(*)..."

Author

Commented:
Maybe the more appropriate question should be, can I get sample code to make an Oracle connection and return a resultset with JDBC Thin?
Top Expert 2016

Commented:
>>It only returns on value? "Select count(*)..."

What do you mean by that?
Top Expert 2016

Commented:
Oh, you mean *one* value? Of course - select count only returns one value. If you want more, do select *

Author

Commented:
typo:  It only returns one value, no need to loop.  "Select count(*)"  returns a single row, single column.

Commented:
Hello tnowacoski,

From the results that you presented at first, it missed one critical output. The message "Done" or "Error"

From your code i assume that if your main method will run it will output either Done or Error. Could you please tell us what was the result?

From your result i suspect that you run some test methods of ojdbc.jar from some conformance test. In a real application, would never appear that kind of output. What you would have to expect was either the following two lines.

results:1234
Done

or anything else with one more line
Error

Top Expert 2016

Commented:
See

http://technojeeves.com/joomla/index.php/free/59-resultset-to-tablemodel

Although that shows the results being put into collections, you can adapt that just to print out the whole table

Author

Commented:
Statal:

I am not sure I understand your questions?  The original post showed all of the code and output.  Like I said further down the thread, I even replaced the Println with "Hello World" and that did not even show.  I am not sure where the "testing oracle-character..." messages are even coming from.

Author

Commented:
New Clues, I think this is an Eclipse thing.  I copied the code and created a new Java Project.  Now I get the following error.  Not sure what to do with it though:

Launch configuration CharacterSetFactoryThin references non-existing project OracleTest.
Got it!  It was an Eclipse issue with the Run Configuration.
<<Got it!  It was an Eclipse issue with the Run Configuration.>>
what was the issue and how did you resolved it?
Program:

package metadata;
import java.sql.*;
public class DemoMetaData {
      public static void main(String[] args) throws Exception
      {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","kumar","oracle");
            String str="select * from emp";
            PreparedStatement ps=c.prepareStatement(str);
            ResultSet rs=ps.executeQuery();
            ResultSetMetaData rsmd=rs.getMetaData();
            
            //boolean t=rs.next();// used for row Continuation
            int count=rsmd.getColumnCount();
            System.out.println("total number of columns: "+count);
            for(int i=1;i<=count;i++)
            {System.out.println(rsmd.getColumnName(i)+" and "+rsmd.getColumnTypeName(i));      
                  
            }
            ps.close();
            c.close();
      }

}

Output:

testing oracle-character-set-1 against <abc>
    PASSED LOSSY
testing oracle-character-set-1 against <ab?c>
    PASSED LOSSY
testing oracle-character-set-1 against <XYZ>
    PASSED LOSSY
testing oracle-character-set-1 against <longlonglonglong...>
    PASSED LOSSY
testing oracle-character-set-31 against <abc>
    PASSED LOSSY
testing oracle-character-set-31 against <ab?c>
    PASSED LOSSY
testing oracle-character-set-31 against <XYZ>
    PASSED LOSSY
testing oracle-character-set-31 against <longlonglonglong...>
    PASSED LOSSY
testing oracle-character-set-870 against <abc>
    PASSED
testing oracle-character-set-870 against <ab?c>
    PASSED
testing oracle-character-set-870 against <XYZ>
    PASSED
testing oracle-character-set-870 against <longlonglonglong...>
    PASSED
testing oracle-character-set-871 against <abc>
    PASSED
testing oracle-character-set-871 against <ab?c>
    PASSED
testing oracle-character-set-871 against <XYZ>
    PASSED
testing oracle-character-set-871 against <longlonglonglong...>
    PASSED



why it happens?? tell me please the solution...
related program is working correctly but why this program give error?
if the error in ecllipse then related programs also got affected and gave the same error...


Do you have any suggestion to solve it?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial