2014-02-04_214525

Java Database Programming


DataBase Connectivity

1. Tentukan Driver

class.forName(“…”);

2. Tentukan Connection

Connection cnt = DriveManager.getConnection(“….”);

3. Tentukan Statement
statement st = cnt.createStatement();
statement st = cnt.createStatement(1004,1008);
1004 -> Result set type
1008 -> Type_scroll_insensitive.

4. Tentukan Resultset
r = st.executeQuery(“Select* from Ms.user”);

Structured Query Language (SQL)

adalah bahasa untuk mendefinisikan tabel dan kendala integritas dan untuk mengakses dan memanipulasi data. SQL (diucapkan “SQL” atau “sekuel”) adalah bahasa universal untuk mengakses sistem database relasional. Program aplikasi dapat memungkinkan pengguna untuk mengakses database tanpa langsung menggunakan SQL, namun aplikasi ini sendiri harus menggunakan SQL untuk mengakses database.

2014-02-04_214525

2014-02-04_214548

The Java API for developing Java database applications is called JDBC.

JDBC is the trademarked name of a Java API that supports Java programs that access relational databases. JDBC is not an acronym, but it is often thought to stand for Java Database Connectivity. JDBC provides Java programmers with a uniform interface for accessing and manipulating a wide range of relational databases.

JDBC provides the DatabaseMetaData interface for obtaining database wide information and the ResultSetMetaData interface for obtaining information on the specific ResultSet, such as column count and column names.

Berikut contoh codingan pada pertemuan ini :

File : Koneksi.java

package pert12kls03pit;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Koneksi {

Connection con;
Statement st;
ResultSet rs;

public Koneksi()
{

try {

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

con = DriverManager.getConnection(“jdbc:odbc:driver=Microsoft Access Driver (*.mdb);DBQ=Database1.mdb”);
st = con.createStatement(1004,1008);
}

catch (SQLException ex) {

Logger.getLogger(Koneksi.class.getName()).log(Level.SEVERE, null, ex);

}

}

catch (ClassNotFoundException ex) {

Logger.getLogger(Koneksi.class.getName()).log(Level.SEVERE, null, ex);

}

}

public ResultSet eksekQuerySelect(String query)
{

ResultSet r = null;
try {

r = st.executeQuery(query);

}

catch (Exception e) {

return r;

}

}

public void eksekManipulasi(String query)
{

try {

st.executeQuery(query);
}

catch (Exception e) {
}
}

}

_______________________________________________________________________________

File : pert12kls03pit.java

package pert12kls03pit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Pert12kls03PIT extends JFrame {

JPanel pnlIsi = new JPanel();
JPanel pnlTombol = new JPanel();

JLabel uname = new JLabel(“username”);
JLabel pass = new JLabel(“password”);
JLabel full = new JLabel(“fullname”);
JTextField tUname = new JTextField();
JTextField tPass = new JTextField();
JTextField tFull = new JTextField();

JButton b1 = new JButton(“<<“);
JButton b2 = new JButton(“<“);
JButton b3 = new JButton(“>”);
JButton b4 = new JButton(“>>”);

Koneksi con = new Koneksi();
ResultSet rs;

public Pert12kls03PIT()
{
setLayout(new BorderLayout());

pnlIsi.setPreferredSize(new Dimension(640,340));
pnlIsi.setBackground(Color.ORANGE);
pnlTombol.setPreferredSize(new Dimension(640,140));
pnlIsi.setLayout(new GridLayout(3,2));
pnlIsi.add(uname);
pnlIsi.add(tUname);
pnlIsi.add(pass);
pnlIsi.add(tPass);
pnlIsi.add(full);
pnlIsi.add(tFull);

pnlTombol.add(b1);
pnlTombol.add(b2);
pnlTombol.add(b3);
pnlTombol.add(b4);

add(pnlIsi, BorderLayout.CENTER);
add(pnlTombol, BorderLayout.SOUTH);

rs = con.eksekQuerySelect(“SELECT * FROM MsUser”);
try {

rs.first();
tUname.setText(rs.getString(1));
tPass.setText(rs.getString(2));
tFull.setText(rs.getString(3));
}

catch (SQLException ex) {

Logger.getLogger(Pert12kls03PIT.class.getName()).log(Level.SEVERE, null, ex);

}

b1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {

rs.first();
tUname.setText(rs.getString(1));
tPass.setText(rs.getString(2));
tFull.setText(rs.getString(3));
}

catch (SQLException ex) {

Logger.getLogger(Pert12kls03PIT.class.getName()).log(Level.SEVERE, null, ex);
}

}

});

b2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {

if(!rs.isFirst())

rs.previous();
tUname.setText(rs.getString(1));
tPass.setText(rs.getString(2));
tFull.setText(rs.getString(3));

}

catch (SQLException ex) {

Logger.getLogger(Pert12kls03PIT.class.getName()).log(Level.SEVERE, null, ex);

}

}

});

b3.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {

if(!rs.isLast())

rs.next();

tUname.setText(rs.getString(1));
tPass.setText(rs.getString(2));
tFull.setText(rs.getString(3));
}

catch (SQLException ex) {

Logger.getLogger(Pert12kls03PIT.class.getName()).log(Level.SEVERE, null, ex);

}
}

});

b4.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {

rs.last();
tUname.setText(rs.getString(1));
tPass.setText(rs.getString(2));
tFull.setText(rs.getString(3));
}

catch (SQLException ex) {

Logger.getLogger(Pert12kls03PIT.class.getName()).log(Level.SEVERE, null, ex);
}

}

});

setSize(640, 480);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);

setVisible(true);
}

public static void main(String[] args) {
new Pert12kls03PIT();
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s