Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Problem with JRadioButton and Access Database
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


sushant
Problem with JRadioButton and Access Database
2 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners


Hello sir ,Here I have write following code to save text on JradioButton but this is not saved in Access Databse,
I want to store course type in to database and also other fields value .
plz help Me.
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

class coursefrm {
JFrame f;
JPanel p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10,l11,l12;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JRadioButton r1,r2;
JScrollPane sp1;
JTextField text;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;
coursefrm(){
f=new JFrame("Form");
p1=new JPanel(new GridLayout(7,3));
p2=new JPanel(new GridLayout(6,2));
p3=new JPanel(new GridLayout(2,2));
tp=new JTabbedPane();
l1=new JLabel("Course ID:");
l2=new JLabel("Course Name:");
l3=new JLabel("Duration:");
l4=new JLabel("Fees:");
l5=new JLabel("Enter Course ID:");
l7=new JLabel("Course ID:");
l8=new JLabel("Course Name:");
l9=new JLabel("Duration:");
l10=new JLabel("Fees:");
l11=new JLabel("Course Type:");
l12=new JLabel();
text=new JTextField(15);
tf1=new JTextField(12);
tf2=new JTextField(12);
tf3=new JTextField(12);
tf4=new JTextField(12);
tf5=new JTextField(12);
tf6=new JTextField(12);
tf7=new JTextField(12);
tf8=new JTextField(12);
tf9=new JTextField(12);
tf10=new JTextField(12);
r1=new JRadioButton("SemWise");
r2=new JRadioButton("YearWise");
savebtn=new JButton(" Add ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(l11);
p1.add(r1);
p1.add(l12);
p1.add(r2);
p1.add(savebtn);
p1.add(resetbtn);
p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);
p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});

savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int value1=Integer.parseInt(tf1.getText());
String value2=tf2.getText();
int value3=Integer.parseInt(tf3.getText());
int value4=Integer.parseInt(tf4.getText());
String g=text.getText();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
PreparedStatement st=con.prepareStatement("insert into course(id,name,duration,fees,coursetype) values(?,?,?,?,?)");
st.setInt(1,value1);
st.setString(2,value2);
st.setInt(3,value3);
st.setInt(4,value4);
st.setString(5,g);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});
deletebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=tf5.getText();
int ide=Integer.parseInt(value1);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
PreparedStatement st=con.prepareStatement("DELETE FROM course WHERE id = ?");
st.setInt(1,ide);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
catch(Exception exp3){
JOptionPane.showMessageDialog(p3,"Error in deleting record.");
}
}
});
editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value=tf7.getText();
int ide=Integer.parseInt(value);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
PreparedStatement st=con.prepareStatement("select * from course where id=?");
st.setInt(1,ide);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can not edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0){
try{
int value1=Integer.parseInt(tf7.getText());
String value2=tf8.getText();
int value3=Integer.parseInt(tf9.getText());
int value4=Integer.parseInt(tf10.getText());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
Statement st=con.createStatement();
//System.out.println("update course set name='"+value2+"', duration='"+value3+"',fees='"+value4+"' where id='"+value1+"'");
st.executeUpdate("update course set name='"+value2+"' , duration="+value3+",fees="+value4+" where id="+value1+"");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);
f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
coursefrm pro=new coursefrm();
pro.dis();
}
/*public ActionListener al ( new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText(((JRadioButton) e.getSource()).getText());
}
});*/
}

Advertisement
View Answers

March 27, 2010 at 10:30 AM


Hi Friend,

You haven't performed any action on radio button.We have modified your code.Now it works.

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

class coursefrm {
JFrame f;
JPanel p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10,l11,l12;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JRadioButton r1,r2;
JScrollPane sp1;
JTextField text;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;
coursefrm(){
f=new JFrame("Form");
p1=new JPanel(new GridLayout(7,3));
p2=new JPanel(new GridLayout(6,2));
p3=new JPanel(new GridLayout(2,2));
tp=new JTabbedPane();
l1=new JLabel("Course ID:");
l2=new JLabel("Course Name:");
l3=new JLabel("Duration:");
l4=new JLabel("Fees:");
l5=new JLabel("Enter Course ID:");
l7=new JLabel("Course ID:");
l8=new JLabel("Course Name:");
l9=new JLabel("Duration:");
l10=new JLabel("Fees:");
l11=new JLabel("Course Type:");
//l12=new JLabel();
text=new JTextField(12);
tf1=new JTextField(12);
tf2=new JTextField(12);
tf3=new JTextField(12);
tf4=new JTextField(12);
tf5=new JTextField(12);
tf6=new JTextField(12);
tf7=new JTextField(12);
tf8=new JTextField(12);
tf9=new JTextField(12);
tf10=new JTextField(12);
r1=new JRadioButton("SemWise");
r2=new JRadioButton("YearWise");
savebtn=new JButton(" Add ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
r1.addActionListener(al);
r2.addActionListener(al);
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(l11);
p1.add(r1);
p1.add(text);
p1.add(r2);
p1.add(savebtn);
p1.add(resetbtn);
p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);
p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
text.setVisible(false);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});

savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int value1=Integer.parseInt(tf1.getText());
String value2=tf2.getText();
int value3=Integer.parseInt(tf3.getText());
int value4=Integer.parseInt(tf4.getText());
String g=text.getText();
System.out.println(g);


March 27, 2010 at 10:30 AM


continue..

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:access","","");
PreparedStatement st=con.prepareStatement("insert into course(id,name,duration,fees,coursetype) values(?,?,?,?,?)");
st.setInt(1,value1);
st.setString(2,value2);
st.setInt(3,value3);
st.setInt(4,value4);
st.setString(5,g);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});
deletebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=tf5.getText();
int ide=Integer.parseInt(value1);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
PreparedStatement st=con.prepareStatement("DELETE FROM course WHERE id = ?");
st.setInt(1,ide);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
catch(Exception exp3){
JOptionPane.showMessageDialog(p3,"Error in deleting record.");
}
}
});
editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value=tf7.getText();
int ide=Integer.parseInt(value);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
PreparedStatement st=con.prepareStatement("select * from course where id=?");
st.setInt(1,ide);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can not edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0){
try{
int value1=Integer.parseInt(tf7.getText());
String value2=tf8.getText();
int value3=Integer.parseInt(tf9.getText());
int value4=Integer.parseInt(tf10.getText());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:mydsn","","");
Statement st=con.createStatement();
//System.out.println("update course set name='"+value2+"', duration='"+value3+"',fees='"+value4+"' where id='"+value1+"'");
st.executeUpdate("update course set name='"+value2+"' , duration="+value3+",fees="+value4+" where id="+value1+"");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);
f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
coursefrm pro=new coursefrm();
pro.dis();
}
private ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText(((JRadioButton) e.getSource()).getText());
}
};
}

Thanks


