الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف أدخل بيانات عن طريق Combobox ؟

بدأه waelmax في 13 يوليو 2008 · 4 رد · 816 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

سلاااااااااام

عندي هذا الكود رايته في المنتدى منذ فترة ولكن اريد ان أن ادخل الى قاعدة البيانات من الكومبوبكس

ما طولش عليكم شوفو الكود:

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:

ومتشكر على المساعدة سلفا

#2

يعني أنا اريد أضافة المعلومات الى جدول:

CrewAirplane

وملىء هذة الحقول:

CrewID

FirstName

LastName

duty

NumOfYears

والحقل الاخير اريد أن أخذه من جدول اخر عن طريق الكومبوبكس: AirplaneID

وهو من :

 rs2=  stmt.executeQuery("select AirplaneName from Airplanes");
#3

هذه الفئة غير موجوة DatabaseConnection

من أين حصلت على المثال؟

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#4

أسف جدا

ده كلاس :

		/*
			* DatabaseConnection is a Class that contains a static function
			* which is responsible for configuring the Driver Manager and establishing
			* the connection with the AirLineDatabase.mdb which is placed in the project's 
			* root folder.
			* so, instead of writing:
		String dbuser = " ";
		String dbpasswd = " ";
		String DriverPrefix = "jdbc:odbc:";
		String DataSource = "AirLineDatabase";


		//This loads the driver for the database
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,""+e.getMessage(),"Database Driver Error", JOptionPane.WARNING_MESSAGE);
		}

		Statement stmt = null;
		Connection con = null;

		//Creates connection to the database.
		try {
		con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser, dbpasswd);
		stmt = con.createStatement();

		* all you nead to write is:
		Statement stmt = null;
		Connection con = null;

		//Creates connection to the database.
		try {
		con = (Connection) DatabaseConnection.getConnection();
		stmt = con.createStatement(); 

		*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection //Static class for connecting to a database
{

	private static String strDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; //Only can do MS Access at the momment

	public static Connection getConnection () throws Exception
	{
		String dbuser =" ";
		String dbpasswd =" ";
		String DriverPrefix = "jdbc:odbc:";
		String DataSource = "AirLineDatabase.mdb";
		Connection databaseConnection;

		try
		{

			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Asumming it tests if the driver is supported
			strDriver += DataSource.trim() + ";DriverID=22;READONLY=true}"; //Adds to the driver the database address and extra stuff
			databaseConnection = DriverManager.getConnection(strDriver, dbuser, dbpasswd); //Create the connection

		}
		catch (ClassNotFoundException ex) //Thrown if driver is not reconised
		{		   
			throw new Exception("\nError loading database access driver.\nPossible reason is that you do not have MS Access");			
		}
		catch (SQLException ex) //Thrown if database can not be found
		{			
			throw new Exception("\nError connecting to database\n" + System.getProperty("user.dir") + "\\" + DataSource + "\nPlease make sure the database exists");			
		}

		return databaseConnection;

	}

}
#5

متشكرين قوي

وشكر خاص لعلاء الدين

حليتها:

:lol:

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();
  JComboBox jComboBox1 = new JComboBox();
  JComboBox jComboBox2 = 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();
 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,460);
	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 Airplane ID : ");
	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.setEditable(true);
	jComboBox1.setForeground(Color.blue);
	jComboBox1.setFont(new java.awt.Font("SansSerif", 1, 12));
	jButton1.setBackground(Color.blue);
	jButton1.setBounds(new Rectangle(273, 380, 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, 380, 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, 380, 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));
	jComboBox2.setForeground(new Color(225, 0, 0));
	jComboBox2.setFont(new java.awt.Font("Dialog", 1, 12));
	jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pilot", "Stewardess" }));
	jComboBox2.setBounds(new Rectangle(144, 268, 162, 27));
	jTextField4.setBounds(new Rectangle(144, 311, 162, 27));
	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));


	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(jComboBox1, null);
	this.getContentPane().add(jComboBox2, null);
	this.getContentPane().add(jTextField1, null);
	this.getContentPane().add(jTextField2, null);
	this.getContentPane().add(jTextField3, null);
	this.getContentPane().add(jTextField4, null);
	this.getContentPane().add(jButton2, null);
	this.getContentPane().add(jButton1, null);
	this.getContentPane().add(jButton3, null);


	rs2=  stmt.executeQuery("select AirplaneID from Airplanes");
	while(rs2.next())
	{
	jComboBox1.addItem(rs2.getString("AirplaneID"));
	 }
  }
  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()+"','"+ jComboBox2.getSelectedItem()+"','"+jTextField4.getText()+"','"+ jComboBox1.getSelectedItem()+"')");
			//stmt.executeUpdate("insert into CrewAirplane(CrewID,FirstName,LastName,duty,NumOfYears,AirplaneID) values ('1111113','wael', 'ayoub','gft','12','2')");



  stmt.close();
  con.close();
  }
  catch(Exception ex)
	{
	  System.out.println("exceotion:"+ex);
	}
  }



  }

مواضيع مشابهة