Results 1 to 1 of 1

Thread: OOP dan Database

  1. #1
    Join Date
    Oct 2001
    Location
    Kuala Lumpur, Malaysia
    Posts
    296
    Rep Power
    224

    OOP dan Database

    Ni saje nak share satu kaedah yang best gak. Ianya adalah di mana kita combinekan OOP dengan keperluan pengkalan data. Nak merepek memanjangkan ... ada yang tension dan tak paham. Trylah tengok simple kod ni ... ni adalah contoh jea. Just to deliver the idea on OOP dan database. Please comment.
    Code:
    #!/usr/bin/perl
    
    ########################################################
    #                                                      #
    # objek adalah untuk pengkalan data seperti berikut    #
    #                                                      #
    #  table : pelajar                                     #
    # +--------------+--------------+                      #
    # | pelajar_id   | int          |                      #
    # +--------------+--------------+                      #
    # | tarikh_lahir | datetime     |                      #
    # +--------------+--------------+                      #
    # | jantina      | varchar(10)  |                      #
    # +--------------+--------------+                      #
    # | nama         | varchar(100) |                      #
    # +--------------+--------------+                      #
    #                                                      #
    # objek pelajar                                        #
    # =============                                        #
    # variable                                             #
    # --------                                             #
    # pelajar_id                                           #
    # tarikh_lahir                                         #
    # jantina                                              #
    # nama                                                 #
    #                                                      #
    # method                                               #
    # --------                                             #
    # new()                                                #
    # next_id()                                            #
    # get_pelajar()                                        #
    # save()                                               #
    # get_all()                                            #
    # set_var()                                            #
    #                                                      #
    # contoh penggunaan                                    #
    # =================                                    #
    #                                                      #
    # use pelajar;                                         #
    #                                                      #
    # $pelajar = pelajar->new();                           #
    #                                                      #
    # $pelajar->set_var(                                   #
    #    nama         => "Khairil Iszuddin Ismail",        #
    #    jantina      => "lelaki",                         #
    #    tarikh_lahir => "1979-05-30");                    #
    #                                                      #
    # $pelajar->save();                                    #
    #                                                      #
    # print $pelajar->pelajar_id;                          #
    #                                                      #
    ########################################################
    
    use DBI;
    
    package Pelajar;
    
    $dbh = DBI->connect('dbi:mysql:database_name', 'username', 'password');
    
    #----------------------------#
    # membina satu objek pelajar #
    #----------------------------#
    sub new
    {
    	my $class = shift;
    	my (%params) = @_;
    	my $self = {};
    	bless $self, $class;
    
    	$self->set_var(
    		pelajar_id => '',
    		nama => '',
    		jantina => '',
    		tarikh_lahir => ''
    	);
    
    	if(defined $params{pelajar_id})
    	{
    		$self->set_var(pelajar_id => $params{pelajar_id});
    		$self->get_pelajar();
    	}
    	return $self;
    }
    
    #----------------------------------------------------#
    # mendapatkan pelajar_id berikutnya untuk rekod baru #
    #----------------------------------------------------#
    sub next_id
    {
    	my $self = shift;
    	$sql = "SELECT pelajar_id FROM pelajar ORDER BY pelajar_id LIMIT 1";
    	$sth = $dbh->prepare($sql);
    	$sth->execute();
    	my ($id) = $sth->fetchrow_array();
    	$id++;
    	$sth->finish();
    	return $id;	
    }
    
    #------------------------------------------#
    # simpan data pelajar dalam pengkalan data #
    #------------------------------------------#
    sub save
    {
    	my $self = shift;
    	my (%params) = @_;
    	
    	$b = $self->nama;
    	$c = $self->jantina;
    	$d = $self->tarikh_lahir;
    	
    	if($self->pelajar_id eq '')
    	{
    		$self->set_var(pelajar_id => $self->next_id());
    		$a = $self->pelajar_id;
    		$sql  = "INSERT INTO pelajar (pelajar_id, nama, jantina, tarikh_lahir) ";
    		$sql .= "VALUES ($a, '$b', '$c', '$d') ";
    	}
    	else
    	{
    		$a = $self->pelajar_id;
    		$sql  = "UPDATE pelajar SET nama='$b', jantina='$c', tarikh_lahir='$d' ";
    		$sql .= "WHERE pelajar_id = $a";
    	}
    	$sth = $dbh->prepare($sql);
    	$result = $sth->execute();
    	return($result);
    }
    
    #------------------------------------#
    # memulangkan semua data dalam rekod #
    #------------------------------------#
    sub get_all
    {
    	my $self = shift;
    	my @pelajar2 = ();
    	
    	$sql  = "SELECT pelajar_id, nama, tarikh_lahir, jantina ";
    	$sql .= "FROM pelajar";
    	$sth = $dbh->prepare($sql);
    	$sth->execute();
    
    	if($sth->rows() > 0)
    	{
    		while(($pelajar_id,$nama,$tarikh_lahir,$jantina) = $sth->fetchrow_array())
    		{
    			$temp = $self->new();
    			$temp->set_var(
    				pelajar_id => $pelajar_id,
    				nama => $nama,
    				tarikh_lahir => $tarikh_lahir,
    				jantina => $jantina
    			);
    			push(@pelajar2, $temp);
    		}
    	}
    	return(@pelajar2);
    }
    
    #--------------------------------#
    # memulangkan satu rekod pelajar #
    #--------------------------------#
    sub get_pelajar
    {
    	my $self = shift;
    	my $sql, $sth;
    	if($self->pelajar_id ne '')
    	{
    		$id = $self->pelajar_id;
    		$sql  = "SELECT pelajar_id, nama, tarikh_lahir, jantina ";
    		$sql .= "FROM pelajar ";
    		$sql .= "WHERE pelajar_id = $id LIMIT 1";
    
    		$sth = $dbh->prepare($sql);
    		$sth->execute();
    		if($sth->rows() > 0)
    		{
    			($pelajar_id,$nama,$tarikh_lahir,$jantina) = $sth->fetchrow_array();
    			$self->set_var(
    				pelajar_id => $pelajar_id,
    				nama => $nama,
    				tarikh_lahir => $tarikh_lahir,
    				jantina => $jantina
    			);
    			$result = TRUE;
    		}
    		else
    		{
    			$self->set_var(pelajar_id => '');
    			$result = FALSE;
    		}
    	}
    	else
    	{ $result = FALSE; }
    	return($result);
    }
    
    #------------------------------#
    # memulangkan nilai pelajar_id #
    #------------------------------#
    sub pelajar_id
    { my $self = shift; return $self->{pelajar_id}; }
    
    #------------------------#
    # memulangkan nilai nama #
    #------------------------#
    sub nama
    { my $self = shift; return $self->{nama}; }
    
    #--------------------------------#
    # memulangkan nilai tarikh_lahir #
    #--------------------------------#
    sub tarikh_lahir
    { my $self = shift; return $self->{tarikh_lahir}; }
    
    #---------------------------#
    # memulangkan nilai jantina #
    #---------------------------#
    sub jantina
    { my $self = shift; return $self->{jantina}; }
    
    #----------------------------------------#
    # membuat umpukan nilai kepada variables #
    #----------------------------------------#
    sub set_var
    {
    	my $self = shift;
    	my (%params) = @_;
    	
    	if(defined($params{nama}))
    	{ $self->{nama} = $params{nama}; }
    	
    	if(defined($params{pelajar_id}))
    	{ $self->{pelajar_id} = $params{pelajar_id}; }
    	
    	if(defined($params{jantina}))
    	{ $self->{jantina} = $params{jantina}; }
    	
    	if(defined($params{tarikh_lahir}))
    	{ $self->{tarikh_lahir} = $params{tarikh_lahir}; }
    }
    Last edited by kidino; 07-10-2002 at 06:31 PM.
    --------------------------------------------
    Khairil Iszuddin Ismail

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Emails Database
    By Kennyeoh in forum Email Marketing
    Replies: 7
    Last Post: 09-12-2009, 08:20 PM
  2. nak tarik database??
    By Linn_jie in forum Website Programming
    Replies: 13
    Last Post: 15-09-2007, 03:35 AM
  3. nk ty pasal database..
    By annuR in forum Website Programming
    Replies: 9
    Last Post: 16-03-2007, 09:10 AM
  4. universe database
    By bayam04 in forum Website Programming
    Replies: 2
    Last Post: 06-11-2004, 12:30 AM
  5. ASP & Topspeed database
    By Rizalnet in forum Website Programming
    Replies: 1
    Last Post: 07-08-2004, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112