Monday, November 10, 2014

Pemograman With Database

7:41 PM Posted by beckti'velu No comments
package database_menu;

import java.sql.DriverManager;
import java.util.Scanner;
import java.sql.*;

public class Database_menu {

    public static void main(String[] args) {
    try{
        int pilihan = 0;
        Scanner input = new Scanner(System.in);
        Class.forName ("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/pd2", "root", "");
        Statement stmt = conn.createStatement();
        while (pilihan!=5) {
        System.out.println("1. View (Select");
        System.out.println("2. Insert");
        System.out.println("3. Edit (Update");
        System.out.println("4. Delete");
        System.out.println("5. Exit");
        System.out.print("Pilihan : ");
        pilihan = input.nextInt();
       
        if (pilihan==1){
            ResultSet rs = stmt.executeQuery("SELECT * FROM lagu");
            while (rs.next()){
                System.out.println(rs.getString("id")+" - "+rs.getString("judul")+" - "+rs.getString("penyanyi"));
            }
        }else if (pilihan==2) {
            System.out.print("Judul : ");
            String judul = new Scanner(System.in).nextLine();
            System.out.print("Penyanyi : ");
            String penyanyi = new Scanner(System.in).nextLine();
            String sql = "INSERT INTO lagu (judul,penyanyi)"+ "VALUES('"+judul+"','"+penyanyi+"')";
            stmt.executeUpdate(sql);
           
        }else if (pilihan==3){
            System.out.print("Id yang akan di ubah : ");
            String id = new Scanner(System.in).nextLine();
            ResultSet rs = stmt.executeQuery("SELECT * FROM lagu " + "WHERE id"+id);
            rs.first();
            String judul_lama = rs.getString("judul");
            String penyanyi_lama = rs.getString("penyanyi");
            System.out.print("Judul : ");
            String judul = new Scanner(System.in).nextLine();
            System.out.print("Penyanyi : ");
            String penyanyi = new Scanner(System.in).nextLine();
            String sql = "UPDATE lagu SET judul='"+judul+"'," + "penyanyi= '"+penyanyi+"' WHERE id='"+id;
            stmt.executeUpdate(sql);
        }
    }
   
}catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
    }
    }
   
}

Link database : http://www.4shared.com/document/HVCqfzbece/lagu.html

0 comments:

Silahkan Tinggalkan Komentar Anda