JDBC PreparedStatement
Selain menggunakan Statement untuk mengeksekusi perintah SQL, kita juga dapat menggunakan PreparedStatement. Berikut ini merupakan contoh penggunakan PreparedStatement untuk melakukan perintah SQL ( Insert ).
1. GetConnection.Java
/**
*
* @author Kukuh Utama
*/
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GetConnection {
private final String db_driver = “oracle.jdbc.Driver”;
private final String db_connection = “jdbc:mysql://localhost:3306/db_perpustakaan”;
private final String db_user = “root”;
private final String db_password = “”;
Connection conn;
public GetConnection(){
try{
Class.forName(db_driver);
} catch(ClassNotFoundException ex){
ex.getMessage();
}
}
public Connection getDBConnection(){
try {
conn = DriverManager.getConnection(db_connection, db_user, db_password);
} catch (SQLException ex) {
System.out.println(“Connection Failed! Check output console”);
ex.getMessage();
}
return conn;
}
}
2.JDBCStatement
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
*
* @author Kukuh Utama
*/
public class JDBCStatement {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {
GetConnection setConn = new GetConnection();
Connection conn;
conn = setConn.getDBConnection();
String SQLInsert =”INSERT INTO tbl_buku(id_buku, judul_buku, pengarang, penerbit, jumlah) values (?, ?, ?, ?, ?)”;
PreparedStatement prStatement;
prStatement = conn.prepareStatement(SQLInsert);
prStatement.setInt(1, 100);
prStatement.setString(2, “Sejarah Dunia”);
prStatement.setString(3, “Maria Ano”);
prStatement.setString(4, “Erlangga”);
prStatement.setInt(5,100);
prStatement.executeUpdate();
if( prStatement != null){
prStatement.close();
}
if(conn != null){
conn.close();
}
}
}
Beberapa benefit dengan menggunakan PreparedStatement.
1. PreparedStatement lebih cepat daripada Statement.
2. PreparedStatement lebih dinamis dengan query berparameter
3. PreparedStatement mencegah SQL Injection attacks pada Java
Semoga Bermanfaat.
Referensi : Dari Berbagai Sumber.
Note
” If you don’t know where you are going, any road will get you there.”
Cheshire Cat (Alice in Wonderland)
Membaca File Excell Dengan Java
Beberapa hari yang lalu saya ada task membaca isi file excell dengan Java. Setelah membaca dari beberapa referensi akhirnya bisa juga.
Berikut ini contoh source yang saya buat untuk membaca isi file excell dengan Java.
Nama file : ReadExcell.java
package readexcell;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.read.biff.BiffException;
/**
*
* @author Administrator
*/
public class ReadExcell {
private String inputFilePath;
public void setInputFile(String inputFilePath){
this.inputFilePath = inputFilePath;
}
public void readExcell() throws IOException, BiffException{
int numberSheet = 0;
File fileExcell = new File(inputFilePath);
Workbook workbook = Workbook.getWorkbook(fileExcell);
Sheet sheet;
Cell[] cell;
sheet = workbook.getSheet(numberSheet);
for(int i=1; i <sheet.getRows(); i++){
cell = sheet.getRow(i);
String no = cell[0].getContents();
String namaToko = cell[1].getContents();
String alamatToko = cell[2].getContents();
System.out.println(no+” “+namaToko+” “+alamatToko);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, BiffException {
ReadExcell readEx = new ReadExcell();
readEx.setInputFile(“C:\\Example.xls”);
readEx.readExcell();
}
}
Penjelasan code diatas:
Code tersebut membaca file excell dengan nama Example.xls, kemudian membaca isi pada Sheet1 yang terdiri dari 3 kolom yaitu nomer, Nama Toko , dan Alamat Toko.
Berikut ini contoh output code Java tersebut.
Semoga bermanfaat.
P.S.
Library yang saya gunakan dapat di-download disini





