To show/Display Data in Table Format in Java
To Display Records of particular Table in Database into JTable one needs to import JAR
file rs2xml.jar in Build path in desired project(right click on project and select Build path configuration) and following statement in Declaration part in JAVA code, Below is the code when user click on Load Data button...
import net.proteanit.sql.DbUtils;
JButton btnNewButton = new JButton("Load Data");
btnNewButton.setFont(new Font("Verdana", Font.PLAIN, 12));
btnNewButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
try {
Connection conn = getConnection();
String query="select cast(staff_code as unsigned)Id,F_name Name,case sex when 'M' Then 'Male' when 'F' then 'Female' end Gender from sch_db.staff_master order by id";
PreparedStatement stat = conn.prepareStatement(query);
ResultSet res=stat.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(res));
}
catch (Exception e1) {
e1.printStackTrace();
}}
});
There is no need to declare array list and then populate it a bit complex method but it is simpler
way to do this in above defined way...
Do comment if you face any problem in doing that....
Comments
Post a Comment