Skip to main content

AppQuery: Java Source Code

Source code dari program AppQuery di posting sebelumnya.

//********* (c) 2008 om4gus(tm) ***********//
//********* om4gus.blogspot.com ***********//
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class lihatTabel extends javax.swing.JFrame {
public lihatTabel() throws Exception{
initComponents();
tabel.setModel(model=new DefaultTableModel());
}



private void initComponents() {//GEN-BEGIN:initComponents
buttonGroup1 = new javax.swing.ButtonGroup();
cbTabel = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
tabel = new javax.swing.JTable();
tUser = new javax.swing.JTextField();
tPass = new javax.swing.JPasswordField();
tDb = new javax.swing.JTextField();
bUpdateDb = new javax.swing.JButton();
bUpdateTable = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
rSqlServer = new javax.swing.JRadioButton();
rMySql = new javax.swing.JRadioButton();
rOdbc = new javax.swing.JRadioButton();
rCustom = new javax.swing.JRadioButton();
tKoneksi = new javax.swing.JTextField();
tDriver = new javax.swing.JTextField();
lDriver = new javax.swing.JLabel();
lKoneksi = new javax.swing.JLabel();
lQuery = new javax.swing.JLabel();
tQuery = new javax.swing.JTextField();
lUser = new javax.swing.JLabel();
lDb = new javax.swing.JLabel();
tCopy = new javax.swing.JButton();
lStatus1 = new javax.swing.JLabel();
bRun = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
lStatus = new javax.swing.JTextArea();

getContentPane().setLayout(null);

setTitle("MyTabel om4gus");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

cbTabel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbTabelActionPerformed(evt);
}
});

getContentPane().add(cbTabel);
cbTabel.setBounds(230, 100, 150, 19);

tabel.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
jScrollPane1.setViewportView(tabel);

getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(0, 210, 380, 130);

getContentPane().add(tUser);
tUser.setBounds(70, 70, 130, 20);

getContentPane().add(tPass);
tPass.setBounds(230, 70, 150, 20);

tDb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tDbActionPerformed(evt);
}
});

getContentPane().add(tDb);
tDb.setBounds(70, 100, 130, 20);

bUpdateDb.setText("update DB");
bUpdateDb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bUpdateDbActionPerformed(evt);
}
});

getContentPane().add(bUpdateDb);
bUpdateDb.setBounds(70, 120, 130, 23);

bUpdateTable.setText("update Table");
bUpdateTable.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bUpdateTableActionPerformed(evt);
}
});

getContentPane().add(bUpdateTable);
bUpdateTable.setBounds(230, 120, 150, 23);

jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
jLabel6.setText("Driver Type");
jPanel1.add(jLabel6);

rSqlServer.setText("SQL Server");
buttonGroup1.add(rSqlServer);
rSqlServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rSqlServerActionPerformed(evt);
}
});

jPanel1.add(rSqlServer);

rMySql.setText("MySQL");
buttonGroup1.add(rMySql);
rMySql.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rMySqlActionPerformed(evt);
}
});

jPanel1.add(rMySql);

rOdbc.setText("ODBC");
buttonGroup1.add(rOdbc);
rOdbc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rOdbcActionPerformed(evt);
}
});

jPanel1.add(rOdbc);

rCustom.setText("Custom");
buttonGroup1.add(rCustom);
rCustom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rCustomActionPerformed(evt);
}
});

jPanel1.add(rCustom);

getContentPane().add(jPanel1);
jPanel1.setBounds(0, 10, 400, 30);

getContentPane().add(tKoneksi);
tKoneksi.setBounds(70, 150, 310, 20);

getContentPane().add(tDriver);
tDriver.setBounds(70, 40, 310, 20);

lDriver.setFont(new java.awt.Font("Arial", 1, 12));
lDriver.setText("Driver");
getContentPane().add(lDriver);
lDriver.setBounds(4, 40, 50, 15);

