سلاااااااااام
عندي هذا الكود رايته في المنتدى منذ فترة ولكن اريد ان أن ادخل الى قاعدة البيانات من الكومبوبكس
ما طولش عليكم شوفو الكود:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class CrewAirplane extends JFrame {
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
JLabel jLabel9 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
Connection con;
Statement stmt;
ResultSet rs2;
public CrewAirplane() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CrewAirplane CrewAirplane = new CrewAirplane();
CrewAirplane.setSize(400,500);
CrewAirplane.setVisible(true);
}
private void jbInit() throws Exception {
//Connection to database
Statement stmt = null;
Connection con = null;
con =DatabaseConnection.getConnection();
stmt = con.createStatement();
jLabel1.setBounds(new Rectangle(114, 19, 167, 33));
jLabel1.setText("CrewAirplane page");
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setForeground(new Color(125, 0, 0));
jLabel1.setFont(new java.awt.Font("Dialog", 3, 18));
this.getContentPane().setBackground(new Color(216, 208, 230));
this.getContentPane().setLayout(null);
jLabel2.setBounds(new Rectangle(46, 80, 242, 32));
jLabel2.setText(" Select the Name of Airplane : ");
jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
jLabel2.setForeground(new Color(131, 0, 0));
jLabel2.setFont(new java.awt.Font("Dialog", 3, 14));
jComboBox1.setBounds(new Rectangle(267, 85, 111, 26));
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(ItemEvent e) {
jComboBox1_itemStateChanged(e);
}
});
jComboBox1.setEditable(true);
jComboBox1.setForeground(Color.blue);
jComboBox1.setFont(new java.awt.Font("SansSerif", 1, 12));
jButton1.setBackground(Color.blue);
jButton1.setBounds(new Rectangle(273, 420, 83, 32));
jButton1.setFont(new java.awt.Font("Dialog", 1, 14));
jButton1.setForeground(Color.white);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton1.setText(" Exit");
jButton2.setBackground(Color.blue);
jButton2.setBounds(new Rectangle(21, 420, 83, 32));
jButton2.setFont(new java.awt.Font("Dialog", 1, 14));
jButton2.setForeground(Color.white);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton2.setText(" Back");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
jButton3.setText(" ADD ");
jButton3.setForeground(Color.white);
jButton3.setFont(new java.awt.Font("Dialog", 1, 18));
jButton3.setBounds(new Rectangle(130, 420, 106, 32));
jButton3.setBackground(Color.blue);
jTextField1.setText(" ");
jTextField1.setBounds(new Rectangle(144, 139, 162, 27));
jTextField2.setText(" ");
jTextField2.setBounds(new Rectangle(144, 182, 162, 27));
jTextField3.setText(" ");
jTextField3.setBounds(new Rectangle(144, 225, 162, 27));
jTextField4.setText(" ");
jTextField4.setBounds(new Rectangle(144, 268, 162, 27));
jTextField5.setBounds(new Rectangle(144, 311, 162, 27));
//jLabel5.setText(" FirstName : ");
jLabel9.setBounds(new Rectangle(144, 354, 162, 27));
jLabel9.setBorder(BorderFactory.createLoweredBevelBorder());
jLabel9.setBackground(Color.white);
jLabel9.setFont(new java.awt.Font("Dialog", 1, 12));
jLabel9.setForeground(Color.blue);
jLabel3.setForeground(new Color(125, 0, 0));
jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel3.setText(" FirstName : ");
jLabel3.setBounds(new Rectangle(40, 180, 96, 29));
jLabel4.setForeground(new Color(125, 0, 0));
jLabel4.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel4.setText(" CrewID : ");
jLabel4.setBounds(new Rectangle(40, 139, 96, 29));
jLabel5.setForeground(new Color(125, 0, 0));
jLabel5.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel5.setText(" LastName : ");
jLabel5.setBounds(new Rectangle(40, 225, 96, 29));
jLabel6.setForeground(new Color(125, 0, 0));
jLabel6.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel6.setText(" duty : ");
jLabel6.setBounds(new Rectangle(40, 266, 96, 29));
jLabel7.setForeground(new Color(125, 0, 0));
jLabel7.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel7.setText(" NumOfYears : ");
jLabel7.setBounds(new Rectangle(40, 307, 104, 29));
jLabel8.setForeground(new Color(125, 0, 0));
jLabel8.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel8.setText(" AirplaneID : ");
jLabel8.setBounds(new Rectangle(40, 348, 96, 29));
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel3, null);
this.getContentPane().add(jLabel4, null);
this.getContentPane().add(jLabel5, null);
this.getContentPane().add(jLabel6, null);
this.getContentPane().add(jLabel7, null);
this.getContentPane().add(jLabel8, null);
this.getContentPane().add(jLabel9, null);
this.getContentPane().add(jComboBox1, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jTextField2, null);
this.getContentPane().add(jTextField3, null);
this.getContentPane().add(jTextField4, null);
this.getContentPane().add(jTextField5, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jButton3, null);
rs2= stmt.executeQuery("select AirplaneName from Airplanes");
while(rs2.next())
{
jComboBox1.addItem(rs2.getString("AirplaneName"));
}
}
void jButton1_actionPerformed(ActionEvent e) {
System.exit(0);
}
void jButton2_actionPerformed(ActionEvent e) {
dispose();
}
void jButton3_actionPerformed(ActionEvent e) {
Statement stmt = null;
Connection con = null;
try {
con =DatabaseConnection.getConnection();
stmt = con.createStatement();
stmt.executeUpdate("insert into CrewAirplane(CrewID,FirstName,LastName,duty,NumOfYears,AirplaneID) values ('"+jTextField1.getText()+"','"+jTextField2.getText()+"', '"+jTextField3.getText()+"','"+jTextField4.getText()+"','"+jTextField5.getText()+"', '"+ jComboBox1.getSelectedItem()+"')");
//stmt.executeUpdate("insert into CrewAirplane(CrewID,FirstName,LastName,duty,NumOfYears,AirplaneID) values ('1111111','wael', 'max','Pilot','2','2')");
//stmt.executeUpdate("insert into Customers(CustomerID,CustomerFirstName,CustomerLastName) values ('"+jTextField3.getText()+"','"+jTextField4.getText()+"', '"+ jComboBox1.getSelectedItem()+"')");
stmt.close();
con.close();
}
catch(Exception ex)
{
System.out.println("exceotion:"+ex);
}
}
void jComboBox1_itemStateChanged(ItemEvent e)
{
Statement stmt = null;
Connection con = null;
try {
con =DatabaseConnection.getConnection();
stmt = con.createStatement();
//System.out.println( e.getItem());
ResultSet rs2= stmt.executeQuery("select * from Airplanes where AirplaneName='"+e.getItem()+"'");
while(rs2.next())
{
jLabel9.setText(rs2.getString("AirplaneName"));
}
}catch(Exception re)
{
}
}
}والخطأ على ما أعتقد يكمن هنا:
stmt.executeUpdate("insert into CrewAirplane(CrewID,FirstName,LastName,duty,NumOfYears,AirplaneID) values ('"+jTextField1.getText()+"','"+jTextField2.getText()+"', '"+jTextField3.getText()+"','"+jTextField4.getText()+"','"+jTextField5.getText()+"', '"+ jComboBox1.getSelectedItem()+"')");:wacko:
ومتشكر على المساعدة سلفا
