P. 1
java question

java question

|Views: 111|Likes:
Published by Ajay Joshi

More info:

Published by: Ajay Joshi on Mar 14, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

01/24/2011

pdf

text

original

ASSIGNMENT NO: 1 Q1.

Write a program using Microsoft Access that will display two fields from stu dent table? PROGRAM CODE: import java.sql.*; public class student1 { public student1() { try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(Exception e){System.out.println("Class Not Found");} try{ Connection con=DriverManager.getConnection("jdbc:odbc:DSN1"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from student"); while(rs.next()) { System.out.println(" " + rs.getString(1) + " " + rs.getS tring(2)); } } catch(Exception e) {System.out.println("SQL ERROR");} } public static void main(String [] args) { new student1(); } }

OUTPUT: Microsoft Access:

ASSIGNMENT NO:2 Q2.Write a program using Microsoft access that will display all fields from empl oyee table? PROGRAM CODE: import java.sql.*; public class employee { public employee()

{ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(Exception e){System.out.println("Class Not Found");} try{ Connection con=DriverManager.getConnection("jdbc:odbc:DSN2"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from employee"); while(rs.next()) { System.out.println(" " + rs.getString(1) + " " + rs.getString(2) + " " +rs.getSt ring(3)+ " " +rs.getString(4)+ " " +rs.getString(5)+ " " +rs.getString(6)+ " " + rs.getString(7)+ " " +rs.getString(8)); } } catch(Exception e) {System.out.println("SQL ERROR");} } public static void main(String [] args) { new employee(); } }

OUTPUT: ASSIGNMENT NO: 3 Q3: Write a menu driven program for the following operation with database? 1. Display 2. Update 3. Inserts 4. Delete (accept roll no for update & delete) PROGRAM CODE: import java.sql.*; import java.io.*; public class testmenu { public testmenu() { try{Class.forName("org.gjt.mm.mysql.Driver");} catch(Exception e) { System.out.println("Class Not Found"); } try{ Connection con=DriverManager.getConnection ("jdbc:mysql://localhost:3306/abc","r oot","12345"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from student"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("********************************\n"); System.out.println("1. Insert Detail of Student.");

System.out.println("2. Delete Detail of Student."); System.out.println("3. Modify Detail of Student."); System.out.println("4. Display All Records In Table."); System.out.println("5. Exit From Program."); System.out.println("*********************************"); System.out.println("\n Enter Your Choice=>"); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: { System.out.println("\n Enter Roll Number=>"); int roll=Integer.parseInt(br.readLine()); System.out.println("\n Enter Name=>"); String name=br.readLine(); System.out.println("\n Enter Marks=>"); int marks=Integer.parseInt(br.readLine()); System.out.println("\n Enter Date of Birth(yyyy-mm-dd)=>"); String date=br.readLine(); st.executeUpdate("insert into student values("+roll+",'"+name+"',"+marks+",'" +date+"')"); break; } case 2: { System.out.println("\n Enter the Roll Number Of Student=>"); int del=Integer.parseInt(br.readLine()); st.executeUpdate("delete from student where roll="+del); break; } case 3: { System.out.println("**************************************"); System.out.println("\n1: Want to Update Name."); System.out.println("2: Want to Update Roll Number."); System.out.println("3: Want to Update Marks."); System.out.println("4: Want to Update DOB."); System.out.println("*********************************"); System.out.println("\nEnter Your Choice=>"); int ch1=Integer.parseInt(br.readLine()); switch(ch1) { case 1: { System.out.println("Enter the Roll Number of the student whose record you want t o modify."); int r1=Integer.parseInt(br.readLine()); System.out.println("Enter the name that want to update."); String new1=br.readLine(); st.executeUpdate("update student set name='"+new1 +"'where roll="+r1); System.out.println("\n Record Modified."); break; } case 2: { System.out.println("Enter the Date of Birth of the student whose record you wan t to modify."); String new1=br.readLine(); System.out.println("Enter New Roll Number."); int r1=Integer.parseInt(br.readLine());

st.executeUpdate("update student set roll="+r1+" where dob="+new1); System.out.println("\n Record Modified."); break; } case 3: { System.out.println("Enter the Roll Number of the student whose record you want to modify."); int r1=Integer.parseInt(br.readLine()); System.out.println("Enter the marks that you want to update." ); float m1=Float.parseFloat(br.readLine()); st.executeUpdate("update student set marks="+m1+"where roll="+r1); System.out.println("\n Record Modified."); break; } case 4: { System.out.println("Enter the Roll Number of the student whose record you want to modify."); int r1=Integer.parseInt(br.readLine()); System.out.println("Enter the Date of Birth that want to upda te."); String d1=br.readLine(); st.executeUpdate("update student set dob='"+d1+"'where roll=" +r1); System.out.println("\n Record Modified."); break; } } break; } case 4: { while(rs.next()) { System.out.println(" " + rs.getString(1) + " " + rs.getString(2)+ " " + rs.getSt ring(3)+ " " + rs.getString(4));} break; } case 5: break; } } catch(Exception e) {System.out.println("SQL ERROR");} } public static void main(String [] args) { new testmenu(); } }

OUTPUT

ASSIGNMENT NO: 4 Q4. Write JDBC application which uses Prepared Statement Interface for Inserting & Updating Records? PROGRAM CODE: import java.sql.*; import java.io.*; public class test { public test() { try{Class.forName("org.gjt.mm.mysql.Driver");} catch(Exception e){System.out.println("Class Not Found");} try { Connection con=DriverManager.getConnection ("jdbc:mysql://localhost:3306/abc","r oot","12345"); BufferedReader br=new BufferedReader(newInputStreamReader(System.in)); System.out.println("*******************************************\n"); System.out.println("1. Insert Detail of Student."); System.out.println("2. Modify Detail of Student."); System.out.println("3. Exit From Program."); System.out.println("*******************************************"); System.out.print("\n Enter Your Choice=>"); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: { PreparedStatement st=con.prepareStatement("insert into student values(?,?,?,?)") ; System.out.print("\n Enter Roll Number=>"); int r=Integer.parseInt(br.readLine()); System.out.print("\n Enter Name=>"); String n=br.readLine(); System.out.print("\n Enter Marks=>"); int m=Integer.parseInt(br.readLine()); System.out.print("\n Enter Date of Birth(yyyy-mm-dd)=>"); String d=br.readLine(); st.setInt(1,r); st.setString(2,n); st.setInt(3,m); st.setString(4,d);

st.executeUpdate(); System.out.println("\nRecord Added With Roll Number=" +r); break; } case 2: { System.out.println("*******************************************"); System.out.println("\n1: Want to Update Name."); System.out.println("2: Want to Update Roll Number."); System.out.println("3: Want to Update Marks."); System.out.println("4: Want to Update DOB."); System.out.println("*****************************************"); System.out.print("\nEnter Your Choice=>"); int ch1=Integer.parseInt(br.readLine()); switch(ch1) { case 1: { PreparedStatement st=con.prepareStatement("update student set Name=+? where Roll _no=+?"); System.out.print("Enter the Roll Number of the student whose record you want t o modify."); int r1=Integer.parseInt(br.readLine()); System.out.print("Enter the name that want to update. "); String new1=br.readLine(); st.setString(1,new1); st.setInt(2,r1); st.executeUpdate(); System.out.println("\n Record Modified with roll numb er=" +r1); break; } case 2: { PreparedStatement st=con.prepareStatement("update student set Roll_no=+? where DOB=+?"); System.out.print("Enter the Date of Birth of the student whose record you want to modify."); String new1=br.readLine(); System.out.print("Enter New Roll Number."); int r1=Integer.parseInt(br.readLine()); st.setInt(1,r1); st.setString(2,new1); st.executeUpdate(); System.out.println("\n Record Modified with DOB=" +new1); break; } case 3: { PreparedStatement st=con.prepareStatement("update student set Marks=+? where Ro ll_no=+?"); System.out.print("Enter the Roll Number of the student whose record you want to modify."); int r1=Integer.parseInt(br.readLine()); System.out.print("Enter the marks that you want to update."); float m1=Float.parseFloat(br.readLine()); st.setFloat(1,m1); st.setInt(2,r1); st.executeUpdate();

System.out.println("\n Record Modified with roll number=" +r1); break; } case 4: { PreparedStatement st=con.prepareStatement("update student set DOB=+? where Roll _no=+?"); System.out.print("Enter the Roll Number of the studen t whose record you want to modify."); int r1=Integer.parseInt(br.readLine()); System.out.print("Enter the Date of Birth that want to update."); String d1=br.readLine(); st.setString(1,d1); st.setInt(2,r1); st.executeUpdate(); System.out.println("\n Record Modified with Roll Number=" +r1); break; } } break; } case 3: System.exit(0); } } catch(Exception e) { System.out.println("SQL ERROR"+e); } } public static void main(String[] args) { new test(); } } OUTPUT:

ASSIGNMENT NO:6 Q6.Write JDBC application which will list all the tables available in the databa se. Similarity lists all the columns names for student table (Use database Metad ata Interface & Result set Metadata Interface? PROGRAM CODE: import java.io.*; import java.sql.*; public class metadata { public metadata() { try{ Class.forName("org.gjt.mm.mysql.Driver");

} catch(Exception e) { System.out.println("Class Not Found"); } try{ Connection con=DriverManager.getConnection ("jdbc:mysql://localhost:3306/abc","r oot","12345"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("**************************************\n"); System.out.println("1. List of all the Tables In Database."); System.out.println("2. Column Names For Student Table."); System.out.println("3. Exit From Program."); System.out.println("****************************************"); System.out.println("\n Enter Your Choice=>"); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: { DatabaseMetaData dm=con.getMetaData(); String[] tabTypes={"Table"}; System.out.println("Table Names"); System.out.println("----------------------------------"); ResultSet r1=dm.getTables(null,null,null, tabTypes); while(r1.next()) { System.out.println(r1.getString("table_name")); } break; } case 2: { String str="select * from student"+""; Statement st=con.createStatement(); ResultSet rs=st.executeQuery(str); ResultSetMetaData rm=rs.getMetaData(); rs.next(); System.out.println("Number of Attributes in the student table:"+rm.getColumnCou nt()); System.out.println("----------------------------------"); System.out.println("Attributes of the Student" +"Table"); System.out.println("----------------------------------"); System.out.println("Column Name\t Column Type"); System.out.println("----------------------------------"); for(int i=1;i<=rm.getColumnCount();i++) { System.out.println(rm.getColumnName(i)+"\t\t\t"+rm.ge tColumnTypeName(i)); } System.out.println("----------------------------------"); con.close(); st.close();

break; } case 3: { System.exit(0); } } } }catch(Exception e) {System.out.println("SQL ERROR"+e);} public static void main(String[] args) { new metadata(); } } OUTPUT:

ASSIGNMENT NO: 7 Q7.Design a application using Java Net Beans which will display the header infor mation of the web page trying to connect to a serve? . PROGRAM CODE: HTML Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h2>Welcome To Bharati Vidyapeeth Univ.</h2> <form action="loginpage" method="post" name="f1"> <b>USERNAME</b> <input type="text" name="uname"/> <br><br><br> <b>PASSWORD</b><input type="password" name="password"/> <br><br><br> <input type="submit" value="ENTER"/> </form> </body> </html> Servlet Code: import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class loginpage extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletRespons e response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println("<html>"); out.println("<head>");

out.println("<title>Servlet loginpage</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>Welcome Mr. " + request.getParameter("uname") + "<h2>"); out.print("<h2>Your Password Is=" +request.getParameter("password") + "<h2>"); out.println("<h4>Remote Host IS="+request.getRemoteHost()+"<h4>"); out.println("<h4>Remote Host Address IS=" + request.getRemoteAddr()+"<h4>"); out.println("<h4>Remote Host User IS=" + request.getRemoteUser()+"<h4>"); java.util.Enumeration e = request.getHeaderNames(); String s while(e.hasMoreElements()) { s=e.nextElement().toString(); out.println("<br> <font color=blue>Header Name= " +s); out.println("<br><font color=Blue>Details Are=<font color=re d>" +request.getHeader(s)); } out.println("</body>"); out.println("</html>"); } finally { out.close(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } } OUTPUT:

ASSIGNMENT NO: 8 Q8.Design a application using Java Net Beans which require user to enter his use rname & password. If username & password is correct then show welcome page other wise show an error? PROGRAM CODE: HTML Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body>

<form action="validate" method="post" name="f1"> <h1> <center>Welcome To Amplfy Mindware</center></h1> <h2 align=right> Enter UserName <input type="text" name="uname"/>< /h2> <h2 align=right> Enter Password h2> <h2 align=right><input type="submit" value="SUBMIT"/></h2> </form> </body> </html> Servlet Code: import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class validate extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String u=request.getParameter("uname"); String p=request.getParameter("pass"); String res; int status=0; if(u.equals("Amplify")&& p.equals("Mindware")) { res="Welcome To Amplify Mindware...."; status=1; } else { res="Incorrect UserName or Password Please Try Again later....."; status=0; } try { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet validate</title>"); out.println("</head>"); out.println("<body>"); if(status==0) out.println("<h1> <font color=red>" +res); else out.println("<h1> <font color=blue>" +res +"Mr. "+ request.getParame ter("uname")); out.println("</body>"); out.println("</html>"); } finally { out.close(); <input type="text" name="pass"/></

} } protected void doGet(HttpServletRequest request, HttpServletResponse respons e) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse respon se) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }

OUTPUT:

ASSIGNMENT NO: 5 Q5.Create procedure which will display marks for specified rollno & call init pr ocedure using JDBC application? PROGRAM CODE: import java.sql.*; public class second { Connection cn; PreparedStatement ps; ResultSet rs; public void callproce()throws Exception { CallableStatement cst=cn.prepareCall("{call p1(?,?)}"); cst.registerOutParameter(2,java.sql.Types.INTEGER); cst.setInt(1,101); cst.executeQuery(); int x=cst.getInt(2); System.out.println("Marks of Roll_no:101=" + x); } public void getconnection() { try { Class.forName("org.gjt.mm.mysql.Driver"); } catch(Exception e) { System.out.println("class not found"); } try { cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/abc","root" ,"12345"); String str ="select * from student where Roll_no=?"; ps=cn.prepareStatement(str); ps.setInt(1,101); rs=ps.executeQuery(); while(rs.next()) { System.out.println(""+rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+ " "+rs.getString(4)); } } catch(Exception e) { System.out.println("Db not found"); } } public static void main(String[] args) { second s1=new second(); s1.getconnection(); try{ s1.callproce(); }catch(Exception e)

{ System.out.print(" "+e); } } }

OUTPUT:

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->