You are on page 1of 2

What is a JTable?

JTable is a swing component that already placed in the java libraries.In the database projects this component
takes a viral role. But lots of students have no idea how this works. First of all you have to drag and drop JTable
from your swing palette.
And also there is a class called Vector. This Vector class behaves like an array. It can hold huge amount of data
rather than array and it is very easy to get those data from the vector class. You can see in the example how
that vector class took placed in the project. But I think it is somewhat hard to understand
that classes.But search something on the internet and find some factors about vector class.
I have added a new JFrame called table and I added JTable component into my frame. You can see in the
picture where the JTable placed.

I have renamed my JTable as tableResult and I am going to fill that table using Vector class from my MySQL
database. Lets we see how it happen.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
connection con=new connection();
Connection getcon=null;
Vector col = new Vector();
Vector dat= new Vector();
ResultSet rs = null;
try {
getcon = con.creatConnection();
col.add("Idccol.add("Titleccol.add("First");
col.add("Last");// create contact default colum names and sore it
col.add("Nick");//This texts will display in the header of the table
col.add("E-Mails=getcon.createStatement().executeQuery("select*from
contactinformation"); //getting all the information from the table
dat.clear(); //clear the object before use it

while(rs.next())

{// if record source avilable

Vector v =new Vector();


v.add(rs.getString("ContactId").trim());
v.add(rs.getString("title").trim());
v.add(rs.getString("fName").trim());
v.add(rs.getString("lName").trim());// getting contacts from database and store in
object
v.add(rs.getString("nName").trim());
v.add(rs.getString("email").trim());
dat.add(v);
}
tableResult.setModel(new DefaultTableModel(dat, col)); //finnaly set all the
values to the database
}
catch(Exception ex)

JOptionPane.showMessageDialog(null, ex.getMessage());
}
}

You might also like