import java.sql.*
import java.io.*;
class konek_db
{
public static void main (String [] args)
throws Exception
{
String i; int p;
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
Class.forName("org.sqlite.JDBC");
Connection koneksi = DriverManager.getConnection
("jdbc:sqlite:D:/isnu/db/coba.db");
Statement stm = koneksi.createStatement ();
System.out.print ("==========Menu Utama============\n");
System.out.print ("==========1.Lihat Data==========\n");
System.out.print ("==========2.Tambah Data=========\n");
System.out.print ("==========3.Update Data=========\n");
System.out.print ("==========4.Delete Data=========\n");
System.out.print ("Apakah anda ingin menginput lagi [y/t]");
try
{
while ((i=br.readLine()).equalsIgnoreCase("y"))
{
System.out.print ("Masukkan angka yang anda ingin pilih 1/2 :");
p=Integer.parseInt(br.readLine());
switch (p)
{
case 1:ResultSet rs=stm.executeQuery ("select * from mhs");
while (rs.next())
{
System.out.print (rs.getString ("npm"));
System.out.print ("\t" + rs.getString ("Nama"));
System.out.print ("\t" + rs.getString ("Alamat"));
System.out.println ();
}
System.out.print ("Apakah anda ingin menginput lagi [y/t] : ");
break;
case 2:System.out.print ("Masukkan nama anda : ");
String a = br.readLine();
System.out.print ("Masukkan alamat anda : ");
String b = br.readLine ();
System.out.print ("Masukkan NPM anda : ");
String c = br.readLine ();
stm.executeUpdate ("insert into mhs (Nama,Alamat,npm)values ('"+a+"','"+b+"','"+c+"')");
System.out.print ("Apakah anda ingin menginput lagi [y/t]");
break;
default:System.out.print ("Salah Ketik");
case 3:System.out.print ("Masukkan nama baru : ");
a = br.readLine();
System.out.print ("Masukkan alamat baru : ");
b = br.readLine ();
System.out.print ("Masukkan NPM yang akan diubah : ");
c = br.readLine ();
stm.executeUpdate ("update mhs set Nama='"+a+"',Alamat='"+b+"' where NPM='"+c+"';");
System.out.print ("Apakah anda ingin menginput lagi [y/t]");
break;
case 4:System.out.print ("Masukkan NPM yang akan dihapus : ");
c = br.readLine ();
stm.executeUpdate ("delete from mhs where NPM='"+c+"';");
System.out.print ("Apakah anda ingin menginput lagi [y/t]");
break;
}
}
}
catch (SQLException e)
{
System.err.print (e.getMessage ());
}
}
}
No comments:
Post a Comment