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

مساعدة في ربط الجافا بالMS Access

مغلق
بدأه maas في 31 يوليو 2007 · 6 رد · 1,070 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

يا شباب بغيت أتعلم كيفية ربط الجافا ببرنامج الJCreator مع MS Access

فحصلت على هذا الكود و لكني لم أفهمه

فأرجو منكم التوضيح

import java.sql.*;

public class ConnectToAccess
{
	public static void main(String args[])
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			Connection conn = DriverManager.getConnection("jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);
				DBQ=D:\\path_to_db\db.mdb;PWD=mypass","login","password");
			Statement command = conn.createStatement();
			ResultSet rs = command.executeQuery("select * FROM keyword");
			while (rs.next())
			{
				System.out.println(rs.getString(2));
			}

			System.out.println("Connected To Access");

		}
		catch (SQLException se)
		{
			System.out.println(se);
		}
		catch (Exception ex)
		{
			System.out.println(ex);
		}
	}
}

فهل هذا الكود صحيح أم لا؟؟؟ أو هل توجد أمثلة للتعلم

انه مبتدىء في الqueries و أريد عمل insert و من ثم عرض اللي عملت له insert فهل

يكون الcode قبل كود الربط بين الجافا و الMS Acces أم بعد ؟؟

و مشكورين

تم تعديل هذه المشاركة بواسطة maas في 31 يوليو 2007 في 19:38

#2

يا شباب هل لي بمساعدتكم

حصلت هالكود

class Base{ 
	//paramètres JDBC : 
	String driver ="com.mysql.jdbc.Driver"; 
	String bridge ="Jdbc:mysql:"; 

	//propriétés: 
	String databaseName ="zaki"; 
	Connection db; 
	DatabaseMetaData DBM; 

	Base(String user, String pass){	 
		try{ 
			// 1.charger le Driver 
			Class.forName(driver); 
			// 2.Etablir la connection 
			String url=bridge+"//localhost/" + databaseName; 
			db=DriverManager.getConnection(url,user,pass); 
			DBM =db.getMetaData(); 
			System.out.println("Conexion Etablie"); 
		//	printInfos(); 
			testSelect(); 
		} 
		catch(Exception e){ 
			System.out.println("Erreur BD :"+e.getMessage()); 
		} 
	} 

	void printInfos(){ 
		try{ 
			System.out.println("DriverName :"+DBM.getDriverName()); 
			System.out.println("DriverVersion :"+DBM.getDriverVersion()); 
			System.out.println("URL :"+DBM.getURL()); 
			System.out.println("UserName :"+DBM.getUserName()); 
		} 
		catch(Exception e){} 
	} 

		void testSelect(){ 
		try{ 
		String req="SELECT * FROM HP "; 
		Statement  sql=db.createStatement();			 
		ResultSet rs= sql.executeQuery(req); 
		while(rs.next()){ 
			for(int i=1;i<=3;i++) 
				System.out.print(rs.getString(i)+" "); 

			System.out.println(); 
		} 
		} 
		catch(Exception e){} 
	} 

	void testSelect2(){ 
		try{ 
		String req="SELECT * FROM HP"; 
		Statement  sql=db.createStatement();			 
		ResultSet rs= sql.executeQuery(req); 

		ResultSetMetaData RSM=rs.getMetaData(); 
		int nc=RSM.getColumnCount(); 
		for(int i=1;i<=nc;i++){ 
			System.out.println( 
				RSM.getColumnName(i)+ " : " + 
				RSM.getColumnTypeName(i)+" ,"+ 
				RSM.getColumnDisplaySize(i) 
			); 
		} 
		} 
		catch(Exception e){} 
	} 

	public static void main(String args[]){ 
		new Base("root","zaki"); 
	} 
	}

الdatabase اللي عندي اسمها information و فيها entities الname, telephone, age

فكيف أطبق المثال على المطلوب

#3

اخر code فهمته للربط و لكنه خاطىء

import java.sql.*;
public class Project3 {

	public static void main(String[] args) {

		try {

			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// set this to a MS Access DB you have on your machine
			String filename = "C:\Documents and Settings\Information.mdb";
			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
			database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
			// now we can get the connection from the DriverManager
			Connection con = DriverManager.getConnection( database ,"",""); 
		}


			catch (Exception e) {

			System.out.println("An error Occurred in Connecting with the DB " );

	}
}
#4

تاكد ان ملف الداتا مضاف الى الـ odbc

منقطع من حين لاخر

#5

أخوي أبوالفؤاد

انه عملت الODBC من الControl Panel من الAdministrative tools و كل شي تمام بس لا أعرف لماذا تحصل لي هالمشكلة؟؟

و مشكور أخوي الدكتور عبدالرحمن على الشرح

#6
اقتباس
اخر code فهمته للربط و لكنه خاطىء

ما هو الخطا ؟؟

منقطع من حين لاخر

#7

شكرا أخي تم حل المشكلة

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

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