Saturday 9 November 2013

how to insert data into database with java swing

 First of all set your Database:
    STEP 1: goto your contorl panal
                ->Adminstrative tool
                ->Data Source (ODBC)
  STEP 2: Add new Microsoft Access  driver (*.mdb,*.accdb)
                ->select your database from pressing select button
                ->Databse name as  login1
                 ->then ok and ok
 enjoy....!!! below code
cretae java file compile it ....!!!!
----------------------------------------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
 *
 * @author #Dx Panchal
 */
public class Registration extends JFrame implements ActionListener {
    Container c=getContentPane();
    private JLabel lbluser,lblpswd,lblcpswd;
    private JTextField txtuser;
    private JPasswordField txtpswd,txtcpswd;
    private JButton btnreg,btncnl;
    public Registration()
    {
    super("Registration");
    this.setSize(400,600);
    this.setLayout(null);
    this.setResizable(false);
    this.setLocation(45,45);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
   
    lbluser=new JLabel("Enter Username:");
    lblpswd=new JLabel("Enter Password:");
    lblcpswd=new JLabel("Repeat Password:");
    txtuser=new JTextField();
    txtpswd=new JPasswordField();
    txtcpswd=new JPasswordField();
    btnreg=new JButton("Register");
    btncnl=new JButton("Cancle");
   
    lbluser.setBounds(50,60,140,25);
    txtuser.setBounds(160,60, 140, 25);
    lblpswd.setBounds(50,100, 140, 25);
    txtpswd.setBounds(160,100, 140, 25);
    lblcpswd.setBounds(50,140, 140, 25);
    txtcpswd.setBounds(160,140, 140, 25);
    btnreg.setBounds(160, 180,100, 25);
    btncnl.setBounds(280,180,100,25);
   
   
    this.add(lbluser);
    this.add(txtuser);
    this.add(lblpswd);
    this.add(txtpswd);
    this.add(lblcpswd);
    this.add(txtcpswd);
    this.add(btnreg);
    this.add(btncnl);
   
    btnreg.addActionListener(this);
    btncnl.addActionListener(this);
   
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==btnreg)
    {
        try
        {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection con=DriverManager.getConnection("Jdbc:Odbc:login1");
           Statement st=con.createStatement();
           try
           {
               String username=txtuser.getText();
               String password=txtpswd.getText();
               String cpassword=txtcpswd.getText();
               if(!cpassword.equals(password))
               {
                JOptionPane.showMessageDialog(null,"Password does not match...!!! ","Error",JOptionPane.ERROR_MESSAGE);
               txtuser.setText("");
               txtpswd.setText("");
               txtcpswd.setText("");
               return;
               }
              String str="INSERT INTO login(username,password) values('"+username+"','"+password+"')";
              PreparedStatement ps=con.prepareStatement(str);
              ps.executeUpdate();
             JOptionPane.showMessageDialog(null,"Congratulation your registration done successfully..!!","Thanks to join",JOptionPane.INFORMATION_MESSAGE);
              
           }
           catch(Exception x)
           {
                     JOptionPane.showMessageDialog(null, "Error,Unable to perform database operation", "Error", JOptionPane.ERROR_MESSAGE);
      
           }
        }
        catch(Exception ex)
        {
         JOptionPane.showMessageDialog(null, "Error on database connection, Cannot perform database operation", "Error", JOptionPane.ERROR_MESSAGE);
  
        }
       
    }
   
    if(e.getSource()==btncnl)
    {
    txtuser.setText("");
               txtpswd.setText("");
               txtcpswd.setText("");
   
    }
   
       
    }
    public static void main(String args[])
    {
         new Registration().setVisible(true);   
    }
}

No comments:

Post a Comment

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...