Hi.. I am vinay.. I am developing a small application using swings and mysql. I am sending part of the code here..
The problem is i need to update the mysql fields with values which are gettin from dynamiclly created JTextfields... but i am not getting how i can fetch those particular values and update database with those value.....
Please help me..
My work is paused here... please
Here the code....
package kp;
import java.awt.*; import java.util.regex.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*; import java.net.*; import java.util.*; import java.sql.*; import java.lang.*; public class attend extends JFrame{
private JLabel jLabel1;
private JButton jButton1;
private JTextField jTextField2;
private JPanel contentPane;
private JTextField jTextField1;
final Vector columnNames = new Vector();
final Vector data = new Vector();
Vector row;
final Object g=new Vector();
public attend()
{
super();
initializeComponent();
this.setVisible(true);
}
private void initializeComponent()
{
jLabel1 = new JLabel();
jButton1 = new JButton();
jTextField1 = new JTextField(20);
//jTextField1.setName("att");
contentPane = (JPanel)this.getContentPane();
contentPane.setBackground(Color.pink);
jLabel1.setText("ATTENDANCE");
jButton1.setText("Submit");
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/vinay", "root", "admin");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from student");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i));
if(i==4)
{
row.addElement(jTextField1);
}
}
data.addElement( row );
//
}
String k=jTextField1.getText();
System.out.println(k);
rs.close();
st.close();
}
catch(Exception e) {}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
contentPane.setLayout(null);
addComponent(contentPane, scrollPane, 5,110,200,200);
addComponent(contentPane, jLabel1, 50,1,345,37);
addComponent(contentPane, jButton1, 220,300,100,28);
this.setTitle("Class Attendance");
this.setLocation(new Point(66, 48));
this.setSize(new Dimension(438, 384));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Vector z=new Vector();
//System.out.println(data.get(2));
//System.out.println(row.elementAt(3));
//g=data.get(4);
//System.out.println(g);
try{
int i;
String sql=null;
Connection con = null;
//ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/vinay", "root", "admin");
if(con!=null)
{
System.out.println("connectin established");
}
Statement st = con.createStatement();
for(i=3;i>0;i-=3)
{
System.out.println(row.get(i));
String a=(String) row.get(i);
Pattern p=Pattern.compile("^p");
Matcher m=p.matcher(a);
if(m.find())
{
sql="update student set attend='"+a+"'";
System.out.println(sql);
st.executeUpdate(sql);
}
}
st.close();
con.close();
}
catch(Exception e1) {}
}
});
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
}
Hi Friend,
Please visit the following link:
Hope that it will be helpful for you.
Thanks