lKoneksi.setFont(new java.awt.Font("Arial", 1, 12));
lKoneksi.setText("Connect.");
getContentPane().add(lKoneksi);
lKoneksi.setBounds(0, 150, 70, 15);

lQuery.setFont(new java.awt.Font("Arial", 1, 12));
lQuery.setText("Query");
getContentPane().add(lQuery);
lQuery.setBounds(0, 180, 50, 15);

getContentPane().add(tQuery);
tQuery.setBounds(70, 180, 200, 20);

lUser.setFont(new java.awt.Font("Arial", 1, 12));
lUser.setText("User");
getContentPane().add(lUser);
lUser.setBounds(4, 70, 50, 20);

lDb.setFont(new java.awt.Font("Arial", 1, 12));
lDb.setText("DB/Table");
getContentPane().add(lDb);
lDb.setBounds(4, 100, 70, 20);

tCopy.setText("Copy selected data to clipboard");
tCopy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tCopyActionPerformed(evt);
}
});

getContentPane().add(tCopy);
tCopy.setBounds(1, 340, 380, 23);

lStatus1.setFont(new java.awt.Font("Arial", 1, 12));
lStatus1.setText("Status: ");
getContentPane().add(lStatus1);
lStatus1.setBounds(0, 370, 50, 20);

bRun.setText("Run Query");
bRun.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bRunActionPerformed(evt);
}
});

getContentPane().add(bRun);
bRun.setBounds(270, 180, 110, 23);

lStatus.setEditable(false);
jScrollPane2.setViewportView(lStatus);

getContentPane().add(jScrollPane2);
jScrollPane2.setBounds(52, 372, 330, 40);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-403)/2, (screenSize.height-447)/2, 403, 447);
}//GEN-END:initComponents

private void tDbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tDbActionPerformed
bUpdateDbActionPerformed(evt);
}//GEN-LAST:event_tDbActionPerformed

private void tCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tCopyActionPerformed
Clipboard c = this.getToolkit( ).getSystemClipboard( );
c.setContents(new copyData((String)tabel.getValueAt(tabel.getSelectedRow(),tabel.getSelectedColumn())),
new ClipboardOwner( ) { public void lostOwnership(Clipboard c,Transferable t){}
});

}//GEN-LAST:event_tCopyActionPerformed

private void bRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRunActionPerformed
updateTabel();
}//GEN-LAST:event_bRunActionPerformed

private void cbTabelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTabelActionPerformed
tQuery.setText("SELECT * FROM "+cbTabel.getSelectedItem());
updateTabel();
}//GEN-LAST:event_cbTabelActionPerformed

private void rCustomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rCustomActionPerformed
setType();
}//GEN-LAST:event_rCustomActionPerformed

private void rOdbcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rOdbcActionPerformed
setType();
}//GEN-LAST:event_rOdbcActionPerformed

private void rMySqlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rMySqlActionPerformed
setType();
}//GEN-LAST:event_rMySqlActionPerformed

private void rSqlServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rSqlServerActionPerformed
setType();
}//GEN-LAST:event_rSqlServerActionPerformed

private void bUpdateTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateTableActionPerformed
tQuery.setText("SELECT * FROM "+cbTabel.getSelectedItem());
updateTabel();
}//GEN-LAST:event_bUpdateTableActionPerformed

private void bUpdateDbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateDbActionPerformed
setType();
}//GEN-LAST:event_bUpdateDbActionPerformed

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm

