Archive

Archive for February, 2013

Cute Little Owl

February 24, 2013 Leave a comment
Little Owl

Little Owl

 

Source

Categories: Ren's Notes Tags: ,

Menyimpan Hasil Query Select Pada List ( Pemrograman Java )

February 23, 2013 Leave a comment

Beberapa waktu lalu saya membutuhkan cara bagaimana menyimpan hasil query select dari sebuah table pada database kedalam sebuah list. Setelah membaca beberapa referensi, saya akhirnya bisa juga. Untuk database yang saya gunakan pada contoh ini adalah MySQL. Berikut ini langkah-langkahnya.

1. Buat satu tabel yang isinya akan disimpan kedalam list

Contoh : tbl_person yang terdiri dari 4 kolom / field yaitu

person_id  int(5), person_name varchar(20)

person_address varchar(30), person_job varchar(20)

2. Buat satu buah class pada project java yang digunakan untuk melakukan koneksi ke MySQL.

Contoh : MyConnection.java

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

public class MyConnection {

private static Connection connection;

static public Connection getMyConnection(String urlDB, String userDB, String passwordDB)
{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(urlDB, userDB, passwordDB);
return connection;
} catch (InstantiationException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch(ClassNotFoundException ex){
ex.printStackTrace();
} catch(SQLException ex){
ex.printStackTrace();
}

return connection;
}

}

3. Buat satu class bean dari table yang kita buat pada langkah pertama

Contoh : Person.java

public class Person {

private int personID;
private String personName;
private String personAddress;
private String personJob;

/**
* @return the personID
*/
public int getPersonID() {
return personID;
}

/**
* @param personID the personID to set
*/
public void setPersonID(int personID) {
this.personID = personID;
}

/**
* @return the personName
*/
public String getPersonName() {
return personName;
}

/**
* @param personName the personName to set
*/
public void setPersonName(String personName) {
this.personName = personName;
}

/**
* @return the personAddress
*/
public String getPersonAddress() {
return personAddress;
}

/**
* @param personAddress the personAddress to set
*/
public void setPersonAddress(String personAddress) {
this.personAddress = personAddress;
}

/**
* @return the personJob
*/
public String getPersonJob() {
return personJob;
}

/**
* @param personJob the personJob to set
*/
public void setPersonJob(String personJob) {
this.personJob = personJob;
}

}

4. Buat pada main class sebagai berikut:

Contoh : Q2List.java

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
*
* @author Ren
*/
public class Q2List {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {

List<Person> listPerson = new ArrayList<>();

String userDB="root";
String urlDB="jdbc:mysql://localhost:3306/qlist";
String passwordDB="";
ResultSet rs;
String query = "select * from tbl_person";
Connection connection = MyConnection.getMyConnection(urlDB, userDB, passwordDB);

Statement statement = connection.createStatement();
rs = statement.executeQuery(query);

while(rs.next()){
Person person = new Person();
person.setPersonID(rs.getInt(1));
person.setPersonName(rs.getString(2));
person.setPersonAddress(rs.getString(3));
person.setPersonJob(rs.getString(4));

listPerson.add(person);

}

ListIterator <Person> itr;
itr = listPerson.listIterator();

while(itr.hasNext()) {
Person printPerson = itr.next();
System.out.println("ID : "+ printPerson.getPersonID()+"\n");
System.out.println("Name : "+ printPerson.getPersonName()+"\n");
System.out.println("Address : "+printPerson.getPersonAddress()+"\n");
System.out.println("Job : " +printPerson.getPersonJob()+"\n");

}

}
}

5. Output program :

Contoh Output

Contoh Output

Semoga bermanfaat. 🙂

Categories: Java Tags: , , ,