import java.awt.Container;
import java.awt.GridLayout;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class DeleteStaff implements ActionListener
{
JDialog dialog;
//JTextField tf=new JTextField();
public DeleteStaff(JFrame jf)
{
dialog=new JDialog(jf,"Staff Delete");
Container dialogPane = dialog.getContentPane();
dialogPane.setLayout(new GridLayout(2,2));
dialogPane.add(new JLabel("Please input StaffNo:",SwingConstants.CENTER));
//dialogPane.add(tf);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection("jdbc:odbc:MS","test","1234");
Statement stmt;
stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("Select * From Person");
int i=0;
while(rs.next())
{
i++;
}
int n;
n=i;
String data[]=new String[n];
while(rs.next())
{
data[i]=rs.getInt("ID")+" ";
}
JComboBox combo=new JComboBox(data);
combo.setEditable(true);
ComboBoxEditor editor=combo.getEditor();
combo.configureEditor(editor, "please choose or input the ID");
stmt.close();
con.close();
dialogPane.add(combo);
JButton b1=new JButton("EnSure");
dialogPane.add(b1);
JButton b2=new JButton("Cancle");
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
dialog.setBounds(200,150,500,100);
dialog.setVisible(true);
}
catch(Exception ex){}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("EnSure"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MS","test","1234");
String sq="DELETE FROM Person WHERE ID=?";
PreparedStatement pstmt=con.prepareStatement(sq);
int ID=Integer.parseInt(combo.getSelectedItem().toString());
pstmt.setInt(1, ID);
pstmt.executeUpdate();
pstmt.close();
con.close();
JOptionPane.showMessageDialog(dialog,"Delete Success","Success",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){}
}
else if(cmd.equals("Cancle"))
{
dialog.dispose();
}
}
}
红色的一行是错误的
import java.awt.GridLayout;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class DeleteStaff implements ActionListener
{
JDialog dialog;
//JTextField tf=new JTextField();
public DeleteStaff(JFrame jf)
{
dialog=new JDialog(jf,"Staff Delete");
Container dialogPane = dialog.getContentPane();
dialogPane.setLayout(new GridLayout(2,2));
dialogPane.add(new JLabel("Please input StaffNo:",SwingConstants.CENTER));
//dialogPane.add(tf);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection("jdbc:odbc:MS","test","1234");
Statement stmt;
stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("Select * From Person");
int i=0;
while(rs.next())
{
i++;
}
int n;
n=i;
String data[]=new String[n];
while(rs.next())
{
data[i]=rs.getInt("ID")+" ";
}
JComboBox combo=new JComboBox(data);
combo.setEditable(true);
ComboBoxEditor editor=combo.getEditor();
combo.configureEditor(editor, "please choose or input the ID");
stmt.close();
con.close();
dialogPane.add(combo);
JButton b1=new JButton("EnSure");
dialogPane.add(b1);
JButton b2=new JButton("Cancle");
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
dialog.setBounds(200,150,500,100);
dialog.setVisible(true);
}
catch(Exception ex){}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("EnSure"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MS","test","1234");
String sq="DELETE FROM Person WHERE ID=?";
PreparedStatement pstmt=con.prepareStatement(sq);
int ID=Integer.parseInt(combo.getSelectedItem().toString());
pstmt.setInt(1, ID);
pstmt.executeUpdate();
pstmt.close();
con.close();
JOptionPane.showMessageDialog(dialog,"Delete Success","Success",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){}
}
else if(cmd.equals("Cancle"))
{
dialog.dispose();
}
}
}
红色的一行是错误的
解决方案 »
- JComboBox
- 如何使用 XMMP 协议与 openfire交互
- [求助]想用java做个挂.但必须有个前提就是自动实现鼠标的按住左键移动
- 如何练就C++,JAVA或.NET大型项目经验?
- 如何取出数组每个中的元素并去掉每个元素中左右的空格?
- java中""和null者有什么区别?
- java.util.logging 怎么用啊
- 为什么用"package"无法使生成的class文件到我指定的文件夹中?
- jTextField的focus
- 请教getCommittedVirtualMemorySize() 到底返回的是什么?
- 又一条关于颜色的问题,打印出来的结果为什么是很多带颜色的小方块?
- 请大家帮忙急急急!求java swing做一个Login panel谢谢!
如果需要访问,把combo写成这个类的一个field变量.
作用域只在try中的
可以定义为全局变量来访问