Friday, May 24, 2013

Java database application that allow user to insert, update and delete values in a table and manage appropriate exception handling when wrong values are enter



Aim:Write a database application that allow user to insert, update and delete values in a table and manage appropriate exception handling when wrong values are enter.

package bc;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
public class database extends javax.swing.JApplet
{
            public void init()
            {
                        setSize(350,250);
                        try
{
                                    java.awt.EventQueue.invokeAndWait(new Runnable()
                                    {
                                                public void run()
                                                {
                                                            initComponents();
                                                }
                                    });
                        }
                        catch (Exception ex)
                        {
                                    ex.printStackTrace();
                        }
            }
private void InsertActionPerformed(java.awt.event.ActionEvent evt)
{                                      
 try
                        {
                                    Connection con=DriverManager.getConnection("jdbc:odbc:DB1");
                                    Statement st= con.createStatement();
String stm="insert into student(Name,Department,Roll_No,Phone_No)     values('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+t4.getText()+"')";
                                    JOptionPane.showMessageDialog(rootPane,"Data is Inserted");
st.executeUpdate(stm);
                        }
                        catch(SQLException sqlEx)
                        {
                                     System.out.println("error");
                        }
             }                                     
    private void deleteActionPerformed(java.awt.event.ActionEvent evt)
   {                                       
  try
            {
                        Connection con=DriverManager.getConnection("jdbc:odbc:DB1");
                        Statement pst= con.createStatement();
                        String stm="delete from student where Roll_No="+t3.getText()+" ";
                        pst.executeUpdate(stm);
 JOptionPane.showMessageDialog(rootPane,"Data is deleted");
             }
            catch(SQLException sqlEx)
            {
                        System.out.println("error");
        }
   }                                     
   private void updateActionPerformed(java.awt.event.ActionEvent evt)
   {                                      
            try
            {
                        Connection con=DriverManager.getConnection("jdbc:odbc:DB1");
                        Statement pst= con.createStatement();
                        String stm=”update student set Name='"+t1.getText()+'",
                                             Department='"+t2.getText()+"',
                                             Phone_No='"+t4.getText()+"',
                                WHERE Roll_No="+t3.getText()+" ";
                         pst.executeUpdate(stm);
                        JOptionPane.showMessageDialog(rootPane,"Data is updated");
            }
            catch(SQLException sqlEx)
            {
                        System.out.println("error");
            }
    }      

Output:


 
 
                              

No comments:

Post a Comment