Jtable not showing what is in the database (MySQL Query browser is used) in Java -
i working on getting data database , transfer jtable in java. problem is, data gathered not showing in jtable. tried debugging printing query in java, prints not show data in jtable. please me, in advance. tt_tt
here ui code
/* * programmer : inodeo, claire fatima r. * date developed : april 20, 2015 * company : * purpose : gather data database , display * jtable. desktop application requires java installed * in pc run. java installer visit www.oracle.com , download * latest version of java. * */ import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import javax.swing.*; @suppresswarnings("serial") public class userinterface extends jpanel { private jbutton srch_btn; table_data obj; private jscrollpane scrollpane; //jlabel private jlabel date_lbl; private jlabel frm_lbl; private jlabel to_lbl; private jlabel pl_lbl; private jlabel where_lbl; //sql connection con=null; statement stmt = null; //combobox private jcombobox<string> frm_drp; private jcombobox<string> to_drp; private jcombobox<string> brngy_drp; public static string names[] = {"negros", "panay","leyte"}; public static jcombobox<string> city_drp; //******constructor********************* public userinterface() { //construct components obj = new table_data (); srch_btn = new jbutton ("search"); date_lbl = new jlabel ("date:"); frm_lbl = new jlabel ("from :"); to_lbl = new jlabel ("to :"); frm_drp = new jcombobox<> (); to_drp = new jcombobox<> (); pl_lbl = new jlabel ("place"); where_lbl = new jlabel ("where :"); city_drp = new jcombobox<> (names); brngy_drp = new jcombobox<> (); //adjust size , set layout setpreferredsize (new dimension (900, 400)); setlayout (null); //*****************************sql goes here************************************ //================================================================================== //=========================combox places========================================== //================================================================================== city_drp.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string chosenname = (string) city_drp.getselecteditem(); //removes unecessary things in combobox //prevents overlapping informations brngy_drp.removeallitems(); to_drp.removeallitems(); frm_drp.removeallitems(); if(chosenname.equals("panay")) { try { con = drivermanager.getconnection("jdbc:mysql://:3306/ngcp_table","root","root"); stmt = con.createstatement(); resultset rs = stmt.executequery("select distinct panay_place, panay_date ngcp_table.panay;"); while(rs.next()) { brngy_drp.additem (rs.getstring("panay_place")); frm_drp.additem (rs.getstring("panay_date")); to_drp.additem (rs.getstring("panay_date")); } rs.close(); stmt.close(); con.close(); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } }else if(chosenname.equals("negros")) { try { con = drivermanager.getconnection("jdbc:mysql://:3306/ngcp_table","root","root"); stmt = con.createstatement(); resultset rs = stmt.executequery("select distinct negros_place, negros_date ngcp_table.negros;"); while(rs.next()) { brngy_drp.additem (rs.getstring("negros_place")); frm_drp.additem (rs.getstring("negros_date")); to_drp.additem (rs.getstring("negros_date")); } rs.close(); stmt.close(); con.close(); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } }else if(chosenname.equals("leyte")) { try { con = drivermanager.getconnection("jdbc:mysql://:3306/ngcp_table","root","root"); stmt = con.createstatement(); resultset rs = stmt.executequery("select distinct leyte_place, leyte_date ngcp_table.leyte;"); while(rs.next()) { brngy_drp.additem (rs.getstring("leyte_place")); frm_drp.additem (rs.getstring("leyte_date")); to_drp.additem (rs.getstring("leyte_date")); } rs.close(); stmt.close(); con.close(); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } } }); //***************************for table*********************** //====================search button========================== srch_btn.addmouselistener(new mouselistener() { @override public void mousereleased(mouseevent e) { } @override public void mousepressed(mouseevent arg0) { // todo auto-generated method stub } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub //loads information database jtable scrollpane = obj.table((frm_drp.getselectedindex()+1),(to_drp.getselectedindex()+1),(brngy_drp.getselectedindex()+1)); //jscrollpane table(char sample_place, char sample_date, int kw,int kv) system.out.println("this " +frm_drp.getselecteditem()); system.out.println("this " +to_drp.getselecteditem()); system.out.println("this " +brngy_drp.getselecteditem()); if (scrollpane == null) { scrollpane.setsize(100, 300); //end of table codes system.out.println("no"); } else { add(scrollpane); system.out.println("yes"); } } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } });//*********************end of search button code****************** //============================================================================= //===========components added here , size set================ //============================================================================= //add components add (srch_btn); add (date_lbl); add (frm_lbl); add (to_lbl); add (frm_drp); add (to_drp); add (pl_lbl); add (where_lbl); add (city_drp); add (brngy_drp); scrollpane = obj.table((frm_drp.getselectedindex()+1),(to_drp.getselectedindex()+1),(brngy_drp.getselectedindex()+1)); add (scrollpane); //*******set component bounds (only needed absolute positioning)*********** srch_btn.setbounds (140, 240, 105, 25); date_lbl.setbounds (25, 40, 100, 25); frm_lbl.setbounds (45, 70, 100, 25); to_lbl.setbounds (45, 110, 100, 25); frm_drp.setbounds (100, 70, 150, 25); to_drp.setbounds (100, 105, 150, 25); pl_lbl.setbounds (25, 140, 100, 25); where_lbl.setbounds (40, 170, 100, 25); city_drp.setbounds (100, 170, 150, 25); brngy_drp.setbounds (100, 200, 150, 25); } //=================================================================================== //=============================main program goes here================================ //=================================================================================== public static void main (string[] args) { jframe frame = new jframe ("ngcp data system"); frame.setdefaultcloseoperation (jframe.exit_on_close); frame.getcontentpane().add (new userinterface()); frame.pack(); frame.add(new jscrollpane()); frame.setvisible (true); } }
and here table code
//for sql import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; //javaswing import javax.swing.*; import javax.swing.table.defaulttablemodel; public class table_data extends jframe { /** * */ private static final long serialversionuid = 1l; private jtable table_panel; private jscrollpane scrollpane; private defaulttablemodel dtm; connection con=null; statement stmt = null; @suppresswarnings("serial") public jscrollpane table(int date,int date2, int place) { string columnlabel[] = {"date","kw","kv"}; dtm = new defaulttablemodel(); dtm.addcolumn(columnlabel[0]); dtm.addcolumn(columnlabel[1]); dtm.addcolumn(columnlabel[2]); //creates static table try { con = drivermanager.getconnection("jdbc:mysql://:3306/ngcp_table","root","root"); stmt = con.createstatement(); resultset rs = stmt.executequery ( "select * ngcp_table.negros " + "where negros_place = '"+place+"'and negros_date between '" + date + "' , '" + date2 + "'" ); system.out.println("hellowsss"); while(rs.next()) { dtm.addrow(new object[] { rs.getstring("negros_date"), rs.getstring("kw"), rs.getstring("kv") } ); system.out.println("yeah vaery nice"); } rs.close(); stmt.close(); con.close(); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } table_panel = new jtable(dtm); scrollpane = new jscrollpane(table_panel); scrollpane.setvisible(true); scrollpane.setbounds(280, 15, 600, 380); //setbounds(x,y,width,lenth); return scrollpane; } }