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

Class for database

مغلق
بدأه khwlani في 7 يونيو 2003 · 1 رد · 939 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
//
// © Copyright by Khwlani
//
package database;
import java.io.*;
import java.util.*;
import java.sql.*;

/*----------------------------------------------------------------------*/
/**
 * Class for connect with database  

 *
 * @author   Al-Khwlani
 * @version V1.0
 * @since   V1.0 05.06.2003 Khaled Al-Khwlani           
 */

public class RunSql
{


    protected int  _dbErrorStaus=0;
    public  String _treiber;
    public Vector _dbError=null;
    protected boolean _connectError=false;
    protected String _user;
    protected String _password;
    protected String _database;
    protected Connection _conn;
    protected Statement _stmt;
    protected ResultSet _result;

  /**
      *  Constructor  for Oracle  

      *      
      * @author  Al-Khwlani
      * @since   V1.0  05.06.2003 Al-Khwlani 

      */
   public RunSql(String user,String passwd,String databasename, String database)throws Exception
   {
                this._dbError=new Vector();
                this._user =user;
                this._password=passwd;
                this._database=databasename; 
                if(this._database.toUpperCase().equals("ORACLE"))
                   oracleconnect();
                else 
                 odbcConnect();
   }
   /**
      *  Constructor  for odbc 

      *      
      * @author  Khaled Al-Khwlani
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */
   public RunSql(String databasename)throws Exception
   {


                this._dbError=new Vector();
                this._user ="";
                this._password="";
                this._database=databasename; 
                odbcConnect();
   }

  /**
      *  Connect  for Access 

      *      
      * @author  Khaled Al-Khwlani
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */         

   public void odbcConnect()throws Exception
   {

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String temp="jdbc:odbc:"+this._database;
        this._conn = DriverManager.getConnection(temp);
         if(this._conn!=null)
           {
           this._stmt = _conn.createStatement();
           System.out.println("connected.");
           }
           this._connectError=false;
   }



     /**
      *  return the connecErorr 

      *      
      * @author  Khaled Al-Khwlani
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */

   public boolean getConnectError()
   {
    return this._connectError;
   }

    /**
      *  Connect  for Oracle database 

      *      
      * @author  Al-Khwlani
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */

   public void oracleconnect()throws Exception
   {
        System.out.println("Connection well excute, please wait a moment");
        //if you have the Oracle-dreiver please aktiv the flow line, Oracle-Driver is in classes12.zip
         //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
           this._conn = DriverManager.getConnection(this._database, _user, _password);
           if(this._conn!=null)
           {
           this._stmt = _conn.createStatement();
           System.out.println("connected.");

           }
           this._connectError=false;


    }

 /**
      *  excute Select-Statement  

      *      
      * @author   Al-Khwlani
      * @param   SqlString: Select-Statement
      * @return Vector for result
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */  

 public Vector runSelect(String SqlString)throws Exception//Vector Von Vector 
   {

        Vector rows=new Vector();
            int k=0;
        {
        this._result= this._stmt.executeQuery(SqlString);
		int n=  _result.getMetaData().getColumnCount();
		int i = 1;
		while(this._result.next())
		{
		    k++;
		    Vector  cols=new Vector();
		   for(i=1;i<=n;i++)
			{
			   String erg=_result.getString(i);
			    if(erg == null)
			         erg="";
			         cols.add(erg);
			}
			rows.add(cols);
		}

		}
        return rows;
   }


 /**
      *  for insert or update statemen in the database   

      *      
      * @author   Al-Khwlani
      * @param   str: update/insert Statement
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */

public int  excute(String str)throws Exception
{
    boolean ok=false;
    int anz=-1;
    System.out.println(str);
    anz=_stmt.executeUpdate(str);
    return anz;
 }

  /**
      *  close the database   

      *      
      * @author  Al-Khwlani
      * @since   V1.0 05.06.2003 Al-Khwlani 

      */
    public void closedatabase()throws Exception
 {

     if( _result!=null) 
         _result.close();
    if(_stmt!=null)
        _stmt.close();
    if(_conn!=null)
        _conn.close();
    System.out.println("Database is closed.");

 }
     public static void main(String args[])
     {

          RunSql rsql=null;
          try
         {
          rsql=new RunSql("FPNWIND");
         //Test for Selectstatment
         String str="select * from personal";
         Vector v=rsql.runSelect(str);
         if(v!=null&&v.size()>0)
         {
            Vector row;
            String value;

            for(int i=0;i            {
                row=(Vector)v.elementAt(i);
                for(int j=0;j                {
                  value= (String)row.elementAt(j); 
                  System.out.print(value+"t");
                }
                System.out.println();
            } 
         }
         //end of select

         //test for update
         str="update personal set vorname='khwlani' where personalNr=1";
         int anz=rsql.excute(str);
        System.out.println(""+anz+"  rows are changed");
         // end update
                  }
         catch(Exception e)
            {
             System.out.println(e.toString());
            }


         try
         {
        	 rsql.closedatabase();
         }
         catch(Exception ex)
         {
          System.out.println(ex.toString());   
         }


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

لتحقيق المثال السابق اتبع الخطوات التالية

1. FPNWIND مثال موجود في Access2000 تحت الدليل C:programmeMicrosoft OfficeOffice101031

2. عرف FPNWIND في ODBC

3. عدل اسم الجدول في الclass من pesonal الى اسم جدول موجود في هذه ال database

4. عدل اسم العمود في ال class الى اسم موجود في الجدول المراد تعديله

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

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

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