Skip to main content

java insert using JFrame and Prepaired Statement

Java Program to insert record using JFrame and Prepaired Statement



Here is table Design 

Create table demo using two column as id  and Dname , id is primary key 


Program source code 




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

public class FrameInsert extends JFrame implements ActionListener{  
    JTextField tf1,tf2,tf3;  
    JButton b1,b2;  
JLabel l1,l2;

Connection con;    PreparedStatement psmt;   int result;
//declaration of control 
  FrameInsert(){  
        JFrame f= new JFrame("InsertDemo");  
//
l1=new JLabel("Enter RollNo");
l1.setBounds(150,10,150,50);
        tf1=new JTextField();  
        tf1.setBounds(150,50,150,20);  
l2=new JLabel("Enter Name");
l2.setBounds(150,60,150,50);
        tf2=new JTextField();  
        tf2.setBounds(150,100,150,20);
        tf3=new JTextField();  
        tf3.setBounds(150,150,150,20);
       tf3.setEditable(false);   
        b1=new JButton("Add");  
        b1.setBounds(150,200,100,50);  
        b2=new JButton("Clear");  
        b2.setBounds(250,200,100,50); 
        b1.addActionListener(this);  // event handing power 
        b2.addActionListener(this);  
f.add(l1); f.add(l2);
        f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);  
        f.setSize(500,300);  
        f.setLayout(null);  
        f.setVisible(true);  
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }         
    public void actionPerformed(ActionEvent e) {  
       String s1=tf1.getText(); 
            int id =Integer.parseInt(s1);  
  
       String nm=tf2.getText();  
   
        
      
        if(e.getSource()==b1){  
  try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  // loding Driver
  
con=DriverManager.getConnection("jdbc:odbc:demodb");
 
  psmt=con.prepareStatement("insert into demo(id,dname)values(?,?)");
  
            psmt.setInt(1,id);
psmt.setString(2,nm);
 
int result=psmt.executeUpdate();
  tf3.setText("record inserted");  
  
  con.close();
  }
  catch(Exception e1)
  {
  tf3.setText("Error" +e1);
  
  }
            
        }else if(e.getSource()==b2){  
           tf1.setText(" ");
  tf2.setText(" ");
    tf3.setText("record cleared");
        }  
        
    }  
public static void main(String[] args) {  
    new FrameInsert();  
} }  




You Can Find Related post 



Java Program to insert record using JFrame and Prepaired Statement


Java Program to delete record using JFrame and Prepaired Statement


Java Program to update record using JFrame and Prepaired Statement


Java Program to Display  record using JFrame and Prepaired Statement










Labels

Show more

Popular posts from this blog

Basic java

java java java, learn java online free, classroom java tutorial, beginners java, java beginners, java programming,  basic java , basic java Tutorial,  basic java  Assignment           Hello friends ,       I am learning Basic java . my motive behind this blog is to help those want to learn Basic java online free of cost and grab each and every basic aspect of java programming .     you can find java tutorial from basic on this blog i will post each and every assignment of basic java with source code .          we will divide our course  in to Day work .          Let's start Day One  Basic Java Assignment for  Beginners      Basic Requirement to learn Java Programming Download following software             1. Text Editor (Notepad, Notepad++, Edit pad, Eclipse...

java ms access connectivity

java ms access connectivity java ms access connectivity Step by Step jdbc odbc driver for ms access java database connectivity with ms access 2007 java ms access connectivity steps how to connect java with ms access 2007 database with an example how to connect ms access database in java without dsn how to connect ms access database in java without using dsn Hi Friends in this article i am using ms access 2007 database as a back end, using java simple code we will connect to emp database created in ms access , after connecting with emp we are going to perform operation like display records from emp using java simple code, add record to emp using java simple code, delete record from emp using java simple code , update /edit emp using java simple code , i am using simple because vary basic code you found here . follow steps you can watch video or download PDF very Soon  for this article .  First  Create Table  use MS ACCESS 2007 or later as shown in imag...

java MS ACCESS insert,update delete using PreparedStatement

java program delete record from table using  PreparedStatement java MS ACCESS  insert,update  delete using  PreparedStatement import java.sql.*; class DeletePreparest {   public static void main(String s[])   {     try {   int cno=Integer.parseInt(s[0]);  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  Connection con=DriverManager.getConnection("jdbc:odbc:demodb"); // Statement smt=con.createStatement();  PreparedStatement psmt=con.prepareStatement("delete from demo where id=?");  psmt.setInt(1,cno);  int result=psmt.executeUpdate();       System.out.println("record deleted"+result);  con.close(); } catch(Exception e){    System.out.println("exce"+e);}         } }