Jtable不显示Java中数据库中的内容(使用MySQL查询浏览器)
我正在研究从数据库中获取数据并将其传输到Java中的JTable。我的问题是,收集的数据没有显示在我的JTable中。我试着用Java打印查询来调试它,它会打印,但不会在JTable中显示任何数据。请帮助我,提前谢谢。TT_TT 这是我的UI代码Jtable不显示Java中数据库中的内容(使用MySQL查询浏览器),java,mysql,swing,jtable,Java,Mysql,Swing,Jtable,我正在研究从数据库中获取数据并将其传输到Java中的JTable。我的问题是,收集的数据没有显示在我的JTable中。我试着用Java打印查询来调试它,它会打印,但不会在JTable中显示任何数据。请帮助我,提前谢谢。TT_TT 这是我的UI代码 /* * Programmer : Inodeo, Claire Fatima R. * Date Developed : April 20, 2015 * Company : * Purpose
/*
* Programmer : Inodeo, Claire Fatima R.
* Date Developed : April 20, 2015
* Company :
* Purpose : To gather data from database and display it
* to JTable. A desktop application which requires Java to be installed
* in PC for it to run. For Java installer visit www.oracle.com and download the
* 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 and set layout
setPreferredSize (new Dimension (900, 400));
setLayout (null);
//*****************************SQL GOES HERE************************************
//==================================================================================
//=========================Combox for places==========================================
//==================================================================================
city_drp.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String chosenName = (String) city_drp.getSelectedItem();
//removes all the 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 FROM 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 FROM 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 FROM 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 the information from the database to the 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 is " +frm_drp.getSelectedItem());
System.out.println("this is " +to_drp.getSelectedItem());
System.out.println("this is " +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 WILL BE ADDED HERE AND SIZE WILL BE 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 by 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);
}
}
到底是什么不起作用?顺便问一下,mysql的url正确吗?主机似乎丢失?您是否尝试在
add(滚动窗格)之后调用revalidate
和repaint
代码>?当您实际上没有使用JFrame
的任何功能时,table_data
为什么从JFrame
扩展而来?我还想补充一点,这看起来像是为了小利益而在圈子里跑来跑去。你只需要<代码> TabLimoDele>代码,你可以在屏幕上留下<代码> jtab>代码,并简单地替换模型,你可能想读一读,这会让人们更容易阅读代码,也可以让你阅读其他人,你也可以考虑使用<代码> PravaReStays< /Cult> s,这将使您的代码执行起来更安全,调试起来更容易,并且不太可能破坏您的数据库。避免使用null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题
//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 a static table
try {
con = DriverManager.getConnection("jdbc:mysql://:3306/ngcp_table","root","root");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
(
"SELECT * FROM ngcp_table.negros "
+ "WHERE Negros_Place = '"+place+"'AND Negros_Date BETWEEN '" + Date + "' AND '" + 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;
}
}