22 Desember 2011
JDBC_2
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;
public class Pengguna {
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
String userid = "hr";
String password = "02091992";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
public Pengguna() {
}
public void getDBConnection() throws SQLException {
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
}
public ResultSet getAllEmployees() throws SQLException {
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "crate table pengguna (id number(4), nama varchar2(20))";
System.out.println("\nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}
public static void main(String[] args) throws SQLException {
Pengguna test = new Pengguna();
test.getDBConnection();
test.conn.close();
}
}
JDBC_1
package PBOproject;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class JDBCSekre {
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
String userid = "hr";
String password = "hr";
Connection conn;
Statement stmt;
ResultSet rset;
String query;
String sqlString;
public JDBCSekre(){
}
public void getDBConnection() throws SQLException {
try{OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn = ds.getConnection(userid, password);
System.out.println("Koneksi berhasil");
}catch (SQLException ex) {
System.out.println("Masih belum konek");
// OracleDataSource ds;
// ds = new OracleDataSource();
// ds.setURL(jdbcUrl);
// conn = ds.getConnection(userid, password);
}
}
public ResultSet getAllSekretariat() throws SQLException {
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "Select * FROM Sekretariat";
System.out.println("\nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}
public static void main(String[] args) throws SQLException {
JDBCSekre ld = new JDBCSekre();
ResultSet rset = ld.getAllSekretariat();
while (rset.next()) {
System.out.println(rset.getInt(1) + " "
+ rset.getString(2) + " "
+ rset.getString(3) + " "
+ rset.getString(4) + " "
+ rset.getString(5) + " "
+ rset.getString(6) + " "
+ rset.getString(7) + " "
+ rset.getString(8));
}
ld.conn.close();
}
}
JFileChooser
public void save() throws FileNotFoundException {
JFileChooser jc = new JFileChooser();
int op = jc.showSaveDialog(jc);
if (op == JFileChooser.APPROVE_OPTION) {
String file = jc.getSelectedFile().getPath();
File outFile = new File(file);
FileOutputStream outFileStream = new FileOutputStream(outFile, true);
PrintWriter outStream = new PrintWriter(outFileStream);
String surat = null;
surat = this.getNoSurat() + ":" + this.getPerihal() + ":"
+ this.getTglSurat() + "/"
+ this.getTglTerima() + "/"
+ this.getPengirim() + ":"
+ this.getPenerima();
outStream.println(surat);
outStream.close();
JOptionPane.showMessageDialog(null, "Tersimpan !!");
} else {
JOptionPane.showMessageDialog(null, "Surat Masuk Tidak Tersimpan");
}
}
----------------------------------------------------------------
save1 = new JButton("SAVE AS");
save1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Sekre s = new Sekre();
s.setNoSurat(nosurat.getText());
s.setPerihal(perihal.getText());
s.setTglSurat(tglsurat.getText());
s.setTglTerima(tglterima.getText());
s.setPengirim(pengirim.getText());
s.setPenerima(penerima.getText());
try {
s.save();
} catch (IOException ex) {
Logger.getLogger(Sekre.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
21 Desember 2011
String_1
package string;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Dokumen {
File file;
String teks = "";
public Dokumen(File file) {
this.setFile(file);
}
public Dokumen(String fileName) {
File tempFile = new File(fileName);
setFile(tempFile);
}
Dokumen() {
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getTeks() {
return teks;
}
public void setTeks(String teks) {
this.teks = teks;
}
public void baca() throws FileNotFoundException, IOException {
FileInputStream stream = new FileInputStream(file);
int result = 0;
while ((result = stream.read()) != -1) {
teks = teks + (char) result;
}
}
void simpan() throws FileNotFoundException, IOException {
FileOutputStream stream = new FileOutputStream(file);
stream.write(teks.getBytes());
stream.close();
}
}
String_2
package string;
import java.util.Date;
import java.util.StringTokenizer;
public class Koleksi {
String koleksiId;
String judul;
private Date tanggalPinjam;
public Date getTanggalPinjam() {
return tanggalPinjam;
}
public void setTanggalPinjam(Date tanggalPinjam) {
this.tanggalPinjam = tanggalPinjam;
}
public void setTanggalPinjam(String tanggalPinjam) {
this.tanggalPinjam = new Date();
StringTokenizer test = new StringTokenizer(tanggalPinjam, "-");
String tglString = test.nextToken();
this.tanggalPinjam.setDate(Integer.valueOf(tglString));
String blnString = test.nextToken();
this.tanggalPinjam.setMonth(Integer.valueOf(blnString)-1);
String thnString = test.nextToken();
this.tanggalPinjam.setYear(Integer.valueOf(thnString)-1900);
}
}
String_3
package string;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class ListPengguna {
private ArrayList
private Dokumen dokumen;
public void baca() throws FileNotFoundException, IOException {
dokumen.baca();
}
public ArrayList
return pengguna;
}
public void setPengguna(ArrayList
this.pengguna = pengguna;
}
public Dokumen getDokumen() {
return dokumen;
}
public void setDokumen(Dokumen dokumen) {
this.dokumen = dokumen;
}
}
String_4
package string;
public class Pengguna {
public String nama;
public String nomor;
}
-----------------------------------------------
package string;
public class Mahasiswa {
String nama;
String nomor;
}
String_5
package string;
public class TestKoleksiTanggalPinjam {
public static void main(String[] args) {
Koleksi test = new Koleksi();
test.judul = "Salah Asuhan";
test.setTanggalPinjam("14-11-2011");
System.out.println("Tanggal Pinjam = " + test.getTanggalPinjam());
}
}
String_6
package string;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
public class TestListPengguna {
public static void main(String[] args) throws FileNotFoundException, IOException {
//operasi baca pada list pengguna
Dokumen test1 = new Dokumen();
test1.setFile(new File("test.txt"));
test1.baca();
String teks = test1.getTeks();
//buat list pengguna
StringTokenizer test = new StringTokenizer(teks, "[\n\r;]");
int jumlahPengguna = Integer.valueOf(test.nextToken());
//buat list pengguna
ListPengguna test2 = new ListPengguna();
//
System.out.println("Jumlah Pengguna = " + jumlahPengguna);
//buat pengguna
for (int i = 0; i < jumlahPengguna; i++) {
Pengguna temp = new Pengguna();
temp.nomor = test.nextToken();
temp.nama = test.nextToken();
test2.getPengguna().add(temp);
//baca jumlah pengguna
int jumlahPinjaman = Integer.valueOf(test.nextToken());
for (int j = 0; j < jumlahPinjaman; j++) {
Koleksi temp1 = new Koleksi();
temp1.koleksiId = test.nextToken();
temp1.judul = test.nextToken();
temp1.setTanggalPinjam(test.nextToken());
test2.getPengguna().add(temp);
}
}
}
}
String_7
package string;
public class TestListPengguna1 {
public static void main(String[] args) {
ListPengguna test = new ListPengguna();
test.setDokumen(new Dokumen());
}
}
String_8
package string;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
public class TestStringTokenizer {
public static void main(String[] args) throws FileNotFoundException, IOException {
Dokumen test1 = new Dokumen();
test1.setFile(new File("test.txt"));
test1.baca();
String teks = test1.getTeks();
StringTokenizer test = new StringTokenizer(teks, "[\n\r;]");
while(test.hasMoreTokens()){
System.out.println(test.nextToken());
}
}
}
Stream_8
package stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
public class TestStringTokenizer {
public static void main(String[] args) throws FileNotFoundException, IOException {
Dokumen test1 = new Dokumen();
test1.setFile(new File("test.txt"));
test1.baca();
String teks = test1.getTeks();
StringTokenizer test = new StringTokenizer(teks, "[\n\r;]");
while(test.hasMoreTokens()){
System.out.println(test.nextToken());
}
}
}
Stream_7
package stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadDataDemo {
public ReadDataDemo() {
}
public static void main(String[] args) {
// write the data out with FileOutputStream
// buat file
File inputFile = new File("latihan.txt");
try{
// buka stream
FileInputStream in = new FileInputStream(inputFile);
// buat buffer pembacaan data
String testInput = new String();
// variabel integer untuk membaca data dari file yang dibuka
int c;
// cek akhir dari file apakah bernilai -1
while((c=in.read())!=-1){
// tambahkan data pembacaan ke buffer
testInput += (char)c;
}
// tutup stream
in.close();
System.out.println(testInput);
} catch (IOException ex){
System.out.println(ex.getMessage());
}
}
}
Stream_6
package stream;
import java.awt.Frame;
import javax.swing.JFileChooser;
public class FileChoiser {
public static void main(String[] args) {
Frame frame = new Frame();
JFileChooser choos = new JFileChooser();
frame.add(choos);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Stream_9
package stream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteDataDemo {
public WriteDataDemo() {
}
public static void main(String[] args) throws IOException {
// write the data out with FileOutputStream
// buat file
File outputFile = new File("latihan.txt");
// buka stream
FileOutputStream out = new FileOutputStream(outputFile);
System.out.println(outputFile.isFile());
// sediakan data string
String testOutput ="This is an exercise to write data to a file with FileOutputStream," ;
System.out.println(outputFile.getAbsoluteFile());
/* Cara 1 : tulis data ke stream
for(int i=0;i
out.write((byte)testOutput.charAt(i));
}
*/
/*Cara 2 : tulis data ke stream*/
out.write(testOutput.getBytes());
//untuk melihat
// tutup stream
out.close();
}
}
Stream_5
package stream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DokumenTestSimpan {
public static void main(String[] args) throws FileNotFoundException, IOException {
Dokumen tes = new Dokumen("Dokumen.txt");
System.out.println(tes.file.getAbsoluteFile());
tes.setTeks("You can do it");
tes.simpan();
}
}
Stream_4
package stream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DokumenTestBaca {
public static void main(String[] args) throws FileNotFoundException, IOException {
Dokumen tes1 = new Dokumen("Dokumen.txt");
tes1.baca();
System.out.println(tes1.getTeks());
}
}
Stream_3
package stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Dokumen {
File file;
String teks = "";
public Dokumen(File file) {
this.setFile(file);
}
public Dokumen(String fileName) {
File tempFile = new File(fileName);
setFile(tempFile);
}
Dokumen() {
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getTeks() {
return teks;
}
public void setTeks(String teks) {
this.teks = teks;
}
public void baca() throws FileNotFoundException, IOException {
FileInputStream stream = new FileInputStream(file);
int result = 0;
while ((result = stream.read()) != -1) {
teks = teks + (char) result;
}
}
void simpan() throws FileNotFoundException, IOException {
FileOutputStream stream = new FileOutputStream(file);
stream.write(teks.getBytes());
stream.close();
}
}
Stream_2
package stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BacaRead {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream in = null;
File file = new File("Latihan.txt");
in = new FileInputStream(file);
in.close();
}
}
Stream_1
package stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Baca {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileOutputStream out = null;
File file = new File("Latihan.txt");
out = new FileOutputStream(file);
String nim = "105314009";
String nama = "sadhega";
String idKoleksi = "001";
String judul = "Laskar Pelangi";
String txt = idKoleksi + "\t" + judul + "\n";
out.write(txt.getBytes());
out.close();
}
}
MainStack
public class MainStack {
public static void main(String[] args) {
Stack test = new Stack();
test.push(40);
test.push(20);
test.push(30);
System.out.println("Stack :" + test.tumpukan.toString());
System.out.println("Ukuran :" + test.size());
System.out.println("Setelah di POP :");
test.pop();
System.out.println("Stack :" + test.tumpukan.toString());
System.out.println("Ukuran :" + test.size());
}
}
Stack
import LinkedLstHead.LinkedList;
import LinkedLstHead.ListNode;
public class Stack {
LinkedList tumpukan;
public Stack() {
tumpukan = new LinkedList();
}
public void push(int data) {
tumpukan.addFirst(data);
}
public int pop() {
return tumpukan.removeFirst();
}
public int size() {
return tumpukan.getSize();
}
public boolean isEmpty() {
if (tumpukan.isEmpty()) {
return true;
} else {
return false;
}
}
}
ListNode
package LinkedListObject;
public class ListNode {
Object element;
ListNode next;
ListNode prev;
public ListNode() {
}
public ListNode(Object element) {
this.element = element;
}
public Object getElement() {
return element;
}
public void setElement(Object element) {
this.element = element;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
public ListNode getPrev() {
return prev;
}
public void setPrev(ListNode prev) {
this.prev = prev;
}
}
public class ListNode {
Object element;
ListNode next;
ListNode prev;
public ListNode() {
}
public ListNode(Object element) {
this.element = element;
}
public Object getElement() {
return element;
}
public void setElement(Object element) {
this.element = element;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
public ListNode getPrev() {
return prev;
}
public void setPrev(ListNode prev) {
this.prev = prev;
}
}
LinkedList
public class LinkedList {
int size;
ListNode head;
public LinkedList() {
head = new ListNode();
head.next = head;
head.prev = head;
size = 0;
}
private ListNode addBefore(int x, ListNode bantu) {
ListNode baru = new ListNode(x);
baru.next = bantu;
baru.prev = bantu.prev;
bantu.prev.next = baru;
bantu.prev = baru;
size++;
return baru;
}
private int remove(ListNode bantu) {
if (isEmpty()) {
System.out.println("Senarai Kosong");
} else {
bantu.next.setPrev(bantu.getPrev());
bantu.prev.setNext(bantu.getNext());
bantu.setNext(null);
bantu.setPrev(null);
}
size--;
return bantu.getElement();
}
public void addFirst(int x) {
addBefore(x, getHead().getNext());
}
public void addLast(int x) {
addBefore(x, getHead());
}
public int removeFirst() {
return remove(getHead().getNext());
}
public int removeLast() {
return remove(getHead().getPrev());
}
public int remove(int x) {
ListNode bantu = head;
if (isEmpty()) {
System.out.println("Senarai kosong");
} else {
while (bantu.next != head) {
bantu = bantu.next;
if (bantu.element == x) {
remove(bantu);
break;
}
}
}
return bantu.element;
}
public void addSort(int x) {
if (isEmpty()) {
addBefore(x, head.next);
} else {
ListNode bantu = head.next;
while (bantu != head && x > bantu.element) {
bantu = bantu.next;
}
addBefore(x, bantu);
}
}
public String toString() {
String string = new String();
ListNode bantu = head;
while (bantu.next != head) {
bantu = bantu.next;
string += bantu.element + " ";
}
if (isEmpty()) {
return "Senarai Kosong";
} else {
return string;
}
}
public void display() {
ListNode bantu = head;
while (bantu.next != head) {
bantu = bantu.next;
System.out.println(bantu.element);
}
}
public ListNode search(int x) {
ListNode bantu = head;
while (bantu.next != head) {
bantu = bantu.next;
if (bantu.element == x) {
return bantu;
}
}
System.out.println("Tidak ditemukan");
return head;
}
public boolean isEmpty() {
if (head.next == head) {
return true;
} else {
return false;
}
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public ListNode getHead() {
return head;
}
public void setHead(ListNode head) {
this.head = head;
}
}
Langganan:
Postingan (Atom)