java - how to output the table data from database extends jpanel with scrollbars -


public class mydatabase extends jpanel {     string sql;     //string tablename;     public mydatabase(){}     public mydatabase(string sql)     {          system.out.printf("hai");         arraylist columnnames = new arraylist();         arraylist data = new arraylist();          //  connect mysql database, run query, result set            // this.tablename=tablename;            this.sql =sql;           // ensure sql objects closed when program         // finished them         try {             connection con=databaseconnection.getconnection();             statement stmt=con.createstatement();             resultset rs = stmt.executequery( sql );              resultsetmetadata md = rs.getmetadata();             int columns = md.getcolumncount();              //  column names             (int = 1; <= columns; i++)             {                 columnnames.add( md.getcolumnname(i) );             }              //  row data             while (rs.next())             {                 arraylist row = new arraylist(columns);                  (int = 1; <= columns; i++)                 {                     row.add( rs.getobject(i) );                 }                  data.add( row );             }         }         catch (sqlexception e)         {             system.out.println( e.getmessage() );         }          // create vectors , copy on elements arraylists them         // vector deprecated using them in example keep         // things simple - best practice create custom defined         // class inherits abstracttablemodel class         vector columnnamesvector = new vector();         vector datavector = new vector();          (int = 0; < data.size(); i++)         {             arraylist subarray = (arraylist)data.get(i);             vector subvector = new vector();             (int j = 0; j < subarray.size(); j++)             {                 subvector.add(subarray.get(j));             }             datavector.add(subvector);         }          (int = 0; < columnnames.size(); i++ )             columnnamesvector.add(columnnames.get(i));          //  create table database data         jtable table = new jtable(datavector, columnnamesvector)         {              public class getcolumnclass(int column)             {                 (int row = 0; row < getrowcount(); row++)                 {                     object o = getvalueat(row, column);                      if (o != null)                     {                         return o.getclass();                     }                 }                  return object.class;             }         };          jframe frame=new jframe();             jscrollpane scrollpane = new jscrollpane( table );             frame.getcontentpane().add( scrollpane );              jpanel buttonpanel = new jpanel();             frame.getcontentpane().add( buttonpanel, borderlayout.south );                   system.out.println("query building"+sql);     }      public static void main(string[] args)     {         scanner in = new scanner(system.in);         system.out.print("please enter user name : ");         string tablename = in.nextline();                system.out.println("you entered : " + tablename);         string sql=" select * "+tablename;          jframe frame = new jframe();          mydatabase pan = new mydatabase(sql);         pan.setvisible(true);         frame.add(pan);         frame.pack();         frame.setvisible(true);         system.out.println("hai");     } } 

displaying output frame, not table data.

you create jframe twice. 1 in main() , second in mydatabase(string sql), add table , scroll pane. need add scroll pane frame created in main() instead. (or, since mydatabase extends jpanel add that; adding mydatabase frame in main() should work intended)

furthermore, swing single threaded, , should create , access swing components in event dispatch thread. see tutorial tells details how that.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -