Question 2
(b) Explain the use of CallableStatement and PreparedStatement with example./* create or replace procedure studentDataPro(enrollNumber number,name varchar2,branch varchar2,sem number,cpi number) is begin insert into studentData values(enrollNumber,name,branch,sem,cpi); end; */ import java.sql.*; public class CallableStatementPreparedStatement { public static void main(String s[]) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:XE","scott","tiger"); CallableStatement cs= con.prepareCall ("{call studentDataPro (?,?,?,?,?)}"); cs.setDouble(1, 6066); cs.setString(2, "Jaydip Panchal"); cs.setString(3, "Information Technology"); cs.setInt(4, 7); cs.setDouble(5, 7.01); cs.execute(); System.out.println("1 Student Record Inserted!!!"); String query="insert into studentData values (?,?,?,?,?)"; PreparedStatement ps=con.prepareStatement (query); ; ps.setDouble(1, 6050); ps.setString(2, "Arpan Patel"); ps.setString(3, "Information Technology"); ps.setInt(4, 7); ps.setDouble(5, 6.79); int i=ps.executeUpdate(); System.out.println(i+" Student Record Inserted!!!"); ps.setDouble(1, 6061); ps.setString(2, "Pratik Patel"); ps.setString(3, "Information Technology"); ps.setInt(4, 7); ps.setDouble(5, 7.47); i=ps.executeUpdate(); System.out.println("Query executed for the second time count : "+i); con.close(); } }
Question 3
(b) Consider Bank table with attributes AccountNo, CustomerName, Balance, Phone and Address. Write a database application which allows insertion, updation and deletion of records in Bank table. Print values of all customers whose balance is greater than 20,000.import java.sql.*; import java.util.*; import java.io.*; public class CallableStatementPreparedStatement { public static void main(String args[]) throws Exception,IOException { int n=0,ch=0; Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:XE","scott","tiger"); Scanner input = new Scanner(System.in); System.out.println("Enter Choice : "); System.out.println("1. Insert Data"); System.out.println("2. Delete Data"); System.out.println("3. Update Data"); if(input.hasNextInt()) n = input.nextInt(); switch(n) { case 1 : String query="insert into bankTable values (?,?,?,?,?)"; System.out.print("Enter Account Number : "); if(input.hasNextInt()){ ps.setInt(1,input.nextInt());} System.out.print("Enter Customer Name : "); ps.setString(2,input.next()); System.out.print("Enter Balance : "); ps.setString(3,input.next()); System.out.print("Enter Phone : "); ps.setString(4,input.next()); System.out.print("Enter Address : "); ps.setString(5,input.next()); int i=ps.executeUpdate(); System.out.println(i+" Account Record Inserted!!!"); break; case 2 : String query="delete * from bankTable where accountNumber = ?"; System.out.print("Enter Account Number : "); if(input.hasNextInt()){ ps.setInt(1,input.nextInt());} int i=ps.executeUpdate(); System.out.println(i+" Account Record Inserted!!!"); break; case 3 : System.out.println("What u want to update?"); System.out.println("1. Name"); System.out.println("2. Phone"); System.out.println("3. Address"); if(input.hasNextInt()) ch = input.nextInt(); switch(ch) { case 1 : System.out.print("Enter New Name : "); String str = input.next(); String query="update backTable set customerName = "+cname+" where accountNumber = ?"; System.out.print("Enter Account Number : "); if(input.hasNextInt()){ ps.setInt(1,input.nextInt());} int i=ps.executeUpdate(); System.out.println(i+" Account Record Updated!!!"); break; case 2: System.out.print("Enter New Phone : "); String str = input.next(); String query="update backTable set phone = "+cname+" where accountNumber = ?"; System.out.print("Enter Account Number : "); if(input.hasNextInt()){ ps.setInt(1,input.nextInt());} int i=ps.executeUpdate(); System.out.println(i+" Account Record Updated!!!"); break; case 3: System.out.print("Enter New Address : "); String str = input.next(); String query="update backTable set address = "+cname+" where accountNumber = ?"; System.out.print("Enter Account Number : "); if(input.hasNextInt()){ ps.setInt(1,input.nextInt());} int i=ps.executeUpdate(); System.out.println(i+" Account Record Updated!!!"); break; default: System.out.println("Wrong Choice"); break; } break; default : System.out.println("Wrong Choice"); break; } Statement st=con.createStatement(); String query = "select customerName from bankTable where balance > 20000"; ResultSet rs = st.executeQuery(query ); if(rs.next()) { System.out.println("Customer Name : "+rs.getString(1)); } con.close(); } }
(a) Write an RMI application where client sends empno and server returns corresponding salary by querying database.
// ReceiveMessageInterface.java import java.rmi.*; public interface ReceiveMessageInterface extends Remote { void receiveMessage(String x) throws RemoteException; }
// RmiServer.java import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; import java.net.*; import java.sql.*; import java.util.*; import java.io.*; public class RmiServer extends java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface throws Exception,IOException { String address; Registry registry; public void receiveMessage(String x) throws RemoteException { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:XE","scott","tiger"); Statement st=con.createStatement(); String query = "select salary from employeeTable where empId = "+x; ResultSet rs = st.executeQuery(query ); if(rs.next()) { System.out.println("Employee's Salary whose Emp Id = "+x+" is : "+rs.getDouble(1)); } con.close(); } public RmiServer() throws RemoteException { try { address = (InetAddress.getLocalHost()).toString(); } catch(Exception e) { System.out.println("Server can't get INET address."); } int port=3232; System.out.println("Address = " + address + ",Port = " + port); try { registry = LocateRegistry.createRegistry(port); registry.rebind("rmiServer", this); } catch(RemoteException e) { System.out.println("Remote Exception"+ e); } } static public void main(String args[]) { try { RmiServer server = new RmiServer(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
// RmiClient.java import java.rmi.*; import java.rmi.registry.*; import java.net.*; public class RmiClient { static public void main(String args[]) { ReceiveMessageInterface rmiServer; Registry registry; String serverAddress=args[0]; String serverPort=args[1]; String empNo=args[2]; System.out.println("Sending " + empNo + " to " + serverAddress + " : " + serverPort); try { registry=LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue()); rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer")); rmiServer.receiveMessage(empNo); } catch(RemoteException e) { e.printStackTrace(); } catch(NotBoundException e) { System.err.println(e); } } }
(b) Write a program to demonstrate use of Internationalization for various language supports.
// InternationalizationLanguageSupport.java import java.util.*; import java.io.*; public class InternationalizationLanguageSupport { public static void main(String s[]) throws Exception { String country,language; Locale locale; ResourceBundle rb; int n=0; Scanner input = new Scanner(System.in); System.out.println("Select Your Language : "); System.out.println("1. English"); System.out.println("2. France"); System.out.println("3. Spanish"); if(input.hasNextInt()) n = input.nextInt(); switch(n) { case 2 : language = "fr"; country = "FR"; break; case 3 : language = "es"; country = "ES"; break; default: language = "en"; country = "US"; break; } locale = new Locale(language, country); rb = ResourceBundle.getBundle("InternationalizationLanguageSupports", locale); System.out.println(rb.getString("localeInfo") + " ( " + locale.getDisplayLanguage() + "," + locale.getDisplayCountry() + ").\n"); System.out.println(rb.getString("welcomeUser")); System.out.println(rb.getString("exitUser")); } }
// InternationalizationLanguageSupports.properties localeInfo = The text displayed is specific to locale welcomeUser = Hello, how are you? Welcome To Help2Engg!! Gujarat's First Engineering Social Network!! exitUser = Thanks to visit Help2Engg.
// InternationalizationLanguageSupports_es_ES.properties localeInfo = El texto mostrado es específico al lugar welcomeUser = ¿Hola, ¿cómo estás? Bienvenido a Help2Engg! Ingeniería de Redes Primera Gujarat Social! exitUser = Gracias por visitar Help2Engg.
// InternationalizationLanguageSupports_fr_FR.properties localeInfo = Le texte affiché est spécifique à la scène welcomeUser = Bonjour, comment allez-vous? Bienvenue Help2Engg! Gujarat en technologie des réseaux sociaux d'abord! exitUser = Merci de visiter Help2Engg.
Question 4
(b) Write a client program to send any string from its standard input to the server program. The server program reads the string, finds number of characters and digits and sends it back to client program. Use connection-oriented or connection-less communication.// Server.java import java.io.*; import java.net.*; class Server { public static void main(String args[]) throws Exception { ServerSocket ss = new ServerSocket(888); Socket s = ss.accept(); System.out.println("Connection established"); PrintStream ps = new PrintStream(s.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); while(true) { String str,str1; while((str = br.readLine()) != null) { System.out.println("Counting Characters and Digits from String..."); int countChar = 0,countNumber = 0; for(int i=0;i<str.length();i++) { if( (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) countChar++; else if(str.charAt(i) >= '0' && str.charAt(i) <= '9') countNumber++; } str1 = "Total Number Of Characters = " + countChar; str1 += "Total Number Of Digits = " + countNumber; ps.println(str1); } ps.close(); br.close(); kb.close(); ss.close(); s.close(); System.exit(0); } } }
// Client.java import java.io.*; import java.net.*; class Client { public static void main(String args[]) throws Exception { Socket s = new Socket("localhost", 888); int stop = 1; DataOutputStream dos = new DataOutputStream(s.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); String str="",str1=""; do { str = kb.readLine(); dos.writeBytes(str+"\n"); str1 = br.readLine(); System.out.println(str1); }while(stop != 1); dos.close(); br.close(); kb.close(); s.close(); } }
(a) Explain Socket, ServerSocket, InetAddress classes. Write a java program to find an IP address of the machine on which the program runs.
import java.util.*; import java.lang.*; import java.net.*; public class getIP { public static void main(String args[]) { try { InetAddress IPAddressOfMachine = InetAddress.getLocalHost(); System.out.println("IP of Your system is : " + IPAddressOfMachine.getHostAddress()); } catch (Exception e) { System.out.println("Exception : " + e.getMessage()); } } }
Question 5
(a) Explain object serialization in detail with example.import java.io.*; class sampleClass implements Serializable { String str; int no; public sampleClass(String str, int no) { this.str = str; this.no = no; } public String toString() { return "String is : " + str + "; Number is : " + no; } } public class objectSerialization { public static void main(String args[]) { try { sampleClass object = new sampleClass("Help2Engg | GTU Paper Solution", 420); System.out.println("object of sampleClass : " + object); FileOutputStream fos = new FileOutputStream("objectSerialixationFile.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object); oos.flush(); oos.close(); } catch(Exception e) { System.out.println("Exception : " + e); System.exit(0); } } }
No comments:
Post a Comment