عرض مشاركة واحدة
  #6  
قديم 04-08-2011, 02:37 AM
ahmed abobakr ahmed abobakr غير متواجد حالياً
عضو نشيط
 
تاريخ التسجيل: Mar 2008
المشاركات: 205
معدل تقييم المستوى: 18
ahmed abobakr is on a distinguished road
افتراضي

اوك

دلوقت عشان تجيب بيانات من الداتابيز

دى الخطوات

1- create connection
2- create statement on the current connection
3- create ResultSet to hold the result of executed statement (query)

عشان تعمل الكونكشن انت محتاج تعرف شوية حاجات

1 - نوع ال JDBC driver ال انت هتستخدمه ودا ببساطه بيعمل لودينج للداتابيز وبينفذ عليها ال sql commands

وفى الحالة دى بيبقى اسمه SQLServerDriver

ودا عشان تعرفه محتاج تعمل الخطوة دى

2-
تحمل sqljdbc.jar ودا هتلقيه متوفر على النت
وهتنفذ الخطوة دى فى البداية

right click on project name -> properties -> java build path -> liberaries -> add Jar and browse to locate the jar file

لو معملتش الخطوة دى هيقولك class not found exception

3- عنوان السيرفر ال عليه الداتابيز والبورت بتاعه واسم الداتابيز واليوزرنيم والباس ان وجد



دا الكود

Class for connecting with DB to test it u must change the url parameters

كود:

package javaapplication1;

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


public class Connect
{
    public Connection createConnection()
      {
          String url;
              Connection con = null;
            // Class.forName("com.mysql.jdbc.Driver").newInstance();
              try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
              url = "jdbc:sqlserver://AHMED-8A6808913:1433;databaseName=Test;selectMethod=cursor;integrated security=false;Trusted_Connection=no";
              try {
                con = DriverManager.getConnection(url,"sa","12345");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              if(con==null)
              {
                  System.err.print("No Connection");
              }
              try {
                if(!con.isClosed())
                        System.out.println("Successfully connected to " +
                          "SQL server using TCP/IP...");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

              return con;
      }
}
To test

كود:
public class Main {

    /**
     * *param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Connect con = new Connect();
        con.createConnection();
    }
result for testing

كود:
Successfully connected to SQL server using TCP/IP...
Now DB is Ready for ur query

to execute query you will define statement on the current connection and the write ur query and excecute

this is ex

كود:
Connect connection = new Connect();
Statement statement = null;
        Connection con = null;
ResultSet resultSet = null; //-------------------> to hold result of query
con = connection.createConnection();  //---------------------->> connection created

// create statement and define the type of result set

statement = con.createStatement(Room_resultSet.TYPE_SCROLL_SENSITIVE,Room_resultSet.CONCUR_UPDATABLE);

String query = "Select * from table where Class"; 

//////// excecuteeeeeee
resultSet = statement.executeQuery(query);
after that
you have resulrtset contains data u are looking for

retrieve the data and work


اسف انى دخلت اللغات بس عشان معظم الكلام مينفعش عربى

عموما انا موجود فى اى وقت عشان لو فى حاجة مش واضحة او فى مشاكل اخرى

السلام عليكم
__________________
اللهم انى استغفرك واتوب اليك
فأغفرلى
انه لا يغفر الذنوب الا انت