Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

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

Friday, October 23, 2009

How to Configure Path of Java on Windows

In this post, I assume that all of you have downloaded and installed Java Developement Kit (JDK 2.0 or above) . If java already installed on your computer but you still can’t use the command like ‘javac’ or ‘java’ on your command promt. Maybe you haven’t set the classpath of the java itself.

These are some ways to activate java command on your command prompt :

  1. First of all, make sure you have already installed JDK 2.0 that can be downloaded at www.java.sun.com free.
  2. And then, see at the address where you put the installed files. The default path is at C:\ C:\Program Files\Java\... After that, see where the bin folder exist.
  3. When you’re already active in C:\Program Files\Java\jdk1.6.0_10\bin, copy the address.
  4. And then, double click on My Computer, choose properties
  5. Activate the advanced tab, and click on environment variables.

  1. Find path or classpath or if they are not exist, you can create the new one with its name.


  1. Now, edit on the path or classpath. Type the semicolon (;), and then followed by paste the ‘C:\Program Files\Java\jdk1.6.0_10\bin’.


  1. Click OK, and now you can try the javac command on your command promt. Just try it! Java is so full of fun if you dare to love it.. J