TY_BCA_2015_ADV_JAVA_15MARKS_SLIP15



SLIP15:  Write a JDBC program in java to update an address of given customer(cid,cname,address) and display updated details.

import java.sql.*;
import java.io.*;
class Slip15_1
{
            public static void main(String args[])
            {
                        Connection con;
                        Statement stmt;
                        PreparedStatement ps;
                        String name;
                        try
                        {
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    con=DriverManager.getConnection("jdbc:odbc:dsn");
                                    if(con==null)
                                    {
                                                System.out.println("Connection Failed....");
                                                System.exit(1);
                                    }

                                    System.out.println("Connection Established...");

                                    stmt=con.createStatement();

                                    String query="Update Customer set address=? where name=?";
                                    ps=con.prepareStatement(query);

                                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                                    System.out.println("Enter Customer name for Update:");
                                    name=br.readLine();

                                    System.out.println("Enter new Address:");
                                    String addr=br.readLine();
                                    ps.setString(1,addr);
                                    ps.setString(2,name);
                                    int no=ps.executeUpdate();

                                    if(no==0)
                                    {
                                                System.out.println("Not updated in table....");
                                                System.out.println("Name Not match"+name);
                                    }          
                                    else
                                    {
                                                System.out.println("Succesfully updated in table....");

                                                ps=con.prepareStatement("select * from Customer where name=?");
                                                ps.setString(1,name);
                                                ResultSet rs=ps.executeQuery();
                                                System.out.println("cid\t"+"Name\t"+"address\t"+"ph_no");
                                                while(rs.next())
                                                {
                                                            System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)); 
                                                }
                                    }
                                    con.close();

                        }
                        catch(Exception e)
                        {
                                    System.out.println(e);
                        }
            }
}

No comments:

Post a Comment