
To accept details from user like name Birth date address contact no etc and store in a database

Hi Friend,
Try this:
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class FormData{
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Name: ");
JLabel label2=new JLabel("Date Of Birth: ");
JLabel label3=new JLabel("Address: ");
JLabel label4=new JLabel("Contact No: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
final JTextField text3=new JTextField(20);
final JTextField text4=new JTextField(20);
JButton b=new JButton("Save");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String v1=text1.getText();
String v2=text2.getText();
String v3=text3.getText();
int v4=Integer.parseInt(text4.getText());
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into data(name,dateOfBirth,address,contactNo) values('"+v1+"','"+v2+"','"+v3+"',"+v4+")");
JOptionPane.showMessageDialog(null,"Data is inserted successfully");
}
catch(Exception ex){
System.out.println(ex);
}
}
});
JPanel p=new JPanel(new GridLayout(5,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(label3);
p.add(text3);
p.add(label4);
p.add(text4);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.