Java 如何使用SQL和Netbeans从数据库中删除行?
我对netbeans有一个问题。 我想在按下jbutton后从数据库中删除一行,但我不能 我有类似的错误:Java 如何使用SQL和Netbeans从数据库中删除行?,java,sql,swing,Java,Sql,Swing,我对netbeans有一个问题。 我想在按下jbutton后从数据库中删除一行,但我不能 我有类似的错误: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ca
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'car_manufacturing,modelyear,carprice'
下面是我的代码:
package cargestion;
import javax.swing.table.DefaultTableModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
*
*/
public class FenetreListeOutil extends javax.swing.JFrame {
Statement stmt;
Statement stmtcarlist;
Connexion myconnexion = new Connexion();
/**
* Creates new form Fenetrecarlist
*/
public Fenetrecarlist() {
initComponents();
DefaultTableModel model = new DefaultTableModel();
model.addColumn("NAMECAR");
model.addColumn("CAR MANUFACTURING");
model.addColumn("Year");
model.addColumn("PRICE");
String requetecarlist = "select *from car";
try {
stmtcarlist = myconnexion.ObtenirConnexion().createStatement();
ResultSet resultat = stmtcarlist.executeQuery(requetecarlist);
while (resultat.next()) {
model.addRow(new Object[]{resultat.getString("namecar"), resultat.getString("car_manufacturing"), resultat.getString("Modelyear"), resultat.getString("carprice")});
}
} catch (SQLException ex) {
System.out.println(ex);
}
Tablecar.setModel(model);
}
/**
* This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Tablecar = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cargestion.cargestionApp.class).getContext().getResourceMap(Fenetrecarlist.class);
jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
TableOutil.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
TableOutil.setName("TableOutil"); // NOI18N
jScrollPane1.setViewportView(TableOutil);
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(125, 125, 125)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sql = "DELETE FROM `car` " + "WHERE namecar=,car_manufacturing,Modelyear,carprice";
try {
stmt = myconnexion.ObtenirConnexion().createStatement();
stmt.executeUpdate(sql);
} catch (SQLException ex) {
System.err.println(ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Fenetrecarlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Fenetrecarlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Fenetrecarlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Fenetrecarlist.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Fenetrecarlist().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTable TableOutil;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration }
在相等的simbol后面有一个冒号,通常在一句话中删除条件是表的主键 试试这个
DELETE FROM car WHERE <primary_key_column>=<value>
注意。查询语法在每列之后无效
String sql = "DELETE FROM `car` " +"WHERE namecar=,car_manufacturing,Modelyear,carprice";
如果要基于多个列值删除,请在where子句中使用and运算符
编辑:
但必须在where子句中使用unique id来删除记录。否则,其他一些记录意味着也将删除重复记录。您的SQL语法无效。首先,在jButton1ActionPerformed中,您将获得所有列名称car、car_manufacturing、Modelyear、carprice值@GOKU45它可以工作,但为什么我每次都必须写相应的列值。在删除查询中,您只提到了多个列。在这里,您也可以使用一列。这是根据您的要求。欢迎来自印度。查看我的编辑更新@Goku45我不明白,我需要在“some1”和“some2”中写些什么?我可以举个例子吗?代替some1等填写您各自的列值。这是可行的,但没有通用方法可以不强制每次写入各自的列值。我想用鼠标在表中选择一行,然后单击“删除”按钮删除该行。因此,请使用鼠标单击“侦听器”获取一行。在“删除”按钮下,在此处获取上述选定行的详细信息。将这些值传递给查询@Goku45能给我举个例子吗?@Goku45这个例子?delete语句,主键,?Goku45这是一个例子
String sql = "DELETE FROM `car` WHERE namecar='some1' and car_manufacturing='some2' and Modelyear='some3' and carprice='some4'";
String sql = "DELETE FROM `car` WHERE namecar='some1';//single column in where clause