Link to home
Start Free TrialLog in
Avatar of wsy
wsy

asked on

vector mysql question

hi

i have a method which takes in a String which is used to query the database, the results gets put into a Vector which is then return, the problem is i dont think the results are added in the Vector can someone help plz

public static Vector query(String query)
{

 try{
      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT author,bookName, from book   where author= ('"+ query+"') ");

      while( rs.next() )
      {
            result.addElement(rs.getString("author"));
            result.addElement(rs.getString("bookName"));      
      }      

}catch (Exception e){System.out.println(e.getMessage());}

return result;

}
Avatar of sudhakar_koundinya
sudhakar_koundinya

I Just want to change your code some thing like this

class Results
{
    String author;
     String bookname;
}

public static Vector query(String query)
{

 try{
     stmt = con.createStatement();
     rs = stmt.executeQuery("SELECT author,bookName, from book   where author= ('"+ query+"') ");

     while( rs.next() )
     {
         Results r=new Results;
          r.author=rs.getString("author");
          r.book=rs.getString("bookName");
            result.addElement(r);
     }    

}catch (Exception e){System.out.println(e.getMessage());}

return result;

}
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
e.g. get first author:

TableModel model = resultSetToTableModel(rs);
String firstAuthor = (String)model.getValueAt(1, 0);

Avatar of wsy

ASKER

hi

thanks for the input i am going to try out your solution

i was wondering why didnt the code i wrote worked can some one explain?
Well you would have all the authors and bookNames on top of each other. You want them side by side logically - as in the table
yor are mixing both author and book with one collection. This becomes difficult to mage the values
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can use the code as posted in above comment
The brackets in the sql are redundant btw and there are too many commas

Vector vect=query("SELECT author, bookName from book where author= '"+ query+ '");
yes, that's true ;-)
8-)