Related Tutorials/Questions & Answers:
Problem with JRadioButton and Access Database - Java Beginners
Problem with JRadioButton and Access Database  Hello sir ,Here I have write following code to save text on JradioButton but this is not saved in Access Databse, I want to store course type in to database and also other fields
How to Validate JRadioButton and How to Save data from JRadioButton to MS Access Database - Java Beginners
How to Validate JRadioButton and How to Save data from JRadioButton to MS Access Database  Hello Sir I want Store Corse Type that contains Two JRadioButton I want Store Information from JRadioButton to MS Access Database, and I
Advertisements
Save JRadioButton Text in to database - Java Beginners
Save JRadioButton Text in to database   Hello Sir I have Two... JRadioBuuton in to Access Databse and Access Databse. with user can select any one...=new JTextField(15); final JRadioButton c1,c2; ButtonGroup radioGroup=new
Problem with Double type Value in Access Database - Java Beginners
Problem with Double type Value in Access Database  Hello sir I have store Double Type Value in MS Access Databse,but when I search Record... Mobile Number with MS access Database,plz Help Me Sir.   Hi Friend
ACCESS DATABASE FROM HTML
ACCESS DATABASE FROM HTML  I want to access sql 2008 database in html page without help of ADODB connection.. because if access through ADODB means there is a security problem. so, Access database in html page(client side
Connect database in Access to Netbean?
Connect database in Access to Netbean?  how to connect database in micrsoft access to Netbean?i know it can be connected by using JDBC-ODBC bridge, can i know the steps in connecting the database? Is there any source code
Access 2007 database connectivity
Access 2007 database connectivity  i design an application form... source and destination. pls tell me the code of connectivity with access 2007 database using JComboBox.thanks
problem in database
problem in database   thanks for web site. I want change this code to insert data into PostgreSql database using jsp,servlets. but i getting...){ out.println("Couldn't load database driver: " + e.getMessage()); } catch
connecting to access database
Access Driver(*.mdb) Select database name and Create the DSN name (e.g access...connecting to access database  print("code sample");Hi I Write java... this there is no error but my data is not going to my Acess Database. There is working
database problem
database problem  I did connect my jsp page to database and it showed the result on another jsp page but i want to show the data of that field on which i click to mouse from the jsp page where all the data field has been showed
database problem
database problem  I did connect my jsp page to database and it showed the result on another jsp page but i want to show the data of that field on which i click to mouse from the jsp page where all the data field has been showed
link hibernate to MS ACCESS database
link hibernate to MS ACCESS database  how to link hibernate to ms access database instead of sql database
how to access database in applet
how to access database in applet  HI... I'm having an applet where we should display the database values in the applet... It works fine in the local system(same network)... but when its in the server, we r getting null values
Applet database access - Applet
Applet database access  HI... I'm having an applet where we should display the database values in the applet... It works fine in the local system... but when its in the server, we r getting null values in the local system.. I
JSP and Database access
JSP and Database access  Hi, Please help me with the following program. I am not able to update all the pa column values in my database. csea.jsp: <html> <body> <%@page import="java.sql.*"%> <form method
how to access the MS ACCESS database with java
how to access the MS ACCESS database with java   how to access the MS ACCESS database with java how can we insert,delete,update,search records of ms access with java   Java MS Access database connectivity Follow
problem in jsp using ms-access
problem in jsp using ms-access  after starting server(tomcat) wen v... and select the driver Microsoft Access Driver(*.mdb). 3)After selecting... the driver Microsoft Access Driver(*.mdb). 3)After selecting the driver, click
java servlet connectivity problem with access
java servlet connectivity problem with access  Import java.sql javax.servlet //all packages entered try { Class.forName...=con.createStatement(); ^ i am confused what is the problem
MS-ACCESS Query Problem - SQL
MS-ACCESS Query Problem  hi sir i have table which is initially have empty records Deleted Attributes: Roll Name Add Age Course Class... = DriverManager.getConnection("jdbc:odbc:access"); Statement st
The JDBC API to access a desktop database like Microsoft Access over the network.
The JDBC API to access a desktop database like Microsoft Access over the network.  How can I use the JDBC API to access a desktop database like Microsoft Access over the network
how to access the MS ACCESS database with java - Java Beginners
how to access the MS ACCESS database with java  how can we insert,delete,update,search records of ms access with java
code for insert the value from jsp to access database
code for insert the value from jsp to access database  code for insert the value from jsp to access database
Migrating sql database to Access through coding
Migrating sql database to Access through coding  How can i migrate SQL database(table) to My access through coding
problem of static in jsp page by multiple user access
problem of static in jsp page by multiple user access  hi , i am continuing my online exam project,but i have a Singleton class which i am invoking from my jsp page , ths page can access by more number of users .every user
Oracle 9i database problem
Oracle 9i database problem  Hi Somebody has given the solution but i.............,send reply immediately. I have a problem in Oracle9i .I have installed... what i created .But after closing the SQL+ ,the problem started.When i again opened
Database problem - WebSevices
simple database connectivity code. if you have any problem then send me detail...Database problem  Hello, How i connect my database file& tables to Zend library file. In which library file i should change that code
Problem in accessing data from Database
Problem in accessing data from Database  hi..... i'm making a project on servlet and jsp with ms access 2007 at the backend. One field in my database... someone plz help me out with this problem
jfreechart display from access database data.
jfreechart display from access database data.  I have made a database... to retrieve the data from the access database using prepared statement and then display... is to be done in a servlet.. Note that it is a access made database. How can I
updation problem during transaction to ms-access
updation problem during transaction to ms-access  Hey friends I am a beginner to java,and my problem is related to updation query in ms-access.Actually have created a database and through a jform button I am retrieving data
updation problem during transaction to ms-access
updation problem during transaction to ms-access  Hey friends I am a beginner to java,and my problem is related to updation query in ms-access.Actually have created a database and through a jform button I am retrieving data
updation problem during transaction to ms-access
updation problem during transaction to ms-access  Hey friends I am a beginner to java,and my problem is related to updation query in ms-access.Actually have created a database and through a jform button I am retrieving data
 

 

 

DMCA.com