我想編寫一個程序,允許用戶連接,查看和添加或刪除數據庫中的值。我被卡住了鞦韆部分。當我選擇一個組合框選項沒有發生,但我想創建一個像mysql工作臺的視圖。它假設是這樣的;用戶從組合框中選取一個表名稱,並可以從該表格和文本字段中查看列名稱,以在列名稱上添加新值或現有值。通過選擇JComboBox選項動態添加JTextFields和JLabels
我的代碼是這樣的,到目前爲止:
public class DBC extends JFrame{
static String tablo;
static JTextField tf = new JTextField(20);
static int columnCount;
static JPanel tfPanel = new JPanel();
static JLabel depName = new JLabel("Name");
static JLabel depLocation = new JLabel("Location");
static Box box = new Box(BoxLayout.Y_AXIS);
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project"
,"root","123456789");
final Statement statement = connect.createStatement();
JLabel tabloSec = new JLabel("Tablo Seçin:");
final JComboBox<String> tablolar = new JComboBox<String>();
DatabaseMetaData md = connect.getMetaData();
final ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
tablolar.addItem(rs.getString(3));
}
tablolar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
tablo = tablolar.getSelectedItem().toString();
try {
columnCount = rs.getMetaData().getColumnCount();
for(int i=0;i<=columnCount;i++){
box.add(tf);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
JButton ekle = new JButton("Ekle");
ekle.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
switch(tablo){
case "department":
statement.executeUpdate("INSERT INTO department(Name,Location) VALUES('"+tf.getText()+"')");
case "employee":
statement.executeUpdate("INSERT INTO employee(Id,FirstName,LastName,Sex,Address,Email,Salary,BirthDate,JoinDate) VALUES('"+tf.getText()+"')");
case "engineer":
statement.executeUpdate("INSERT INTO engineer(EngineerType) VALUES('"+tf.getText()+"')");
case "manager":
statement.executeUpdate("INSERT INTO manager(Department) VALUES('"+tf.getText()+"')");
case "project":
statement.executeUpdate("INSERT INTO project(Name,Number,Value) VALUES('"+tf.getText()+"')");
case "secretary":
statement.executeUpdate("INSERT INTO secretary(TypingSpeed) VALUES('"+tf.getText()+"')");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
JButton cik = new JButton("Çık");
cik.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel panel = new JPanel(new GridLayout(4,3));
panel.add(tabloSec);
panel.add(tablolar);
panel.add(box);
panel.revalidate();
panel.add(ekle);
panel.add(cik);
JFrame frame = new JFrame("Deneme");
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
如果您的問題與Swing相關,請嘗試創建示例以在此處發佈不需要數據庫的示例。然後其他人可以輕鬆調試您的問題。 – 2013-03-24 19:57:00