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.

Reading the columns, JDBC

  • 10-03-2009 10:11PM
    #1
    Registered Users, Registered Users 2 Posts: 539 ✭✭✭


    So i have this peace of java code

    try {
    Dao d = new Dao();
    Connection con = d.getConnection();
    Statement s = con.createStatement();
    s.execute("select ID from VEHICLES"); // select the data from the table

    ResultSet rs = s.getResultSet(); // get any ResultSet that came from our quer
    if (rs != null) // if rs == null, then there is no ResultSet to view
    {
    while (rs.next()) // this will step through our data row-by-row
    {
    /* the next line will get the first column in our current row's ResultSet
    as a String ( getString( columnNumber) ) and output it to the screen */
    result.add(rs.getString(1));
    }
    }

    s.close(); // close the Statement to let the database know we're done with it
    con.close(); // close the Connection to let the database know we're done with it

    } catch (Exception ex) {
    System.out.println("Message " + ex.getMessage());
    }

    It reads all rows in a database. I would be searcing some particular Vehicle lets say. What I want to know is, how would I extract the corresponding columns to the row I find.

    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 539 ✭✭✭but43r


    Ok firgured this one out.
    Instead of having s.execute("select ID from VEHICLES"); i had to have s.execute("select * from VEHICLES"); and just change result.add(rs.getString(1)) whichever column I want :)


  • Registered Users, Registered Users 2 Posts: 25 Malached


    SELECT id FROM vehicles where <vehicle_type> = <vehicle type> basic ANSI SQL. Any language.


Advertisement