我一直试图将数据插入MYSql数据库,同时在JTable中显示数据。问题是我无法将数据存储到数据库中,但我能够在JTable中显示。我已将db连接字符串放在另一个类中,如下所示
public class DatabaseConnection {
public static Connection getDBConnection() {
Connection connection;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounting","root","jinXED007");
return connection;
} catch (Exception ex) {
return null;
}
}
}
以及实现JTable的方法如下
table = new JTable(dTableModel);
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounting","root","jinXED007");
Statement sqlState = conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String selectStuff = "Select customerid,name,address,town,postal_code,tel_no,contact_person from customer";
rows = sqlState.executeQuery(selectStuff);
Object[] tempRow;
while (rows.next()) {
tempRow = new Object[] { rows.getInt(1), rows.getString(2),
rows.getString(3), rows.getString(4), rows.getString(5) ,rows.getInt(6),rows.getString(7)};
dTableModel.addRow(tempRow);
}
}
catch (SQLException ex) {
// String describing the error
System.out.println("SQLException: " + ex.getMessage());
// Vendor specific error code
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table.setAutoCreateRowSorter(true);
table.setFont(new Font("Segoe UI", Font.PLAIN, 12));
table.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
scrollPane.setViewportView(table);
而actionlistener如下
btnSave.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
String sCustomerID="",sCustomerName="",sAddress="",sCity="",sPostalCode="",sTelNo="",sContactPerson="";
sCustomerID=txtcustomernumber.getText();
sCustomerName=txtcustomername.getText();
sAddress=txtaddress.getText();
sCity=txtcity.getText();
sPostalCode=txtpostalcode.getText();
sTelNo=txttelno.getText();
sContactPerson=txtcontactperson.getText();
try {
// Moves the database to the row where data will be placed
rows.moveToInsertRow();
// Update the values in the database
rows.updateString("customerid", sCustomerID);
rows.updateString("name", sCustomerName);
rows.updateString("address", sAddress);
rows.updateString("town", sCity);
rows.updateString("postal_code", sPostalCode);
rows.updateString("tel_no", sTelNo);
rows.updateString("contact_person",sContactPerson );
// Inserts the changes to the row values in the database
rows.insertRow();
// Directly updates the values in the database
rows.updateRow();
} catch (SQLException e1) {
e1.printStackTrace();
}
//int sCustomerid = 0;
try {
// Go to the last row inserted and get the id
rows.last();
//sCustomerid = rows.getInt(1);
} catch (SQLException e1) {
e1.printStackTrace();
}
Object[] customer = {sCustomerID,sCustomerName,sAddress,
sCity,sPostalCode,sTelNo,sContactPerson };
// Add the row of values to the JTable
dTableModel.addRow(customer);
}
});