Fat’s Albojongi

Just another WordPress.com weblog

tugas database

Posted by fatoni95 pada Februari 1, 2009


st1\:*{behavior:url(#ieooui) }
<!– /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:””; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:”Times New Roman”; mso-fareast-font-family:”Times New Roman”;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} –>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Table Normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:””;
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:”Times New Roman”;
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}

//Listing Program inputdata.java

import java.io.*;

import java.sql.*;

public class database

{

static BufferedReader baca= new BufferedReader(new InputStreamReader(System.in));

static String nim, nama, alamat, kota;

static char hasil;

static String database = “jdbc:odbc:latihan”;

static Connection con;

static Statement stmt;

static ResultSet rs;

static int m;

static String query = “select * from mhs”;

public static void main(String args[]) throws IOException

{

do

{

System.out.println(“\n=====M E N U U T A M A=====”);

System.out.println(“1. Inpu Data”);

System.out.println(“2. Edit Data)”);

System.out.println(“3. Hapus Data”);

System.out.println(“4. Tampil Data”);

System.out.println(“0. Keluar”);

System.out.print(“Pilihan Anda : “);

m=Integer.parseInt(baca.readLine());

if (m==1)

{

input();

}

else if (m==3)

{

delete();

}

else if (m==4)

{

tampil();

}

}

while (m!=0);

}

//fungsi input

public static void input() throws IOException

{

try

{

Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);

}

catch (java.lang.ClassNotFoundException e)

{

System.err.print(“ClassNotFoundException: “);

System.err.println(e.getMessage());

}

try

{

con = DriverManager.getConnection(database,””,””);

stmt = con.createStatement();

System.out.println(“\n:::::::::: Input Data ::::::::::”);

System.out.print(“Masukkan NIM : “);

nim=baca.readLine();

System.out.print(“Masukkan Nama : “);

nama=baca.readLine();

System.out.print(“Masukkan Alamat : “);

alamat=baca.readLine();

System.out.print(“Masukkan Kota : “);

kota=baca.readLine();

System.out.print(“Input Data ? (y/t)”);

hasil=(char)baca.read();

if ((hasil==’y’) || (hasil==’Y’))

{

stmt.executeUpdate(“insert into mhs (nim, nama, alamat, kota) values(“

+ “‘” + nim + “‘,”

+ “‘” +nama+ “‘,”

+ “‘” +alamat + “‘,”

+ “‘” +kota + “‘”

+ “)”);

}

else if ((hasil==’t’) || (hasil==’t’))

{

System.out.print(“Inputan Gagal”);

return;

}

else

{

System.out.print(“Inputan Salah!!! “);

return;

}

}

catch(SQLException ex)

{

System.err.println(“SQLException:”+ex.getMessage());

}

}

// fungsi tampil

public static void tampil()

{

try

{

Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);

}

catch (java.lang.ClassNotFoundException e)

{

System.err.print(“ClassNotFoundException: “);

System.err.println(e.getMessage());

}

try

{

con = DriverManager.getConnection(database,””,””);

stmt = con.createStatement();

System.out.println(“\n!\n!::::::::: ========================= :::::::::”);

rs = stmt.executeQuery(query);

System.out.println(“\nDaftar Mahasiswa”);

while (rs.next())

{

String s1 = rs.getString(“nim”);

String s2 = rs.getString(“nama”);

String s3 = rs.getString(“alamat”);

String s4 = rs.getString(“kota”);

System.out.println(s1+ “ ” +s2+ “ ” +s3+ “ ” +s4);

}

stmt.close();

con.close();

}

catch(SQLException ex)

{

System.err.println(“SQLException:”+ex.getMessage());

}

}

// fungsi delete

public static void delete() throws IOException

{

try

{

Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);

}

catch (java.lang.ClassNotFoundException e)

{

System.err.print(“ClassNotFoundException: “);

System.err.println(e.getMessage());

}

try

{

System.out.print(“nim yang akan dihapus : “);

nim=baca.readLine();

con = DriverManager.getConnection(database,””,””);

stmt = con.createStatement();

stmt.executeUpdate(“delete from mhs where nim=”+nim+””);

// ResultSet rs = stmt.executeQuery(query);

// System.out.println(“Daftar Mahasiswa”);

// while (rs.next())

// {

// String s1 = rs.getString(“nim”);

// String s2 = rs.getString(“nama”);

// String s3 = rs.getString(“alamat”);

// String s4 = rs.getString(“kota”);

// System.out.println(s1+ ” ” +s2+ ” ” +s3+ ” ” +s4);

// }

stmt.close();

con.close();

}

catch(SQLException ex)

{

System.err.println(“SQLException:”+ex.getMessage());

}

}

}

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

 
%d blogger menyukai ini: