APLIKASI CLIENT SERVER
Oleh :
Nama : Nursella Sari
Kelas : 4CC
Nim : 061530701249
Dosen Pembimbing : Indarto S.T.,MCs
TEKNIK KOMPUTER
POLITEKNIK NEGERI SRIWIJAYA
2017
APLIKASI CLIENT-SERVER
Langkah pertama harus membuat 3 project terlebih dahulu, 2 project aplikasi dan 1
project class.
- Database : Java Class
- Database_server : Java Aplication
- Database_client : Java Aplication
Didalam project database terdapat 2 package, yaitu sella dan [Link]. Didalam
[Link] terdapat class Person sedangkan di [Link] ada java interface PersonService.
Person dibuat karena akan digunakan di project database client dan server. Person service
berupa data akses object.
- source code Person :
package sella;
import [Link];
import [Link];
public class Person implements Serializable {
private Long id;
private String firstName;
private String lastName;
private Date birthDay;
public Long getId() {
return id;
}
public void setId(Long id) {
[Link] = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
[Link] = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
[Link] = lastName;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
[Link] = birthDay;
}
}
- Source code PersonService
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public interface PersonService extends Remote {
Person insertPerson(Person person) throws RemoteException;
void updatePerson(Person person) throws RemoteException;
void deletePerson(Long id) throws RemoteException;
Person getPerson(Long id) throws RemoteException;
List<Person> getPerson() throws RemoteException;
}
Didalam project database_server terdapat 3 package. Di database_server dibuat class
server untuk menjalankan program. PersonServiceServer merupakan implementasi dari
database. Sedangkan DatabaseUtilities berguna untuk koneksi ke database.
- Source code Database_server :
package database_server;
import database_server.[Link];
import database_server.[Link];
import [Link];
import [Link];
import [Link];
public class Database_server {
public static void main(String[] args) throws RemoteException {
// TODO code application logic here
Registry server = [Link](6789);
PersonServiceServer personService = new PersonServiceServer();
[Link]("service", personService);
[Link]("Server berhasil berjalan");
}
}
- Source code PersonServiceServer :
package database_server.service;
import [Link];
import [Link];
import database_server.[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class PersonServiceServer extends UnicastRemoteObject implements
PersonService {
public PersonServiceServer() throws RemoteException {
}
@Override
public Person insertPerson(Person person) throws RemoteException {
[Link]("Client melakukan proses insert");
PreparedStatement statement = null;
try {
statement = [Link]().prepareStatement(
"INSERT INTO person (id, first_name, last_name, birth_day) values
(null, ?, ?, ?)");
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, new Date([Link]().getTime()));
[Link]();
ResultSet result = [Link]();
if ([Link]()) {
[Link]([Link](1));
}
[Link]();
return person;
} catch (SQLException exception) {
[Link]();
return null;
} finally {
if (statement != null) {
try {
[Link]();
} catch (SQLException exception) {
}
}
}
@Override
public void updatePerson(Person person) throws RemoteException {
[Link]("Client melakukan proses update");
PreparedStatement statement = null;
try {
statement = [Link]().prepareStatement(
"UPDATE person SET first_name = ?" +
", last_name = ? , birth_day = ? " +
"WHERE id = ?");
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, new Date([Link]().getTime()));
[Link](4, [Link]());
[Link]();
} catch (SQLException exception) {
[Link]();
} finally {
if (statement != null) {
try {
[Link]();
} catch (SQLException exception) {
[Link]();
}
}
}
}
@Override
public void deletePerson(Long id) throws RemoteException {
[Link]("Client melakukan proses delete");
PreparedStatement statement = null;
try {
statement = [Link]().prepareStatement(
"DELETE FROM person WHERE id = ?");
[Link](1, id);
[Link]();
} catch (SQLException exception) {
[Link]();
} finally {
if (statement != null) {
try {
[Link]();
} catch (SQLException exception) {
[Link]();
}
}
}
}
@Override
public Person getPerson(Long id) throws RemoteException {
[Link]("Client melakukan proses get-by-id");
PreparedStatement statement = null;
try {
statement = [Link]().prepareStatement(
"SELECT * FROM person WHERE id = ?");
ResultSet result = [Link]();
Person person = null;
if ([Link]()) {
person = new Person();
[Link]([Link]("id"));
[Link]([Link]("first_name"));
[Link]([Link]("last_name"));
[Link]([Link]("birth_day"));
}
[Link]();
return person;
} catch (SQLException exception) {
[Link]();
return null;
} finally {
if (statement != null) {
try {
[Link]();
} catch (SQLException exception) {
[Link]();
}
}
}
}
@Override
public List<Person> getPerson() throws RemoteException {
[Link]("Client melakukan proses get-all");
Statement statement = null;
try {
statement = [Link]().createStatement();
ResultSet result = [Link]("SELECT * FROM person");
List<Person> list = new ArrayList<Person>();
while([Link]()){
Person person = new Person();
[Link]([Link]("id"));
[Link]([Link]("first_name"));
[Link]([Link]("last_name"));
[Link]([Link]("birth_day"));
[Link](person);
}
[Link]();
return list;
} catch (SQLException exception) {
[Link]();
return null;
} finally {
if (statement != null) {
try {
[Link]();
} catch (SQLException exception) {
[Link]();
}
}
}
}
}
- Source code DatabaseUtilities :
package database_server.utilities;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DatabaseUtilities {
private static Connection connection;
public static Connection getConnection() {
if(connection == null) {
try {
[Link](new [Link]());
connection =
[Link]("jdbc:mysql://localhost:3306/client_server", "root",
"");
} catch (SQLException ex) {
[Link]([Link]()).log([Link],
null, ex);
}
}
return connection;
}
}
Didalam database_client terdapat 3 package. Pada Database_client itu
merupakan aplikasi nya atau program client yang akan dijalankan. Form merupakan
bentuk atau output dari aplikasi client server. TableModelPerson digunakan sebagai
model pada tabel
- Source code Database_client :
package database_client;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import database_client.[Link];
public class Database_client {
public static void main(String[] args) throws RemoteException,
NotBoundException {
// TODO code application logic here
Registry registry = [Link]("[Link]", 6789);
PersonService service = (PersonService) [Link]("service");
[Link](new Runnable() {
@Override
public void run() {
From form = new From(service);
[Link](true);
}
});
}
}
- Source code Form :
package database_client.form;
import [Link];
import [Link];
import database_client.[Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class From extends [Link] {
private TableModelPerson tableModelPerson = new TableModelPerson();
private PersonService personService;
public From(PersonService personService) {
[Link] = personService;
try {
[Link]([Link]());
} catch (RemoteException exception) {
[Link]();
}
initComponents();
[Link](tableModelPerson);
[Link]().addListSelectionListener(new
ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int row = [Link]();
if (row != -1) {
Person person = [Link](row);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
});
}
@SuppressWarnings("unchecked")
private void buttonRefreshActionPerformed([Link] evt) {
// TODO add your handling code here:
try {
List<Person> list = [Link]();
[Link](list);
}catch (RemoteException exception) {
[Link]();
}
}
private void buttonInsertActionPerformed([Link] evt) {
// TODO add your handling code here:
try {
Person person = new Person();
[Link]([Link]());
[Link]([Link]());
[Link]((Date) [Link]());
Person person1 = [Link](person);
[Link](person1);
}catch(RemoteException exception) {
[Link]();
}
}
private void buttonUpdateActionPerformed([Link] evt) {
// TODO add your handling code here:
try{
int row = [Link]();
if(row == -1) {
return;
}
Person person = [Link](row);
[Link]([Link]());
[Link]([Link]());
[Link]((Date) [Link]());
[Link](person);
[Link](row, person);
}catch(RemoteException exception){
[Link]();
}
}
private void buttonDeleteActionPerformed([Link] evt) {
// TODO add your handling code here:
try{
int row = [Link]();
if(row == -1) {
return;
}
Long id = [Link](row).getId();
[Link](id);
[Link](row);
}catch(RemoteException exception){
[Link]();
}
}
// Variables declaration - do not modify
private [Link] buttonDelete;
private [Link] buttonInsert;
private [Link] buttonRefresh;
private [Link] buttonUpdate;
private [Link] jLabel1;
private [Link] jLabel2;
private [Link] jLabel3;
private [Link] jLabel4;
private [Link] jPanel1;
private [Link] jPanel2;
private [Link] jScrollPane1;
private [Link] tablePerson;
private [Link] textBirthDay;
private [Link] textFirstName;
private [Link] textId;
private [Link] textLastName;
// End of variables declaration
}
- Form Aplikasi :
- Source code TableModelPerson :
package database_client.model;
import [Link];
import [Link];
import [Link];
import [Link];
public class TableModelPerson extends AbstractTableModel {
private List<Person> list = new ArrayList<Person>();
public TableModelPerson() {
}
public Person get(int row){
return [Link](row);
}
public void insert(Person person) {
[Link](person);
fireTableDataChanged();
}
public void update(int row, Person person) {
[Link](row, person);
fireTableDataChanged();
}
public void delete(int row) {
[Link](row);
fireTableDataChanged();
}
public void setData(List<Person> list) {
[Link] = list;
fireTableDataChanged();
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "Id";
case 1:
return "First Name";
case 2:
return "Last Name";
case 3:
return "Birth Day";
default:
return null;
}
}
@Override
public int getRowCount() {
return [Link]();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return [Link](rowIndex).getId();
case 1:
return [Link](rowIndex).getFirstName();
case 2:
return [Link](rowIndex).getLastName();
case 3:
return [Link](rowIndex).getBirthDay();
default:
return null;
}
}
}
HASIL :