0% found this document useful (0 votes)
21 views2 pages

Membuat Method Untuk Menghubungkan Dengan Server: Static Try

The document describes methods for connecting to a database, querying customer data, and populating a JTable with the results. It defines a Customers class to represent customer records, a method to connect to a MySQL database, a method to query for customers with names containing "gift" and save the results to an ArrayList, and a method to populate a JTable by adding the customer objects from the ArrayList as rows.

Uploaded by

filantropi yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Membuat Method Untuk Menghubungkan Dengan Server: Static Try

The document describes methods for connecting to a database, querying customer data, and populating a JTable with the results. It defines a Customers class to represent customer records, a method to connect to a MySQL database, a method to query for customers with names containing "gift" and save the results to an ArrayList, and a method to populate a JTable by adding the customer objects from the ArrayList as rows.

Uploaded by

filantropi yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Membuat method untuk menghubungkan dengan server


static Connection BuatKoneksi()
{
Connection conn;
try
{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "logintoroot");
}
catch(SQLException e)
{
return null;
}
return conn;
}

2. Membuat Class Customers (sesuai dengan tabel yang ditampilkan)


class Customers
{
private int cstNum;
private String cstName, conLName, conFName, city;

public Customers(int cstNum, String cstName, String conLName, String conFName, String city)
{
this.cstNum=cstNum;
this.cstName=cstName;
this.conLName=conLName;
this.conFName=conFName;
this.city=city;
}
public int getcustNumber()
{
return cstNum;
}
public String getcustName()
{
return cstName;
}
public String getconLName()
{
return conLName;
}
public String getconFName()
{
return conFName;
}
public String getcity()
{
return city;
}
}

3. Melakukan query dan menyimpan hasilnya pada ArrayList


public ArrayList<Customers> DaftarPelanggan()
{
ArrayList<Customers> listpelanggan = new ArrayList<>();
Connection con = BuatKoneksi();
Statement st;
ResultSet rs;
try
{
st=con.createStatement();
rs=st.executeQuery("SELECT * FROM customers WHERE customerName LIKE '%gift%'");
Customers pelanggan;
while(rs.next())
{
pelanggan = new Customers(rs.getInt("customerNumber"),
rs.getString("customerName"),rs.getString("contactLastName"),
rs.getString("contactFirstName"),rs.getString("city"));
listpelanggan.add(pelanggan);
}
}
catch(SQLException e)
{
return null;
}
return listpelanggan;
}

4. Memasukkan ArrayList ke dalam Jtable


public void PopulateTable()
{
ArrayList<Customers> list = DaftarPelanggan();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
Object[] row = new Object[5];
for(int i = 0; i < list.size(); i++)
{
row[0] = list.get(i).getcustNumber();
row[1] = list.get(i).getcustName();
row[2] = list.get(i).getconLName();
row[3] = list.get(i).getconFName();
row[4] = list.get(i).getcity();

model.addRow(row);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy