我正在开发一个应用程序,通过从数据库中读取数据来动态创建表单。我想要的是用户能够通过使用数据库中的相应按钮来更新或删除数据库(我允许用户连接到几乎任何数据库,所以我不知道列的类型,或更新是否会得到支持)。我已成功创建代码以在任何数据库中插入数据,但我正在努力找出更新和删除记录的方法。这是用于更新/删除的类的代码片段:
/* tflist contains the list of text fields(I used setName() to set their names to
the column names in the table) which I created dynamically
for allowing the user to enter new values and they already hold the
current values,panel contains all the labels and generated gui which is basically
column name in a label and text field,
e.g. Roll no(label) : 9(in a text field),
tablename has the name of the table,
jtable is the table on which event occurred
(you click on a row of table, and a form appears that gives you the option to
update or delete something),and rowno contains the row number of jtable on which
the user clicked */
private void update_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String querypart2=" WHERE ";
for(int i=0;i<tflist.size();i++)
{
JTextField tf=tflist.get(i);
if(tf.getText()!=null&&!tf.getText().equals(""))
{
if(jtable.getValueAt(rowno, i)==null||jtable.getValueAt(rowno, i).equals(""))
{
querypart2=querypart2+"\""+tf.getName()+"\" IS NULL";
}
else
{
querypart2=querypart2+"\""+tf.getName()+"\"='"+jtable.getValueAt(rowno,i)+"'";
}
querypart2=querypart2+" AND ";
}
}
if(querypart2.equals(" WHERE "))
{
querypart2="";
}
else
{
querypart2=querypart2.substring(0, querypart2.length()-5);
}
try {
Statement statement = Aw_supersensible.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs=statement.executeQuery("SELECT * FROM \""+tablename+"\""+querypart2);
rs.absolute(1);
for(int i=0;i<tflist.size();i++)
{
JTextField tf=tflist.get(i);
rs.updateObject(tf.getName(), tf.getText());
}
Aw_supersensible.conn.commit();
for(int i=0;i<tflist.size();i++)
{
JTextField tf=tflist.get(i);
jtable.setValueAt( tf.getText(),rowno,i);
rs.updateRow();
}
central_window.cw.setEnabled(true);
dispose();
}
catch(final Exception e)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new errorWindow(new javax.swing.JFrame(),e.toString()).setVisible(true);
}
});
}
}
private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String querypart2=" WHERE ";
for(int i=0;i<tflist.size();i++)
{
JTextField tf=tflist.get(i);
if(tf.getText()!=null&&!tf.getText().equals(""))
{
if(jtable.getValueAt(rowno, i)==null||jtable.getValueAt(rowno, i).equals(""))
{
querypart2=querypart2+"\""+tf.getName()+"\" IS NULL";
}
else
{
querypart2=querypart2+"\""+tf.getName()+"\"='"+jtable.getValueAt(rowno,i)+"'";
}
querypart2=querypart2+" AND ";
}
}
if(querypart2.equals(" WHERE "))
{
querypart2="";
}
else
{
querypart2=querypart2.substring(0, querypart2.length()-5);
}
try {
Statement statement = Aw_supersensible.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs=statement.executeQuery("SELECT * FROM \""+tablename+"\""+querypart2);
rs.absolute(1);
rs.deleteRow();
((DefaultTableModel)jtable.getModel()).removeRow(rowno);
Aw_supersensible.conn.commit();
central_window.cw.setEnabled(true);
dispose();
}
catch(final Exception e)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new errorWindow(new javax.swing.JFrame(),e.toString()).setVisible(true);
}
});
}
}
答案 0 :(得分:0)
这是您的delete
操作的一些伪代码,向您展示我认为“模块化”方法的样子。
我不是一位经验丰富的开发人员(事实上我不是开发人员),所以可能有更好的方法。
private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {
String tableName;
List<String> whereProperties;
List<String> whereValues;
// get your table name
// get your properties and values from the GUI in such a way that the property in whereProperties.get(n) corresponds to the value in whereValues(n)
// have a function in some class (I've called it SQL) that can take the following parameters and performs the delete operation.
SQL.delete(tableName, whereProperties, whereValues);
}