Skip to main content

insert into database the values from swing components

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
pallavi_indukur
Offline
Joined: 2013-08-11

i have written a code to insert values into database from swing components when i print the values onto the console screen i get the correct values without any exception but when i insert the values into database it throws event dispatcher exception. can i know why and how to resolve the exception???
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;
class Registration extends JFrame implements ActionListener,ItemListener
{
String msg,name1,colName1,gender1,department1,year1,events1="";
JLabel nameLbl=new JLabel("Name");
JLabel genderLbl=new JLabel("Gender");
JLabel collegeLbl=new JLabel("College Name");
JLabel yearLbl=new JLabel("Year");
JLabel branchLbl=new JLabel("Branch");
JLabel eventsLbl=new JLabel("Events");
String[] genderOptions={"Male","Female"};
String[] yearOptions={"1","2","3","4","5"};
String[] branchOptions={"Computer Science","Information Technology","Electronics And Communication"};
JTextField name=new JTextField("");
JComboBox gender = new JComboBox(genderOptions);
JComboBox year = new JComboBox(yearOptions);
JComboBox branch = new JComboBox(branchOptions);
JTextField colName=new JTextField("");
JPanel panel=new JPanel();
ImageIcon registerIcon=new ImageIcon("images.jpg");
ImageIcon resetIcon=new ImageIcon("reset.jpg");
JButton register=new JButton("register");
JButton reset=new JButton("reset");
Image img;
Checkbox ad=new Checkbox("AdZap",false);
Checkbox dg=new Checkbox("Debugging",false);
Checkbox game=new Checkbox("Gaming",false);
Checkbox jam=new Checkbox("Just A Minute",false);
Checkbox op=new Checkbox("Online Photography",false);
Checkbox pp=new Checkbox("Paper Presentation",false);
Checkbox web=new Checkbox("Web Designing",false);
//public void paint(Graphics g)
//{
// img=getImage(getDocumentBase(),"exousia13.jpg");
// g.drawImage(img,0,0,this);
//}
public void dbConnect()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=database2.accdb;";
System.out.println("database");
Connection con=DriverManager.getConnection(database,"","");
System.out.println("connection created");
Statement st=con.createStatement();
System.out.println("statement creates");
String str="INSERT INTO register (Name,CollegeName,Gender,Department,Year,Events)VALUES('"+name1+"','"+colName1+"','"+gender1+"','"+department1+"','"+year1+"','"+events1+"')";
st.execute(str);
st.close();
con.close();
}
catch(Exception er)
{
er.printStackTrace();
System.out.println("Error:-"+er);
}
}
Registration()
{
super("Registration form");
setLayout(null);
nameLbl.setBounds(420,50,80,25);
name.setBounds(500,50,125,25);
genderLbl.setBounds(420,75,80,25);
gender.setBounds(500,75,125,25);
collegeLbl.setBounds(420,100,80,25);
colName.setBounds(500,100,125,25);
branchLbl.setBounds(420,125,80,25);
branch.setBounds(500,125,125,25);
yearLbl.setBounds(420,150,80,25);
year.setBounds(500,150,125,25);
eventsLbl.setBounds(420,175,80,25);
ad.setBounds(500,175,125,25);
dg.setBounds(625,175,125,25);
game.setBounds(500,200,125,25);
jam.setBounds(625,200,125,25);
op.setBounds(500,225,125,25);
pp.setBounds(625,225,125,25);
web.setBounds(500,250,125,25);
register.setBounds(500,300,100,40);
reset.setBounds(625,300,100,40);
add(nameLbl);
add(name);
add(genderLbl);
add(gender);
add(collegeLbl);
add(colName);
add(branchLbl);
add(branch);
add(yearLbl);
add(year);
add(eventsLbl);
add(ad);
add(dg);
add(game);
add(jam);
add(op);
add(pp);
add(web);
add(register);
add(reset);
name.addActionListener(this);
colName.addActionListener(this);
register.addActionListener(this);
reset.addActionListener(this);
gender.addActionListener(this);
branch.addActionListener(this);
year.addActionListener(this);
ad.addItemListener(this);
dg.addItemListener(this);
game.addItemListener(this);
jam.addItemListener(this);
op.addItemListener(this);
pp.addItemListener(this);
web.addItemListener(this);
}
public void reset() {
name.setText("");
colName.setText("");
ad.setState(false);
dg.setState(false);
game.setState(false);
jam.setState(false);
op.setState(false);
pp.setState(false);
web.setState(false);
}
public void actionPerformed(ActionEvent ae)
{
name.setEditable(true);
colName.setEditable(true);
String str1=ae.getActionCommand();
if(str1.equals("register"))
{
name1=name.getText();
colName1=colName.getText();
gender1=(String)gender.getSelectedItem();
department1=(String)branch.getSelectedItem();
year1=(String)year.getSelectedItem();
dbConnect();
System.out.println(name1+","+colName1+","+gender1+","+department1+","+year1);
}
else if(str1.equals("reset"))
{
reset();
}
}
public void itemStateChanged(ItemEvent ie)
{
events1+=(String)ie.getItem()+",";
}
}
public class RegFrame
{
public static void main(String args[])throws Exception
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
Registration reg=new Registration();
reg.setSize(1000,650);
reg.setVisible(true);
reg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}