/**
* @param args the command line arguments
*/
public static void main(String args[]) throws Exception{
new lihatTabel().show();
}
private void updateDb(){
// if(tUser.getText().length()>0){
try{
testDriver();
ResultSet rsTabel=con.getMetaData().getTables(null, null, null, null);
cbTabel.setModel(new javax.swing.DefaultComboBoxModel());
for(;rsTabel.next();)
cbTabel.addItem(rsTabel.getString("TABLE_NAME"));
cbTabel.setSelectedIndex(-1);
lStatus.setText(lStatus.getText()+"\nChange Database success");
}catch(Exception e){
System.err.println(e.toString());
lStatus.setText(lStatus.getText()+"\n"+e.toString());
}
// }
}
private void updateTabel(){
tabel.setModel(model=new DefaultTableModel());
tabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// if(cbTabel.getSelectedIndex()>0){
try{
perintah=tQuery.getText();
ResultSet hasil=stmt.executeQuery(perintah);
ResultSetMetaData rsKolom=hasil.getMetaData();
for(int I=1;I<=rsKolom.getColumnCount();I++)
model.addColumn(rsKolom.getColumnName(I).trim());
Vector V=null;
for(int I=0;hasil.next();I++){
V=new Vector();
for(int J=1;J<=hasil.getMetaData().getColumnCount();J++){
V.add(hasil.getObject(J));
}
model.addRow(V);
}
lStatus.setText(lStatus.getText()+"\nUpdate Table success");
}catch(Exception e){
System.err.println(e.getMessage()+"2");
lStatus.setText(lStatus.getText()+"\n"+e.toString());
}
//}
}
public void testDriver(){
//String dbDrv = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//String dbUrl = "jdbc:microsoft:sqlserver://localhost;DatabaseName="+db;
try{
Class.forName(tDriver.getText());
con = DriverManager.getConnection(tKoneksi.getText(),tUser.getText(),tPass.getText());
stmt=con.createStatement();
lStatus.setText("Driver Connected");
}catch(Exception ce){
System.out.println(ce.toString()+"1");
lStatus.setText(ce.toString());//lStatus.getText()+"\n"+
cbTabel.setModel(new DefaultComboBoxModel());
}
}
private void setType(){
lDb.setText("DB/Table");
tDriver.setEnabled(false);
tKoneksi.setEnabled(false);
tPass.setEnabled(true);
if(rSqlServer.isSelected()){
tDriver.setText(this.sqlServerDriver);
tKoneksi.setText(this.sqlServerUrl+"//localhost;DatabaseName="+tDb.getText());
}else if(rMySql.isSelected()){
tDriver.setText(this.mySqlDriver);
tKoneksi.setText(this.mySqlUrl+"//localhost/"+tDb.getText());
}else if(rOdbc.isSelected()){
tDriver.setText(this.odbcDriver);
tKoneksi.setText(this.odbcUrl+""+tDb.getText());
tPass.setText("");
lDb.setText("DS Name");
}else{
tDriver.setEnabled(true);
tKoneksi.setEnabled(true);
}
updateDb();
}
Connection con;
Statement stmt;
String perintah;
DefaultTableModel model;
private static final String sqlServerDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String sqlServerUrl="jdbc:microsoft:sqlserver:"; //jdbc:microsoft:sqlserver://localhost;DatabaseName="+db;
private static final String mySqlDriver="com.mysql.jdbc.Driver";
private static final String mySqlUrl="jdbc:mysql:";//jdbc:mysql://localhost/dbme
private static final String odbcDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private static final String odbcUrl="jdbc:odbc:"; //jdbc:odbc:sqlKuliah
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bRun;
private javax.swing.JButton bUpdateDb;
private javax.swing.JButton bUpdateTable;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JComboBox cbTabel;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JLabel lDb;
private javax.swing.JLabel lDriver;
private javax.swing.JLabel lKoneksi;
private javax.swing.JLabel lQuery;
private javax.swing.JTextArea lStatus;
private javax.swing.JLabel lStatus1;
private javax.swing.JLabel lUser;
private javax.swing.JRadioButton rCustom;
private javax.swing.JRadioButton rMySql;
private javax.swing.JRadioButton rOdbc;
private javax.swing.JRadioButton rSqlServer;
private javax.swing.JButton tCopy;
private javax.swing.JTextField tDb;
private javax.swing.JTextField tDriver;
private javax.swing.JTextField tKoneksi;
private javax.swing.JPasswordField tPass;
private javax.swing.JTextField tQuery;
private javax.swing.JTextField tUser;
private javax.swing.JTable tabel;
// End of variables declaration//GEN-END:variables
class copyData implements Transferable {
DataFlavor FLAVOR= new DataFlavor("application/x-java-serialized-object; class=java.lang.String","String");
DataFlavor[ ] FLAVORS = new DataFlavor[ ] { FLAVOR };
String str;
public copyData(String str) {
this.str = str;
}
public DataFlavor[ ] getTransferDataFlavors( ) { return FLAVORS; }
public boolean isDataFlavorSupported(DataFlavor f){return f.equals(FLAVOR);}
public Object getTransferData(DataFlavor f)
throws UnsupportedFlavorException {
if (!f.equals(FLAVOR)) throw new UnsupportedFlavorException(f);
return str;
}
}
}

