Wednesday, 24 January 2018

Advancedjava-Slip15



Slip15

Q1. 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
{
            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:lab.dsn");
                                   
                                    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);
                        }
            }
}                                             
                          
                                                                                                     
Q2. Write a SERVLET program in java to accept details of student (SeatNo, Stud_Name, Class, Total_Marks). Calculate percentage and grade obtained and display details on page.  

student.html
<html>
<body>
<form name="f1" method="Post" action="http://localhost:8080/Stud">

Enter Roll No :&nbsp <input type="text" name="txtsno"><br><br>
Enter Name :&nbsp &nbsp <input type="text" name="txtnm"><br><br>
Enter class :&nbsp &nbsp &nbsp <input type="text" name="txtclass"><br><br>
Subject 1 :&nbsp &nbsp &nbsp <input type="text" name="txtsub1"><br><br>
Subject 2 :&nbsp &nbsp &nbsp <input type="text" name="txtsub2"><br><br>
Subject 3 :&nbsp &nbsp &nbsp <input type="text" name="txtsub3"><br><br>

<input type="submit" value="Result">
</div>
</form>
</body>
</html>
                
student.java

 import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
public class Student extends HttpServlet
{
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
        int sno,s1,s2,s3,total;
        String snm,sclass;
        float per;
         res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        sno=Integer.parseInt(req.getParameter("txtsno"));
        snm=req.getParameter("txtnm");
        sclass=req.getParameter("txtclass");
                s1=Integer.parseInt(req.getParameter("txtsub1"));
        s2=Integer.parseInt(req.getParameter("txtsub2"));
        s3=Integer.parseInt(req.getParameter("txtsub3"));
        total=s1+s2+s3;
        per=(total/3);
        out.println("<html><body>");
        out.print("<h2>Result of student</h2><br>");
        out.println("<b><i>Roll No :</b></i>"+sno+"<br>");
        out.println("<b><i>Name :</b></i>"+snm+"<br>");
        out.println("<b><i>Class :</b></i>"+sclass+"<br>");
        out.println("<b><i>Subject1:</b></i>"+s1+"<br>");
        out.println("<b><i>Subject2:</b></i>"+s2+"<br>");
        out.println("<b><i>Subject3:</b></i>"+s3+"<br>");
        out.println("<b><i>Total :</b></i>"+total+"<br>");
        out.println("<b><i>Percentage :</b></i>"+per+"<br>");
        if(per<50)
            out.println("<h1><i>Pass Class</i></h1>");
        else if(per<55 && per>50)
            out.println("<h1><i>Second class</i></h1>");
        else if(per<60 && per>=55)
            out.println("<h1><i>Higher class</i></h1>");
        out.close();
    }
}