美好的一天! 我的更新功能有问题。 我在mysql中有一个表。即tblClientInfo,tblbusinessinfo,tblbusinesssituation,tblledger。表tblClientInfo具有主键CLIENTID。其他3个表使用此clientid作为外键。问题是,每当我运行我的程序并更新用户界面中未填充的文本框时,我都无法更新。只更新tblClientInfo中的信息而不更新其他表。但我可以更新数据库中是否已有信息。但是,如果我保存有关新客户端的新信息,则只保存tblclient,当我尝试更新时,它没有工作......
这是我的SAVE代码:
private void cmdSaveMouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
String sql = "insert into tblClientInfo (ClientID, LastName, FirstName, MiddleName, Gender, BirthPlace, BirthDate, "
+ "Status, ResAddress, EmploymentQuery, HomeNo, ResidenceType) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
String sql2 = "insert into tblBusinessInfo (tblBusinessInfo.ClientID, MarketID, StallNo, Location, BusinessTelNo, YearStarted, "
+ "OtherBusinessQuery, MonthlyIncome, CarQuery, CarBrand, MortgagedQuery, PBalance) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
String sql3 = "insert into tblBusinessSituation (tblBusinessSituation.ClientID, MerchandiseID, StockValue, "
+ "LowestSale, AverageSale) values (?, ?, ?, ?, ?)";
String sql4 = "insert into tblledger (tblledger.ClientID, MarketID, LoanAmount, DateStarted, DailyPay, Expiry) "
+ "values (?, ?, ?, ?, ?, ?)";
try
{
pst = conn.prepareStatement(sql);
//===================================Client Information Layer=======================================
pst.setString(1, txtClientID.getText());
pst.setString(2, txtLastName.getText());
pst.setString(3, txtFirstName.getText());
pst.setString(4, txtMiddleName.getText());
pst.setString(5, (String) comboGender.getSelectedItem().toString());
pst.setString(6, txtBirthPlace.getText());
if(txtBirthDate.getText().isEmpty())
pst.setString(7, null);
else
pst.setString(7, txtBirthDate.getText());
pst.setString(8, (String) comboStatus.getSelectedItem().toString());
pst.setString(9, txtAddress.getText());
pst.setString(10, (String) comboEmploymentQuery.getSelectedItem().toString());
if(txtHomeNo.getText().isEmpty())
pst.setString(11, null);
else
pst.setString(11, txtHomeNo.getText());
pst.setString(12, (String) comboResidency.getSelectedItem().toString());
pst.execute();
//System.out.println("Nasave ang sql ? : "+pst.execute());
UpdateJTable();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
try
{
pst2 = conn.prepareStatement(sql2);
//===================================Buainess Information Layer======================================
pst2.setString(1, txtClientID.getText());
pst2.setString(2, (String) comboMarket.getSelectedItem().toString().substring(0, 1));
if(txtStallNo.getText().isEmpty())
pst2.setString(3, null);
else
pst2.setString(3, txtStallNo.getText());
pst2.setString(4, txtLocation.getText());
if(txtBusinessNo.getText().isEmpty())
pst2.setString(5, null);
else
pst2.setString(5, txtBusinessNo.getText());
if(txtYearStarted.getText().isEmpty())
pst2.setString(6, null);
else
pst2.setString(6, txtYearStarted.getText());
pst2.setString(7, (String) comboOtherBusinessQuery.getSelectedItem().toString());
if(txtMonthlyIncome.getText().isEmpty())
pst.setString(8, null);
else
pst2.setString(8, txtMonthlyIncome.getText());
pst2.setString(9, (String) comboCar.getSelectedItem().toString());
pst2.setString(10, txtBrand.getText());
pst2.setString(11, (String) comboMortgaged.getSelectedItem().toString());
if(txtPBalance.getText().isEmpty())
pst2.setString(12, null);
else
pst2.setString(12, txtPBalance.getText());
pst2.execute();
// System.out.println("Nasave ang sql2 ? : "+ pst2.execute());
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
try
{
pst3 = conn.prepareStatement(sql3);
pst3.setString(1, txtClientID.getText());
pst3.setString(2, (String) comboMerchandise.getSelectedItem().toString().substring(0, 1));
if(txtValueOfStocks.getText().isEmpty())
pst3.setString(3, null);
else
pst3.setString(3, txtValueOfStocks.getText());
if(txtLowestSale.getText().isEmpty())
pst3.setString(4, null);
else
pst3.setString(4, txtLowestSale.getText());
if(txtAveSale.getText().isEmpty())
pst3.setString(5, null);
else
pst3.setString(5, txtAveSale.getText());
pst3.execute();
//System.out.println("Nasave ang sql3 ? : "+ pst3.execute());
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
try
{
pst4 = conn.prepareStatement(sql4);
pst4.setString(1, txtClientID.getText());
pst4.setString(2, (String) comboMarket.getSelectedItem().toString().substring(0, 1));
pst4.setString(3, txtLoanAmount.getText());
pst4.setString(4, txtStart.getText());
pst4.setString(5, txtDailyPay.getText());
if(txtExpiry.getText().isEmpty())
pst4.setString(6, null);
else
pst4.setString(6, txtExpiry.getText());
pst4.execute();
// System.out.println("Nasave ang sql4 ? : "+ pst4.execute());
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
和UPDATE
cmdNewSpouse.setEnabled(false);
String sql = "delete from tblClientInfo where ClientID = ?";
try
{
pst = conn.prepareStatement(sql);
pst.setString(1, txtClientID.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Deleted");
UpdateJTable();
clear();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
private void cmdUpdateMouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
String sql = "update tblClientInfo set LastName = ?, FirstName = ?, MiddleName = ?, Gender = ?, BirthPlace = ?, "
+ "BirthDate = ?, Status = ?, ResAddress = ?, EmploymentQuery = ?, HomeNo = ?, ResidenceType = ? "
+ "where ClientID = ?";
String sql2 = "update tblBusinessInfo set StallNo = ?, "
+ "MarketID = ?, Location = ?, BusinessTelNo = ?, YearStarted = ?, "
+ "OtherBusinessQuery = ?, MonthlyIncome = ? , CarQuery = ?, CarBrand = ?, "
+ "MortgagedQuery = ?, PBalance = ? where tblBusinessInfo.ClientID = ?";
String sql3 = "update tblBusinessSituation set MerchandiseID = ?, StockValue = ?, "
+ "LowestSale = ?, AverageSale = ? where tblBusinessSituation.ClientID = ?";
String sql4 = "update tblledger set MarketID = ?, LoanAmount = ?, DateStarted = ?, DailyPay = ?, Expiry = ? "
+ "where tblledger.ClientID = ?";
try
{
pst = conn.prepareStatement(sql);
//===================================Client Information Layer=======================================
pst.setString(1, txtClientID.getText());
pst.setString(2, txtLastName.getText());
pst.setString(3, txtFirstName.getText());
pst.setString(4, txtMiddleName.getText());
pst.setString(5, (String) comboGender.getSelectedItem().toString());
pst.setString(6, txtBirthPlace.getText());
if(txtBirthDate.getText().isEmpty())
pst.setString(7, null);
else
pst.setString(7, txtBirthDate.getText());
pst.setString(8, (String) comboStatus.getSelectedItem().toString());
pst.setString(9, txtAddress.getText());
pst.setString(10, (String) comboEmploymentQuery.getSelectedItem().toString());
if(txtHomeNo.getText().isEmpty())
pst.setString(11, null);
else
pst.setString(11, txtHomeNo.getText());
pst.setString(12, (String) comboResidency.getSelectedItem().toString());
pst.execute();
//System.out.println("Nasave ang sql ? : "+pst.execute());
UpdateJTable();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
try
{
pst2 = conn.prepareStatement(sql2);
//===================================Buainess Information Layer======================================
pst2.setString(1, txtClientID.getText());
pst2.setString(2, (String) comboMarket.getSelectedItem().toString().substring(0, 1));
if(txtStallNo.getText().isEmpty())
pst2.setString(3, null);
else
pst2.setString(3, txtStallNo.getText());
pst2.setString(4, txtLocation.getText());
if(txtBusinessNo.getText().isEmpty())
pst2.setString(5, null);
else
pst2.setString(5, txtBusinessNo.getText());
if(txtYearStarted.getText().isEmpty())
pst2.setString(6, null);
else
pst2.setString(6, txtYearStarted.getText());
pst2.setString(7, (String) comboOtherBusinessQuery.getSelectedItem().toString());
if(txtMonthlyIncome.getText().isEmpty())
pst.setString(8, null);
else
pst2.setString(8, txtMonthlyIncome.getText());
pst2.setString(9, (String) comboCar.getSelectedItem().toString());
pst2.setString(10, txtBrand.getText());
pst2.setString(11, (String) comboMortgaged.getSelectedItem().toString());
if(txtPBalance.getText().isEmpty())
pst2.setString(12, null);
else
pst2.setString(12, txtPBalance.getText());
pst2.execute();
// System.out.println("Nasave ang sql2 ? : "+ pst2.execute());
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
try
{
pst3 = conn.prepareStatement(sql3);
pst3.setString(1, txtClientID.getText());
pst3.setString(2, (String) comboMerchandise.getSelectedItem().toString().substring(0, 1));
if(txtValueOfStocks.getText().isEmpty())
pst3.setString(3, null);
else
pst3.setString(3, txtValueOfStocks.getText());
if(txtLowestSale.getText().isEmpty())
pst3.setString(4, null);
else
pst3.setString(4, txtLowestSale.getText());
if(txtAveSale.getText().isEmpty())
pst3.setString(5, null);
else
pst3.setString(5, txtAveSale.getText());
pst3.execute();
//System.out.println("Nasave ang sql3 ? : "+ pst3.execute());
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
try
{
pst4 = conn.prepareStatement(sql4);
pst4.setString(1, txtClientID.getText());
pst4.setString(2, (String) comboMarket.getSelectedItem().toString().substring(0, 1));
pst4.setString(3, txtLoanAmount.getText());
pst4.setString(4, txtStart.getText());
pst4.setString(5, txtDailyPay.getText());
if(txtExpiry.getText().isEmpty())
pst4.setString(6, null);
else
pst4.setString(6, txtExpiry.getText());
pst4.execute();
// System.out.println("Nasave ang sql4 ? : "+ pst4.execute());
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
`
提前感谢未来的帮助。
答案 0 :(得分:0)
提供的代码有点杂乱和大,作为建议请检查JTable刷新/验证和任何propertyChange侦听器方法。 在DB中添加新条目后,必须将事件传输到UI,然后必须以编写代码的方式更新JTable