我有两个jpanel都连接到数据库其中一个插入和其他显示我有Button重新验证显示表格的面板插入数据后但是当我点击一个Button它更新面板显示表但保持旧的一个我在另一种情况下看到这个问题我不想使用表模型我只是想知道问题这是代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
public class SimpleTable {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/finalproject";
JPanel panel=new JPanel();
static JFrame frame=new JFrame();
JTextField a0=new JTextField(19);
JTextField a1=new JTextField(19);
JTextField a2=new JTextField(19);
JTextArea a4=new JTextArea(3, 15);
JTextField a5=new JTextField(19);
JTextField a6=new JTextField(19);
JTextField a7=new JTextField(19);
static JScrollPane jsp = new JScrollPane();
JTable jt = new JTable();
String[] names = { "Male", "Female" };
String[] names2 = {"Bachelor", "Master","Ph.D", "Others" };
String[] names3 = {"Administrator", "Doctor","Nurse", "Staff",
"Accountant", "Guard" };
JComboBox gender11 = new JComboBox(names);
JComboBox dgree = new JComboBox(names2);
JComboBox a3 = new JComboBox(names3);
JPanel p=new JPanel();
static JPanel p1=new JPanel();
JLabel l0=new JLabel("Employee ID");
JLabel l1=new JLabel("First Name");
JLabel l2=new JLabel("Last Name ");
JLabel gender111=new JLabel("Gender");
JLabel l3=new JLabel("Type Of Job");
JLabel l4=new JLabel("Address");
JLabel l5=new JLabel("Phone No.");
JLabel l6=new JLabel("Phone No.(2)");
JLabel dgree1=new JLabel("Academic Degree");
public JPanel aaaaa()
{
gender11.setMaximumRowCount(3);
p.setLayout(new FlowLayout());
l0.setPreferredSize(new Dimension(95, 15));
l1.setPreferredSize(new Dimension(95, 15));
l2.setPreferredSize(new Dimension(95, 15));
l3.setPreferredSize(new Dimension(139, 15));
l4.setPreferredSize(new Dimension(95, 15));
l5.setPreferredSize(new Dimension(95, 15));
l6.setPreferredSize(new Dimension(95, 15));
gender111.setPreferredSize(new Dimension(139, 15));
gender11.setPreferredSize(new Dimension(109, 25));
dgree1.setPreferredSize(new Dimension(139, 15));
dgree.setPreferredSize(new Dimension(109, 25));
a0.setPreferredSize(new Dimension(95, 25));
a1.setPreferredSize(new Dimension(95, 25));
a2.setPreferredSize(new Dimension(95, 25));
a3.setPreferredSize(new Dimension(109, 25));
a4.setPreferredSize(new Dimension(95, 45));
a5.setPreferredSize(new Dimension(95, 25));
a6.setPreferredSize(new Dimension(95, 25));
a4.setLineWrap(true);
JScrollPane scroll = new JScrollPane(a4);
scroll.setPreferredSize(new Dimension(155, 44));
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
p.add(l0);
p.add(a0);
p.add(l1);
p.add(a1);
p.add(l2);
p.add(a2);
p.add(gender111);
p.add(gender11);
p.add(l3);
p.add(a3);
p.add(l4);
p.add(scroll);
p.add(l5);
p.add(a5);
p.add(l6);
p.add(a6);
p.add(l3);
p.add(a3);
p.add(dgree1);
p.add(dgree);
JButton logIn = new JButton("Enter");
buttonH handler = new buttonH();
logIn.addActionListener(handler);
logIn.setPreferredSize(new Dimension(75, 55));
p.add(logIn);
return p;
}
private class buttonH implements ActionListener {
public void actionPerformed(ActionEvent event) {
String str=a0.getText();
String b1=a1.getText();
String b2=a2.getText();
Object g1=gender11.getSelectedItem();
Object b3=a3.getSelectedItem();
String b4=a4.getText();
int b5=Integer.valueOf(a5.getText());
int b6=Integer.valueOf(a6.getText());
Object d1=dgree.getSelectedItem();
int b0 = Integer.parseInt(str);
Connection connection = null; // manages connection
Statement statement = null; // query statement
ResultSet resultSet = null; // manages results
try {
Class.forName(DRIVER);
// establish connection to database
connection = DriverManager.getConnection(DATABASE_URL, "root",
"");
// create Statement for querying database
statement = connection.createStatement();
// query database
statement
.executeUpdate("insert into employee (ID,fname,lname,gender,tjob,address,phone,phone2,acadamicdgree) values('"+b0+"','"+b1+"','"+b2+"','"+g1+"','"+b3+"','"+b4+"','"+b5+"','"+b6+"','"+d1+"')");
} catch (SQLException sqlException) {
sqlException.printStackTrace();
} // end catch
catch (ClassNotFoundException classNotFound) {
classNotFound.printStackTrace();
} // end catch
finally // ensure resultSet, statement and connection are closed
{
try {
statement.close();
connection.close();
} // end try
catch (Exception exception) {
exception.printStackTrace();
} // end catch
} // end finally
SimpleTable1().revalidate();
}
}
public JPanel SimpleTable1() {
Connection connection = null; // manages connection
Statement statement = null; // query statement
ResultSet resultSet = null; // manages results
try {
Class.forName(DRIVER);
// establish connection to database
connection = DriverManager.getConnection(DATABASE_URL, "root",
"");
// create Statement for querying database
statement = connection.createStatement();
// query database
resultSet = statement
.executeQuery("SELECT ID,fname, lname,gender,dbirth,tjob,address,phone,phone2,acadamicdgree FROM employee");
String[] id1 = new String[10];
String[][] id2 = new String[10][10];
String[] fname1 = new String[10];
String[] lname1 = new String[10];
String[] gender1 = new String[10];
String[] dbirth1 = new String[10];
String[] tjob1 = new String[10];
String[] address1 = new String[10];
String[] phone1 = new String[10];
String[] phone21 = new String[10];
String[] acadamic1 = new String[10];
jt = new JTable();
while(resultSet.next()){
String g = String.format("%s", resultSet.getObject(1));
String id = String.format("%s", resultSet.getObject(1));
String fname = String.format("%s", resultSet.getObject(2));
String lname = String.format("%s", resultSet.getObject(3));
String gender = String.format("%s", resultSet.getObject(4));
String dbirth = String.format("%s", resultSet.getObject(5));
String tjob = String.format("%s", resultSet.getObject(6));
String address = String.format("%s", resultSet.getObject(7));
String phone = String.format("%s", resultSet.getObject(8));
String phone2 = String.format("%s", resultSet.getObject(9));
String acadamic = String.format("%s", resultSet.getObject(10));
int key = Integer.parseInt(g);
for (int i=0; i<=5; i++){
if (key ==i+1) {
id1[i]=id;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
fname1[i]=fname;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
lname1[i]=lname;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
gender1[i]=gender;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
tjob1[i]=tjob;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
address1[i]=address;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
phone1[i]=phone;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
phone21[i]=phone2;
}
}
for (int i=0; i<=5; i++){
if (key ==i+1) {
acadamic1[i]=acadamic;
}
}
}
for (int i=0; i<=5; i++){
id2[i][0]=id1[i];
}
for (int i=0; i<=5; i++){
id2[i][1]=fname1[i];
}
for (int i=0; i<=5; i++){
id2[i][2]=lname1[i];
}
for (int i=0; i<=5; i++){
id2[i][3]=gender1[i];
}
for (int i=0; i<=5; i++){
id2[i][4]=dbirth1[i];
}
for (int i=0; i<=5; i++){
id2[i][5]=tjob1[i];
}
for (int i=0; i<=5; i++){
id2[i][6]=address1[i];
}
for (int i=0; i<=5; i++){
id2[i][7]=phone1[i];
}
for (int i=0; i<=5; i++){
id2[i][8]=phone21[i];
}
for (int i=0; i<=5; i++){
id2[i][9]=acadamic1[i];
}
String[] stT = new String[10];
stT = new String[] { "ID", "First Name", "Last Name", "Gender", "Type Of Job", "Address", "Phone", "Phone(2)", "Acadamic Dgree" };
jt = new JTable(id2, stT);
jt.setPreferredScrollableViewportSize(jt.getPreferredSize());
jt.setFillsViewportHeight(true);
jsp = new JScrollPane(jt,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
catch (SQLException sqlException) {
sqlException.printStackTrace();
} // end catch
catch (ClassNotFoundException classNotFound) {
classNotFound.printStackTrace();
} // end catch
finally // ensure resultSet, statement and connection are closed
{
try {
resultSet.close();
statement.close();
connection.close();
} // end try
catch (Exception exception) {
exception.printStackTrace();
} // end catch
} // end finally
// jsp.getViewport().setBackground(Color.green);
//jsp.getViewport().setOpaque(false);
jsp.getViewport().setPreferredSize(new Dimension(655, 80));
p1.add(jsp);
return p1;
}
public static void main(String args[])throws Exception {
SimpleTable st = new SimpleTable();
frame.setSize(335, 595);
frame.getContentPane().add(st.aaaaa(), BorderLayout.CENTER);
frame.getContentPane().add(st.SimpleTable1(), BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 0 :(得分:0)
点击JButton
后,您需要删除所有组件,然后添加新组件。然后使用相应容器的updateUI()
方法。