Thursday, January 28, 2010

Tutorial JDBC MySql (1)

In the programming, it does not complete if it does not discuss
database problems, which would make the program more
dynamic. This time, I will cite a brief skript
about how to create a database connection (JDBC) in the
java programming. DBMS used in this example is
MySql database. In my opinion, this is a MySQL connection
most easily done because they do not require configuration
complicated.

MySql default port is 3306.
Each DBMS has a port that each is different.
In PostgreSQL, the default port is 5432,
the SQLServer2000, the default port is 1433.
And this is the code :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class MemberDAOImpl implements MemberDAO {
private Connection connection;

public MemberDAOImpl() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/bank";
connection = DriverManager.getConnection(url, "root",

"");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}


In order to make the connection can be called many times in other classes, add this script


public Connection getConnection(){
return connection;
}



For SQL scripts example, suppose a table is a 'member'.
This script can be made if the class for the entity member is made
first. If you need a file, you can contact me, I will send via email. And it so much easier to detect errors if we use the NetBeans application.

public List getAllMember() {
List listMember = new ArrayList
();
Connection conn;
Statement sttmt;
try {
conn = getConnection();
sttmt = conn.createStatement();
String query = "select * from member";
ResultSet rsMember = sttmt.executeQuery(query);
while(rsMember.next()){
Member member = new Member();
member.setRegNumber(rsMember.getString(1));
member.setId(rsMember.getString(2));
member.setName(rsMember.getString(3));
member.setGender(rsMember.getInt(4));
member.setClas(rsMember.getString(5));
member.setBirthPlaceDate(rsMember.getString(6));
member.setAddress(rsMember.getString(7));
member.setPhoneNumber(rsMember.getString(8));
listMember.add(member);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
return listMember;
}


That's the script to connect to MySql, for other DBMS I will discuss in the next examples. Good luck & Happy coding ^ _ ^!

By: Mr.Stanza

Wednesday, January 27, 2010

Qwerty Algorithm Tutorial

Java programming science is very broad and can be implemented in various fields. Among them are cryptographic. Cryptography itself is one branch of computer science in the world, which is associated with encoding or disguise the original text (plain text) into text that is disguised so as not to be understood by readers (chiper text).

Here is one example of the science of cryptographic algorithms called Qwerty (originaled by: Dwindy Stanza & Paiman). As the name implies, QWERTY, this algorithm adapted to the standard keyboard board. The principle works something like this:

A : index 0 = Q : index 0
B : index 1 = W : index 1
C : index 2 = E : index 2
D : index 3 = R : index 3
...
Z : index 25 = M : index 25

That's for the first letter, while for the second letter there is a shift in the index, so that A is no longer the Q, but the W. And Q was changed to the last index that W to M shifted one index. And so on.

A : index 0 = W : index 0
B : index 1 = E : index 1
C : index 2 = R : index 2
D : index 3 = T : index 3
...
Z : index 25 = Q : index 25

Prior to implementation of the program code, first create the components needed, ie
1. JPanel txtPlain
2. JPanel txtChiper
3. JButton btnEncrypt
4. JButton btnDecrypt

Encrypt used to translate the text into chiper Plain text, while Decrypt chiper translate text into plain text.

For implementation in java, something like this for action btnEncrypt:

private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {
char [] kode = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M' };
char [] abc = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };

int ketemu=0;
char tampung = 0;
String plaintext = txtPlain.getText();
char[] ciphertext = new char[plaintext.length()];

if (!plaintext.equals(""))
txtPlain.setText(plaintext);

for(int w = 0; w
tampung = plaintext.toUpperCase().charAt(w) ;
for(int x = 0 ; x<=25 ; x++){
if(tampung == abc[x] ){
ketemu = x;
}
// else{ ketemu = -1; }
ciphertext[w] = kode[ketemu];
}

char bantu = 0;

bantu = kode[0];
// Pergeseran Index
for(int i = 0 ; i<=24 ; i++ ) {
kode[i] = kode[i+1];
}
kode[25] = bantu;
}
String c;
for(int w = 0; w txtCipher.setText(String.valueOf(ciphertext));
}

}


As for the implementation of the action button Decrypt are as follows:

private void btnDecryptActionPerformed(java.awt.event.ActionEvent evt) {
char [] kode = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M' };
char [] abc = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };

int ketemu=0;
char tampung = 0;
String plaintext = txtPlain.getText();
char[] ciphertext = new char[plaintext.length()];

if (!plaintext.equals(""))
txtPlain.setText(plaintext);

for(int w = 0; w
tampung = plaintext.toUpperCase().charAt(w) ;
for(int x = 0 ; x<=25 ; x++){
if(tampung == kode[x] ){
ketemu = x;
}
// else{ ketemu = -1; }
ciphertext[w] = abc[ketemu];
}

char bantu = 0;

bantu = abc[25];
// Pergeseran Index
for(int i = 25 ; i>=1 ; i-- ) {
abc[i] = abc[i-1];
}
abc[0] = bantu;
}
String c;
for(int w = 0; w txtCipher.setText(String.valueOf(ciphertext));
}

}

For the result, here is an example :
plain text = BEDA
chiper text = WYYR

keep trying and happy coding !

By : Mr.Stanza