I am trying to fetch some data in a jtable (on click of button) and in the database i have the path of the images stored. so i want to put them in the cell of JTable. My code is very big, because it contains some other information, like when we give a proper information in the text field, then only it will fetch me the data from the database.
This program is running fine, and there is no error in it, I just want to insert photos in one of the column in the table.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class VCstVote extends JFrame implements ActionListener{
JFrame frame;
JTextField tauid, tphone;
JLabel label, auid, aphone;
JButton cls, ok, vote;
JCheckBox chkbx;
JPanel panel,panel1,panel2, panel3;
static JTable table,table2;
DefaultTableModel model1 = new DefaultTableModel();
VCstVote() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
final UIManager.LookAndFeelInfo[] plafInfos =UIManager.getInstalledLookAndFeels();
UIManager.setLookAndFeel(plafInfos[1].getClassName() );
frame = new JFrame();
frame.setSize(930, 400);
frame.setTitle("VOTE YOUR CANDIDATE");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
vote = new JButton("VOTE YOUR CANDIDATE");
label = new JLabel("CASTE YOUR VOTE");
label.setFont(new Font("Verdana", Font.BOLD, 18));
panel.add(label);
panel1 = new JPanel();
cls= new JButton("CLOSE");
panel1.add(vote);
panel1.add(cls);
panel2 = new JPanel();
auid = new JLabel("UNIQUE ID",11);
tauid = new JTextField(10);
auid.setFont(new Font("Verdana", Font.BOLD, 10));
aphone = new JLabel("PHONE", 11);
aphone.setFont(new Font("Verdana", Font.BOLD, 10));
tphone = new JTextField(10);
ok = new JButton("SUBMIT");
panel2.add(auid);
panel2.add(tauid);
panel2.add(aphone);
panel2.add(tphone);
panel3 = new JPanel( new FlowLayout(FlowLayout.LEFT, 3,3));
panel3.add(ok);
panel2.add(panel3);
String[] columnNames = {"Candidate Name", "Department Name","Year","Photo","CASTE VOTE"};
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
cls.addActionListener(this);
ok.addActionListener(this);
vote.addActionListener(this);
frame.add(scroll, BorderLayout.WEST);
frame.add(panel2,BorderLayout.EAST);
frame.add(panel,BorderLayout.NORTH);
frame.add(panel1,BorderLayout.SOUTH);
frame.validate();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String action=ae.getActionCommand();
Connection conn = null ;
String cfn= null;
String cln=null;
if(action=="SUBMIT")
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott","tiger");
String query1="insert into vote (vuid,uphno) values(?,?)";
String query2 = "select uphno from accounts where vuid='"+tauid.getText()+"'";
String query3 = "select p.cfname, p.clname, p.deptname, l.dyear from potential p, leads l where p.cfname=l.cfname and p.clname=l.clname and p.deptname =l.deptname";
String query4 = "Select a.ufname, a.ulname from vote v, accounts a where a.uphno=v.uphno and v.vuid='"+tauid.getText()+"'";
chkbx = new JCheckBox();
PreparedStatement ps1 = conn.prepareStatement(query1);
PreparedStatement ps2 = conn.prepareStatement(query2);
PreparedStatement ps3 = conn.prepareStatement(query3);
PreparedStatement ps4 = conn.prepareStatement(query4);
ResultSet rs2= null;
ResultSet rs3= null;
ResultSet rs4= null;
rs2=ps2.executeQuery();
while(rs2.next())
{
ps1.setString(1, tauid.getText());
ps1.setString(2, rs2.getString(1));
ps1.executeUpdate();
conn.commit();
}
rs4= ps4.executeQuery();
while(rs4.next())
{
cfn = rs4.getString(1);
cln = rs4.getString(2);
}
JOptionPane.showMessageDialog(null, "Hii.. "+cfn+" "+cln+" Please Select Only one Candidate for voting, otherwise you vote will not be counted..");
rs3= ps3.executeQuery();
while(rs3.next())
{
String fname= rs3.getString(1);
String lname= rs3.getString(2);
String dept= rs3.getString(3);
String year= rs3.getString(4);
model1.addRow(new Object[]{fname+" "+lname,dept,year});
table.getColumn("CASTE VOTE").setCellEditor(new DefaultCellEditor(chkbx));
}
for (int i=0;i<model1.getRowCount();i++)
{
model1.setValueAt("false", i, 3);
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"Please Enter correct information");
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(action=="VOTE YOUR CANDIDATE")
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott","tiger");
String query1 ="select cfname,clname,deptname,luid from potential";
String test = tphone.getText();
String query2="update vote set cfname=?, clname=?, deptname=?, luid=?, votcount=1 where uphno="+test;
ResultSet rs1 = null;
PreparedStatement ps1= conn.prepareStatement(query1);
PreparedStatement ps2 = conn.prepareStatement(query2);
rs1 =ps1.executeQuery();
for(int i=0;i<model1.getRowCount();i++)
{
Object st = model1.getValueAt(i,3);
String ss= st.toString();
if(ss.equals("true"))
{
rs1.next();
String fname = rs1.getString(1);
String lname = rs1.getString(2);
String dept = rs1.getString(3);
String uid = rs1.getString(4);
ps2.setString(1, fname);
ps2.setString(2, lname);
ps2.setString(3, dept);
ps2.setString(4, uid);
ps2.executeUpdate();
conn.commit();
JOptionPane.showMessageDialog(null,"Successfully registred Your Vote");
}
else{
rs1.next();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(action=="CLOSE")
{
frame.setVisible(false);
}
}
public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
{
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
new VCstVote();
}
}
Please help me out of this, I tried many websites and codes, but it seems they are only for a defined set of data and images, none of them has a code which indicates jtable fetching data from database and rendering an image.