Comments

Post a Comment

Popular posts from this blog

KISS: Complete Ubuntu Server Configuration

The Simplest way to install and configure our Ubuntu Server Edition. With this step-by-step manual instalation, we can configure Network, Internet, APT instalation source, XAMPP for Linux, Apache Web server, ProFtpd Ftp Server, PHP, MySql Database Server, Samba file sharing, Squid proxy server. A. Network And Internet Configuration 1. Intall Ubuntu Server Edition, I prefer using 8.04 LTS, a stable build with long time support, even better than the newer ones, in my opinion. Login as root or use this command to become root for user in sudoers list. # sudo su 2. Now, Change the hostname # pico /etc/hosts 127.0.0.1 localhost //Don't change this 192.168.1.12 hercules //Change with your ip address and hostname ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts 3. Make configuration for network interfaces # pico /etc/network/interfaces # This file describes the network interfaces available on your s...

Java: Pengiriman Parameter antar Frame

Sebenarnya konsep ini berlaku bukan hanya pada frame, tapi bisa dilakukan antar object dari class yang sifatnya umum, karena Java yang sifatnya sudah full OOP. Tapi, karena lebih sering diterapkan dalam pemrograman GUI desktop, khususnya dengan Netbeans, maka dicontohkan dengan class javax.swing.JFrame. Setiap bentuk code java harus dibentuk dalam bentuk class, biar sebagaimanapun sederhananya. Tidak seperti pemrograman lainnya yang menggunakan konsep OOP sebagai tambahan dan pengembangan dari yang sudah ada sebelumnya. Itulah sebabnya kita mengenal class di C++, Pascal, PHP, tapi masih diperbolehkan untuk tetap menggunakan konsep primitif. Sedangkan pada kasus java dan .NET, OOP merupakan inti utamanya yang harus selalu diikuti dan diterapkan. Awalnya akan sangat membingungkan, tapi sangat mudah untuk langkah pengembangan selanjutnya. Sudah lihat JavaDoc? itu adalah dokumen pemrograman yang paling aneh didunia, pada pandangan pertama. Coba bandingkan dengan MSDN-nya .NET, PHP Manual, ...

Java-MySql: Aplikasi Database dengan Data Gambar (Updated)

Untuk update metode simpan gambar lihat di appGambar 2.0 Bagaimana caranya menyimpan data yang membutuhkan penyimpanan data gambar? Misalkan data pegawai yang membutuhkan penyimpanan gambar dari setiap pegawai. Alternatifnya ada dua, dengan kelebihan dan kekurangan masing-masing. Cara yang pertama adalah dengan membuat field dengan tipe data binary, kemudian menyimpan gambar yang dibutuhkan dalam field di database tersebut. Cara ini membutuhkan manajemen program yang lebih rumit, tapi data gambar akan terjamin, karena tersimpan didalam database yang terintegrasi dengan data lainnya. Kekurangan lainnya adalah ukuran database yang akan membesar dan lambat. Bayangkan saja, sebuah field membutuhkan gambar dengan format jpeg, 200kB. Apabila ada 200 juta record, maka ukuran field yang diperuntukan khusus untuk gambar dengan perhitungan kasar adalah 0.2 MB x 200M = 40 x 10^12 = 40 TB. Belum ditambah dengan ukuran data dari field lainnya, kalau misalkan untuk menyimpan data penduduk Indonesia....