Friday, 23 March 2018

Simple Java Form for DB

Hi All,

Use this code.


-------------------------------
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class sample1 extends JFrame
{
 public static Connection cons;
 public Statement s;
 ResultSet rs;
 public JTextField Name_text=new JTextField(20);
 public JTextField User_Name_text=new JTextField(20);
 JPasswordField Password_text=new JPasswordField(20);
 JTextField City_text=new JTextField(20);
 public sample1()
{
  super("Registration Page");
  setSize(500,500);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  Container c=getContentPane();
  c.setLayout(null);
 
  //creating the fields
  setIconImage(getToolkit().getImage("icon.jpeg"));
 
  JLabel name_label=new JLabel("Name");
  JLabel User_Name_label=new JLabel("User Name");
  JLabel Password_label=new JLabel("Password");
  JLabel DOB_label=new JLabel("DOB");
  JLabel Address_label=new JLabel("Address");
  JLabel City_label=new JLabel("city");
  JLabel Gender_label=new JLabel("Gender");
 
  JTextField DOB_text=new JTextField(20);
  JTextField Address_text=new JTextField(20);

  JTextField Gender_text=new JTextField(20);
 
  ButtonGroup buttongroup=new ButtonGroup();
  JRadioButton Male_rbutton=new JRadioButton("Male");
  JRadioButton Female_rbutton=new JRadioButton("Female");
  buttongroup.add(Male_rbutton);
  buttongroup.add(Female_rbutton);
  JButton Submit_button=new JButton("Submit");
 


   Submit_button.addActionListener(new ActionListener()
   {
   public void actionPerformed(ActionEvent e)
   {
   
    try
     {
    String password_text =Password_text.getText();
    String city_text=City_text.getText();
   
   
       s=cons.createStatement();
       rs=s.executeQuery("select * from table1");     
       rs.moveToInsertRow();
 
     
       rs.updateString(3,password_text);
       rs.updateString(6,city_text);
       
    rs.insertRow();

       
       
       
       /*Name_text.setText(rs.getString(1));
       User_Name_text.setText(rs.getString(2));*/
       
     }

    catch(SQLException sqlex)
    {
     System.out.println("error is "+sqlex);
    }
       
       
   }
 
   }
   );
   


 

  //Aligning the Fields
  name_label.setBounds(50,50,50,20);
  Name_text.setBounds(130,50,90,20);
  User_Name_label.setBounds(50,80,80,20);
  User_Name_text.setBounds(130,80,90,20);
  Password_label.setBounds(50,110,80,20);
  Password_text.setBounds(130,110,90,20);
  DOB_label.setBounds(50,140,50,20);
  DOB_text.setBounds(130,140,90,20);
  Gender_label.setBounds(50,170,50,20);
  Male_rbutton.setBounds(130,170,60,20);
  Female_rbutton.setBounds(190,170,90,20);
  Address_label.setBounds(50,200,50,20);
  Address_text.setBounds(130,200,90,20);
  City_label.setBounds(50,230,50,20);
  City_text.setBounds(130,230,90,20);
  Submit_button.setBounds(160,270,90,20);
  Male_rbutton.setSelected(true);

  //adding the fields
  c.add(name_label);
  c.add(User_Name_label);
  c.add(Password_label);
  c.add(DOB_label);
  c.add(Address_label);
  c.add(City_label);
  c.add(Gender_label);
  c.add(Name_text);
  c.add(User_Name_text);
  c.add(Password_text);
  c.add(DOB_text);
  c.add(Address_text);
  c.add(City_text);
  c.add(Male_rbutton);
  c.add(Female_rbutton);
  c.add(Submit_button); 
  setVisible(true);
  }




public static void main(String args[])
 {

  sample1 sobj=new sample1();

  try
  {
   
 
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:sampletwo");
   
       
       
   
  }


  catch(ClassNotFoundException cnfex)
  {
    System.err.println("failed to load driver");
    cnfex.printStackTrace();
    System.exit(1);

  }
 
  catch(SQLException sqlex)
{
  System.err.println("unable to connect");
  sqlex.printStackTrace();
}
   
 



}


}

No comments:

Post a Comment

Swing Menu

Hi All, Use this code. ------------------------------------------------------- package placement; import java.awt.*; import java.io.*...