Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL + Java

  • 25-03-2003 06:18PM
    #1
    Closed Accounts Posts: 7,230 ✭✭✭


    Here's the code to my program:
    <start>
    import java.io.*;
    import java.net.*;
    import java.sql.*;
    import java.util.*;

    public class feh {

    private static BufferedReader input = new BufferedReader
    (new InputStreamReader(System.in));


    public static void main(String argv[]) throws Exception {


    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    String url = "jdbc:odbc:oracle";
    String username, password, sqlQuery;

    System.out.print("\nEnter username: ");
    username = input.readLine();
    System.out.print("\nEnter Password: ");
    password = input.readLine();
    System.out.print("\nEnter SQL Query: ");
    sqlQuery = input.readLine();
    Connection con = DriverManager.getConnection(url, username, password);

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery(sqlQuery);

    while (rs.next()) {
    String s1 = rs.getString(1);
    String s2 = rs.getString(2);
    System.out.println(s1 + "\t" + s2);

    }
    con.close();
    System.out.print("\nClosed connection");
    }//end of main()
    }//end of feh
    <stop>

    Right, if my sql query is "SELECT * FROM Emp;" the while statement will print the first two columns of the table Emp back to me. That's fine i understand that. But i was wondering is there a way to print the entire results gotten from "SELECT * FROM Emp;", every column for each row in the table Emp. I will supply Emp if needs be.


Comments

  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    not quite sure what you're asking. My understanding is that you don't want to 'hard-type' how many results will be returned /dealt with (!?). (A) solution: Polymorphic variable + array. (See java.sun.com / google.ie).

    or a simpler approach:

    while (rs.next()) {
    String s1 = rs.getString(1);
    String s2 = rs.getString(2);
    System.out.println(s1 + "\t" + s2);

    }


    to:

    int i = 0;

    while (rs.next()) {
    i++; // increment i -- loop!
    String s1 = rs.getString(i); // 'put' result into s1...
    printMethod(s1); // pass the result to another method(below)


    // this method will take the result passed...
    public void printMethod(String passedString)
    {
    //...and print it (+ a tab)
    System.out.println(passedString+ "\t");
    }


    ok. me tired. lemme know if it's in the right direction at all....


  • Closed Accounts Posts: 110 ✭✭Korg


    Could be wrong but sounds like ResultSetMetaData is what you're after, this object can be retrieved from a ResultSet object & contains things like the names of all the columns returned etc:

    http://java.sun.com/j2se/1.4.1/docs/api/java/sql/ResultSetMetaData.html

    I've no compiler to hand so i could be typing crap (should work tho) but you get the general idea.

    [PHP]

    ResultSet rs = stmt.executeQuery( "SELECT * FROM Emp" );
    ResultSetMetaData rsmd = rs.getMetaData();

    for( int nRow = 0; rs.next(); nRow++ )
    {
    System.out.println( "Row: " + nRow );

    for( int nColumn = 1; nColumn <= rsmd.getColumnCount(); nColumn++ )
    {
    String sColumnName = rsmd.getColumnName( nColumn );
    String sColumnValue = rs.getString( sColumnName );

    System.out.println( '\t' + sColumnName + ": " + sColumnValue );
    }
    }


    [/PHP]


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Thanks to the both of you for your quick replys. Korg I'm going with yours as I didn't know ResultSetMetaData even existed, and the output is very nice. Many thanks friends.


Advertisement