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

سؤال

مغلق
بدأه صلاح الثبيتي في 29 يونيو 2003 · 6 رد · 829 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class addBooks extends JDialog {
	private Connection connection = null;
	private Statement statement = null;
	private ResultSet resultset = null;

	private JPanel northPanel = new JPanel();
	private JLabel northLabel = new JLabel("Add the information for the book...");

	private JPanel centerPanel = new JPanel();

	private JLabel bookIDLabel;
	private JTextField bookIDTextField = new JTextField();
	private String bookID = null;

	private JLabel titleLabel;
	private JTextField titleTextField = new JTextField();
	private String title = null;

	private JLabel autherLabel;
	private JTextField autherTextField = new JTextField();
	private String authers = null;

	private JLabel publisherLabel;
	private JTextField publisherTextField = new JTextField();
	private String publisher = null;

	private JLabel editionNumberLabel;
	private JTextField editionNumberTextField = new JTextField();
	private String editionNumber = null;

	private JLabel copyrightLabel;
	private JTextField copyrightTextField = new JTextField();
	private String copyrightYear = null;

	private JLabel pagesLabel;
	private JTextField pagesTextField = new JTextField();
	private String pages = null;

	private JLabel ISBNLabel;
	private JTextField ISBNTextField = new JTextField();
	private String ISBN = null;

	private JPanel southPanel = new JPanel();
	private JButton insertInformationButton = new JButton("Insert the Information");

	private JLibrary l;

	public addBooks(JLibrary l) {
		this.l = l;
	}
	public void addBooks(Frame frame, String _title, boolean model) {
		setTitle(_title);

		Container cp = getContentPane();

		northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		northLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		northPanel.add(northLabel);
		cp.add("North", northPanel);

		centerPanel.setLayout(new GridLayout(8,8,1,1));
		centerPanel.add(bookIDLabel = new JLabel("  Write the Book ID:"));
		centerPanel.add(bookIDTextField);
		centerPanel.add(titleLabel = new JLabel("  Write the title:"));
		centerPanel.add(titleTextField);
		centerPanel.add(autherLabel = new JLabel("  Write the name of the auther:"));
		centerPanel.add(autherTextField);
		centerPanel.add(publisherLabel = new JLabel("  Write the name of the publisher:"));
		centerPanel.add(publisherTextField);
		centerPanel.add(editionNumberLabel = new JLabel("  Write the Edition Number:"));
		centerPanel.add(editionNumberTextField);
		centerPanel.add(copyrightLabel = new JLabel("  Write the copyright for the book:"));
		centerPanel.add(copyrightTextField);
		centerPanel.add(pagesLabel = new JLabel("  Write the number of pages:"));
		centerPanel.add(pagesTextField);
		centerPanel.add(ISBNLabel = new JLabel("  Write ISBN for the book:"));
		centerPanel.add(ISBNTextField);
		cp.add("Center", centerPanel);

		southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		southPanel.add(insertInformationButton);
		cp.add("South", southPanel);

		insertInformationButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				bookID = bookIDTextField.getText();
				title = titleTextField.getText();
				authers = autherTextField.getText();
				publisher = publisherTextField.getText();
				editionNumber = editionNumberTextField.getText();
				copyrightYear = copyrightTextField.getText();
				pages = pagesTextField.getText();
				ISBN = ISBNTextField.getText();
				try {
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				}
				catch(Exception e) {
					System.out.println(e.toString());
				}
				try {
					connection = DriverManager.getConnection("jdbc:odbc:Library");
					statement  = connection.createStatement();
					statement.executeUpdate("INSERT INTO Books (bookID,Title,authers,"+
					"publisher,editionNumber,copyrightYear,Pages,ISBN) VALUES ("+bookID+","+title+","+authers+","+
					publisher+","+editionNumber+","+copyrightYear+","+pages+","+ISBN+")");
					statement.close();
					connection.close();
				}
				catch(SQLException SQLe) {
					System.out.println(SQLe.toString());
				}
			}
		});

		setResizable(false);
		setVisible(true);
		pack();
	}
}

كتبت هذا الكود لادخال بيانات كتاب الى قاعده البيانات لكن المشكله اللي تطلع لي هي :

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

وين المشكله بالضبط ؟؟

#2

والله المشكلة

لازم تكون عندي قاعدة البيانات حتى استطيع تشغيل البرنامج ومعرفة الخطأ

لكن

مبدئيا

اعتقد الخطأ هو

في

SQL statment

اللي انت كتبتها

لان في بعض القيم عبارة عن String

وال String لما تسوي لها Insert

لازم تضعه بين هذه العلامة ' دائما قبل وبعد اي قيمة تكون من النوع String

وبعدين انا افترض الان ان جميع القيم اللي انت عرفتها في قاعدة البيانات من النوع String

ولا يوجد فيها اي قيمة من النوع Int او Number

لانك الان تدخل البيانات كلها على شكل String

شئ اخر ايضا

بالنسبةلل Insert

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

فلا تحتاج انك تضع اسماء المتغيرات في ال SQL فقط ضع القيم بالترتيب

جرب الكود اللي تحت وضعه بدل الكود اللي انت كتبته

اذا نفع ... الحمد لله

اذا ما نفع

ارجو ان تكتب وصف لقاعدة البيانات اللي انت عملتها

وسوف اجربها عندي واعرف ما المشكلة

لكن اتمنى الكود اللي بالاسفل يحل المشكلة

.....

جرب هذا الكود

	statement.executeUpdate("INSERT INTO Books VALUES ('"+bookID+"','"+title+"','"+authers+"','"+
              publisher+"','"+editionNumber+"','"+copyrightYear+"','"+pages+"','"+ISBN+"')");
#4

اشتغل البرنامج ؟

#5

:D

#7

الرجاء الألتزام بقواعد المنتدى

اللهم انا نسألك علما نافعا ونعوذ بك من حماقة الجهلاء

هذا الموضوع مغلق.

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