【发布时间】:2013-06-24 05:34:43
【问题描述】:
我想从数据库中删除客户的特定报纸而不是客户的全部详细信息? 一位客户的记录中有多张纸,但我想从多张纸中删除任何纸。 我正在输入客户 ID 并选择要删除的纸张 感谢高级的帮助...
enter code here:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
public abstract class delete_paper extends JFrame implements ActionListener
{
JTextField textFieldId;
JLabel l1;
JLabel l3;
JLabel l5;
JLabel l6;
JComboBox combo;
String course[] = {"Navakal","SandhyaKal","Pudhari",
"MidDay","Inqlab","BusinessLine","MumbaiSamachar",
"GujrajSamachar","KarnatakMalla","Vartahar","PunyaNagari"};
JButton b2;
Container c = getContentPane();
delete_paper()
{
super("Shree DattaDigambar Samarth");
setBounds(140,250,777,555);
c.setLayout(null);
textFieldId = new JTextField();
l1 = new JLabel("New Customer Entry");
l3 = new JLabel("Customer Name");
l5 = new JLabel("Paper");
combo = new JComboBox(course);
l1.setBounds(10,10,340,20);
l3.setBounds(110,20,140,70);
l5.setBounds(400,50,140,20);
textFieldId.setBounds(10,70,70,20);
combo.setBounds(400,70,130,20);
b2 = new JButton("Ok");
b2.setBounds(10,160,50,20);
c.add(combo);
c.add(b2);
c.add(l1);
c.add(l3);
c.add(l5);
c.add(textFieldId);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b2.addActionListener(this);
}
public static void main(String[] args)
{
delete_paper ap=new delete_paper() {};
}
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the button");
if(e.getSource()==b2)
{
try
{
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:devendra");
try
{
java.sql.Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement("delete
from Customer where Customer_Id = ? and Paper_Name in (?) ");
ps.setString(1,textFieldId.getText());
ps.setString(4,combo.getSelectedItem().toString());
ps.executeUpdate();
JOptionPane.showMessageDialog(null,
"You successfully Enter the Entry");
}
catch (SQLException s)
{
System.out.println("SQL code does not execute.");
JOptionPane.showMessageDialog(null,"Please Enter
the Detail Correctly");
}
}
catch (ClassNotFoundException | SQLException ee)
{
System.out.println("Error:connection not created");
JOptionPane.showMessageDialog(null,"Please
Enter the Detail Correctly");
}
}
}
}
【问题讨论】:
-
添加堆栈跟踪消息。
-
对类使用 java 命名约定。不要使用 null 布局和 setBounds。定义正确的布局管理器。不要在对话框和 JOptionPanes 中使用 null parent。用一些合理的名称命名变量,而不是 label1、label2。 SQLException 应该只被捕获一次。不要在每次点击时准备声明。只需准备一次并重复使用。在您的文本中使用大写字母来帮助读者拆分您的文本。发布 SSCCE 和尽可能多的细节(合理)。不如寻求帮助。
-
@StanislavL 哇! 8 点(如果我计算正确的话)点“很好的问题提示”..作为评论.. 干得好。 :)
-
请按照@StanislavL 的建议完善您的问题,并对建议的答案发表评论;不要只问一个模糊的,新的question。
标签: java swing ms-access jdbc-odbc