我有一個「經理」類和「BirthList」框架。在我的BirthList框架中,我有一個顯示mySQL表中所有數據的GUI表,但它的確如此:爲什麼我不能在我的GUI表中顯示我的MySQL信息?
當我打開這個框架時,我看到最後添加的數據,當我點擊關閉按鈕,首先最後的數據將從我的表中刪除,然後框架將被關閉,如果我再次打開框架,我會看到在MySQL tabel中的整個數據,我也會看到最後兩次數據。爲什麼?
我想表明我的GUI表從MySQL表我的數據。如果我關閉框架然後打開它,我希望看到之前添加的所有那些數據+我最近添加的新數據。
用戶類:
public class Manager {
Logger logger = Logger.getLogger(this.getClass().getName());
private static Connection conn = DBManager.getConnection();
public static Admin admin;
private static List<Birth> birthList;
public static void addBirth(String name, String family, String fatherName, String mName, String dOfBirth, String pOfBirth) {
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO birthtable(name,family,fatherName,motherName,dateOfBirth,placeOfBirth)" + "VALUES('" + name + "','" + family + "','" + fatherName + "','" + mName + "','" + dOfBirth + "','" + pOfBirth + "')");
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static boolean isAddBirth(String name, String family, String fatherName, String mName, String dOfBirth, String pOfBirth) {
boolean bool = true;
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
ResultSet rst = null;
try {
rst = stmt.executeQuery("SELECT * FROM birthtable");
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
try {
while (rst.next()) {
if (rst.getString(2).equals(name) && rst.getString(3).equals(family) && rst.getString(4).equals(fatherName) && rst.getString(5).equals(mName) && rst.getString(6).equals(dOfBirth) && rst.getString(7).equals(pOfBirth)) {
bool = false;
} else {
bool = true;
}
}
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
return bool;
}
public static void addToBirthListFromMySQL() throws SQLException {
Birth list1 = null;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM birthtable");
while (rs.next()) {
String s1 = rs.getString(2);
if (rs.wasNull()) {
s1 = null;
}
String s2 = rs.getString(3);
if (rs.wasNull()) {
s2 = null;
}
String s3 = rs.getString(4);
if (rs.wasNull()) {
s3 = null;
}
String s4 = rs.getString(5);
if (rs.wasNull()) {
s4 = null;
}
String s5 = rs.getString(6);
if (rs.wasNull()) {
s5 = null;
}
String s6 = rs.getString(7);
if (rs.wasNull()) {
s6 = null;
}
list1 = new Birth(s1, s2, s3, s4, s5, s6);
birthList = admin.getBirthList();
if (birthList == null) {
birthList = new ArrayList<Birth>();
}
birthList.add(list1);
}
admin.setBirthList(birthList);
}
}
BirthList幀:
public class BirthList extends javax.swing.JFrame {
private Admin admin;
List<Birth> list;
DefaultTableModel model;
/** Creates new form BirthList */
public BirthList(Admin admin) {
initComponents();
this.admin = admin;
Manager.admin = admin;
try {
Manager.addToBirthListFromMySQL();
} catch (SQLException ex) {
Logger.getLogger(BirthList.class.getName()).log(Level.SEVERE, null, ex);
}
fillTable();
}
private void cancleBActionPerformed(java.awt.event.ActionEvent evt) {
for(int i=0;i<jTable1.getRowCount();i++){
model.removeRow(i);
model.fireTableDataChanged();
}
int r = JOptionPane.showConfirmDialog(this, "Are you sure?", "Message", JOptionPane.YES_NO_CANCEL_OPTION);
if(r==JOptionPane.YES_OPTION)
this.dispose(); // TODO add your handling code here:
}
public void fillTable() {
String[] columNames = {"name", "family", "father's name", "mother's name", "date of birth", "place of birth"};
List<Birth> birth = admin.getBirthList();
if (birth==null) {
JOptionPane.showMessageDialog(this, "Death list is empty! at first ,add a person.", "Error", JOptionPane.ERROR_MESSAGE);
}else{
Object[][] data = new Object[birth.size()][columNames.length];
for (int i = 0; i < data.length; i++) {
Birth birth1 = birth.get(i);
data[i][0] = birth1.getName();
data[i][1] = birth1.getFamily();
data[i][2] = birth1.getFatherName();
data[i][3] = birth1.getMotherName();
data[i][4] = birth1.getDateOfBirth();
data[i][5] = birth1.getPlaceOfBirth();
}
model = new DefaultTableModel(data, columNames);
jTable1.setModel(model);
}
}
}
你的函數「isAddBirth」的那種,怎麼說...爲什麼沒有做一個真正的SQL請求而不是大量迭代與結果集?至少在找到結果集時添加一箇中斷... – 2009-12-15 04:23:21
我該怎麼做? – Johanna 2009-12-15 04:29:33
你能給我舉個例子嗎? – Johanna 2009-12-15 04:30:07