المساعدة - البحث - قائمة الأعضاء - التقويم
نسخة كاملة: مساعدة في ربط الجافا بالMS Access
برمجة - شبكات - كمبيوتر - منتديات الفريق العربي للبرمجة > منتديات لغات البرمجة العام > منتدى مبرمجي لغة جافا JAVA
maas
السلام عليكم
يا شباب بغيت أتعلم كيفية ربط الجافا ببرنامج ال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
يا شباب هل لي بمساعدتكم

حصلت هالكود
كود
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
فكيف أطبق المثال على المطلوب
maas
اخر 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 " );
        
    }
}
ابو الفؤاد
تاكد ان ملف الداتا مضاف الى الـ odbc
maas
أخوي أبوالفؤاد
انه عملت الODBC من الControl Panel من الAdministrative tools و كل شي تمام بس لا أعرف لماذا تحصل لي هالمشكلة؟؟
و مشكور أخوي الدكتور عبدالرحمن على الشرح
ابو الفؤاد
إقتباس
اخر code فهمته للربط و لكنه خاطىء

ما هو الخطا ؟؟
maas
شكرا أخي تم حل المشكلة
هذه "نسخة - خفيفة" من محتويات الرئيسية للإستعراض الكامل مع المزيد من الصور والخيارات الرجاء إضغط هنا.
Invision Power Board © 2001-2009 Invision Power Services, Inc.