Top Banner

of 13

Modul Prokom2

Oct 13, 2015

Download

Documents

Redondo Asprila

dari DTM
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Modul Praktik Pemrograman Komputer II

    Pengolahan DATABASE Berbasis MYSQL Menggunakan Bahasa Pemrograman Microsoft Visual C++ 2010 Express

    disusun oleh : TIM LABORATORIUM KOMPUTASI

    PROGRAM DIPLOMA TEKNIK MESIN

    SEKOLAH VOKASI UGM

    2013

  • Menggunakan Bahasa Pemrograman Microsoft Visual C++ 2010 Express

    1. Membuat Project Baru

    Pilih New Project

  • 2. Menggunakan Label,textBox,Button

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { float a,b,c; a=Convert::ToDouble(textBox1->Text); b=Convert::ToDouble(textBox2->Text); c=a+b; label5->Text = Convert::ToString(c); textBox3->Text = Convert::ToString(c); }

    3. Menggunakan ComboBox,ListBox,GroupBox,RadioButton,CeckBox

  • //untuk comboBox

    if (comboBox1->SelectedIndex == 0) c=a+b; if (comboBox1->SelectedIndex == 1) c=a-b; if (comboBox1->SelectedIndex == 2) c=a*b; if (comboBox1->SelectedIndex == 3) c=a/b;

    //untuk listBox

    if (listBox1->SelectedIndex == 0) c=a+b; if (listBox1->SelectedIndex == 1) c=a-b; if (listBox1->SelectedIndex == 2) c=a*b; if (listBox1->SelectedIndex == 3) c=a/b; //untuk radioButton if (radioButton1->Checked) c=a+b; if (radioButton2->Checked) c=a-b; if (radioButton3->Checked) c=a*b; if (radioButton4->Checked) c=a/b; //untuk checkBox

    if (checkBox1->Checked) if (checkBox1->Checked) // untuk tombol Hapus

    textBox1->Text=""; textBox2->Text=""; textBox3->Text=""; label5->Text="xxxxxxxxxx"; comboBox1->SelectedIndex=-1; listBox1->SelectedIndex=-1;

    4. Menggunakan MassageBox

  • private: virtual System::Void button1_Click(System::Object^ sender, System::EventArgs^ e){ float a,b,c; a=Convert::ToDouble(textBox1->Text); if (a>0)label4->Text = "Positif"; if (aText = "Negatif"; if (a==0)MessageBox::Show("Input tdk boleh nol !!"); }

    Membuat Database Menggunakan MySQL Server 5.5 dan MySQL Front

    1. Membuka MySQL Server 5.5

  • 2. Membuka MySQL Front

    3. Membuat Database dan Tabel

  • Perintah-perintah yang digunakan :

    1. Untuk Melihat Data (SELECT)

    SELECT * FROM tbl_sparepart

    SELECT (nama) FROM tbl_sparepart

    SELECT (nama,stok) FROM tbl_sparepart

    SELECT * FROM tbl_sparepart order by nama

    SELECT * FROM tbl_sparepart order by stok

    SELECT * FROM tbl_sparepart where nama like '%olie'

    SELECT * FROM tbl_sparepart where id >= 5

    SELECT COUNT(nama) FROM tbl_sparepart

    SELECT MAX(stok) FROM tbl_sparepart

    SELECT MIN(stok) FROM tbl_sparepart

    SELECT AVG(stok) FROM tbl_sparepart

    2. Untuk Meng-Update Data (UPDATE)

  • UPDATE tbl_sparepart set stok = 0 where id = ...

    UPDATE tbl_sparepart set nama = 'xxx' where id = ...

    3. Untuk Menambah Data (INSERT)

    INSERT INTO tbl_sparepart(nama,stok) values('olie curah', 500)

    4. Untuk Menghapus Data (DELETE)

    DELETE FROM tbl_sparepart where id = 1

    Pengolahan DATABASE

  • 1. Step-step akses DB lewat VC++ 2010 a. Step koneksi

    1. Tambahkan reference ke MySql.Data ke Project Anda

    2. Tambahkan namespace MySql, yaitu : using namespace MySql::Data::MySqlClient;

    3. Siap membuat program

  • a. string utk parameter DB

    - nama_db

    - alamat db

    - port db

    - username

    - password

    Contoh : String^ nama_object =L"database=

    db_toko;datasource=localhost;port=3306;username=root;password=" ;

    String^ xxx = L"database=db_toko;datasource=localhost;port=3306;username=root;password=" ;

    b. Mendeklarasikan object MySqlConnection

    Contoh : MySqlConnection^ nama_object = gcnew

    MySqlConnection(nama_variabel_berisi_parameter_db);

    MySqlConnection^ yyy= gcnew MySqlConnection(xxx);

    c. Mendeklarasikan object MySqlCommand

    Contoh : MySqlCommand^ nama_object = gcnew

    MySqlCommand("perintah_sql_query",nama_object_MySqlConnection);

    MySqlCommand^ nnn = gcnew MySqlCommand("select id from tbl_user",yyy);

    d. Mendeklarasikan object MySqlDataReader

    Contoh : MySqlDataReader^ nama_object;

    MySqlDataReader^ myReader;

    e. Open Koneksi

    Contoh : konek->Open() ;

    Perintah-perintah DB harus ditampung dalam Error Exception yaitu :

    try

  • {

    //perintah-perintah yg bisa dieksekusi

    yyy->Open(); myReader=nnn->ExecuteReader(); MessageBox::Show("Koneksi Sukses");

    } catch(Exception^ ex)

    {

    //error yg terjadi MessageBox::Show(ex->Message);

    }

    f. Eksekusi perintah

    Contoh : myReader=cmd->ExecuteReader();

    g. Menampung hasil eksekusi

    while(myReader->Read())

    {

    //ambil data di kolom 1 (id) dalam int

    textBox1->Text += (myReader->GetInt32(0).ToString()) ;

    }

    h. Close Koneksi

    yyy->Close();

    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { String^ xxx = L"database=db_toko;datasource=localhost;port=3306;username=root;password=" ; MySqlConnection^ yyy= gcnew MySqlConnection(xxx); MySqlCommand^ nnn = gcnew MySqlCommand("select * from tbl_user",yyy); MySqlDataReader^ myReader; try { yyy->Open();

  • myReader=nnn->ExecuteReader(); MessageBox::Show("Koneksi Sukses"); while(myReader->Read()) { textBox1->Text += (myReader->GetInt32(id).ToString()) ; comboBox1->Items->Add(myReader->GetString(0)); listBox1->Items->Add(myReader->GetInt32(0).ToString()) ; listBox2->Items->Add(myReader->GetString(nama)); listBox3->Items->Add(myReader->GetString(2)); } yyy->Close(); } catch(Exception^ ex) { MessageBox::Show(ex->Message); } }

    2. Manage Database Lewat VC++ 2010

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ xxx = L"database=db_toko;datasource=localhost;port=3306;username=root;password=" ; MySqlConnection^ yyy= gcnew MySqlConnection(xxx); MySqlCommand^ nnn = gcnew MySqlCommand("insert into tbl_sparepart(nama,STOK,harga) values('" + this->textBox1->Text + "','"+ this->textBox2->Text +"','"+ this->textBox3->Text +"') ; ",yyy); MySqlDataReader^ myReader; try {

    yyy->Open(); myReader=nnn->ExecuteReader(); MessageBox::Show("Saved") ; while(myReader->Read()) {

  • } } catch(Exception^ ex) { MessageBox::Show(ex->Message); } } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { String^ xxx = L"database=db_toko;datasource=localhost;port=3306;username=root;password=" ; MySqlConnection^ yyy= gcnew MySqlConnection(xxx); MySqlCommand^ nnn = gcnew MySqlCommand("select * from tbl_sparepart where nama like '%" + textBox4->Text + "%';" ,yyy); MySqlDataReader^ myReader; try { yyy->Open(); myReader=nnn->ExecuteReader(); while(myReader->Read()) { comboBox1->Items->Add(myReader->GetString(1)); } } catch(Exception^ ex) { MessageBox::Show(ex->Message); } }