INTERNAL ASSESSMENT TEST II Date : 20-09-2016 Max Marks: 50 Subject & Code: JAVA & J2EE (10IS752) Section : A & B Name of faculty: Sreenath M V Time : 8.30-10.00 AM Note: Answer all five questions 1) a) Explain any two event listener interfaces with its function or methods. 5 The KeyListener Interface This interface defines three methods. The keypressed () and keyreleased () methods are invoked when a key is pressed and released, respectively. The keytyped () method is invoked when a character has been entered. Ex: If a user presses and releases the a key, three events are generated in sequence: key pressed, typed and released. If a user presses and releases the HOME key, two key events are generated in sequence: key pressed and released. The general forms of the three methods are: void keypressed (KeyEvent ke) void keyreleased (KeyEvent ke) void keytyped (KeyEvent ke) The MouseListener Interface Five methods: void mouseclicked (MouseEvent me) (Invoked when the mouse is pressed and released at the same point) void mouseentered (MouseEvent me) (Invoked when the mouse enters a component) void mouseexited (MouseEvent me) (Invoked when the mouse exits a component)
void mousepressed (MouseEvent me) (Invoked when the mouse is pressed) void mousereleased (MouseEvent me) (Invoked when the mouse is released) b) What is adapter class? Demonstrate with an example. 5 Each adapter class implements the corresponding interface with a series of donothing methods. For example, MouseListener declares these five methods: public abstract void mouseclicked(mouseevent evt) public abstract void mousepressed(mouseevent evt) public abstract void mousereleased(mouseevent evt) public abstract void mouseentered(mouseevent evt) public abstract void mouseexited(mouseevent evt) Therefore, MouseAdapter looks like this: package java.awt.event; import java.awt.*; import java.awt.event.*; public class MouseAdapter implements MouseListener { public void mouseclicked(mouseevent evt) { public void mousepressed(mouseevent evt) {
public void mousereleased(mouseevent evt) { public void mouseentered(mouseevent evt) { public void mouseexited(mouseevent evt) { By subclassing MouseAdapter rather than implementing MouseListener directly, you avoid having to write the methods you don't actually need. You only override those that you plan to actually implement. 2) What is multithreading? Write a program to create multiple threads in JAVA. 10 class MyThread implements Runnable { String tname; Thread t; MyThread (String threadname) { tname = threadname; t = new Thread (this, tname); t.start(); public void run() { try { System.out.println("Thread: " + tname ); Thread.sleep(2000); catch (InterruptedException e ) {
System.out.println("Exception: Thread " + tname + " interrupted"); System.out.println("Terminating thread: " + tname ); class Demo { public static void main (String args []) { new MyThread ("1"); new MyThread ("2"); new MyThread ("3"); new MyThread ("4"); try { Thread.sleep (10000); catch (InterruptedException e) { System.out.println( "Exception: Thread main interrupted."); System.out.println( "Terminating thread: main thread.");
3) What are applets? Explain different stages in the life cycle of an applet with example. 10 An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. // Sample Java Applet; an Applet skeleton import java.applet.applet; import java.awt.graphics; public class Simple extends Applet { StringBuffer buffer; public void init() { buffer = new StringBuffer(); additem("initializing... "); public void start() { additem("starting... "); public void stop() { additem("stopping... "); /* A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. */ public void destroy() { additem("preparing for unloading..."); private void additem(string newword) { System.out.println(newWord); buffer.append(newword); repaint(); public void paint(graphics g) { //Draw a Rectangle around the applet's display area. g.drawrect(0, 0, getwidth() - 1, getheight() - 1);
//Draw the current string inside the rectangle. g.drawstring(buffer.tostring(), 5, 15); Invoking an Applet: An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser. The <applet> tag is the basis for embedding an applet in an HTML file. Below is an example that invokes the "Hello, World" applet: <html> <title>the Hello, World Applet</title> <hr> <applet code="helloworldapplet.class" width="320" height="120"> If your browser was Java-enabled, a "Hello, World" message would appear here. </applet> <hr> </html> The code attribute of the <applet> tag is required. It specifies the Applet class to run. Width and height are also required to specify the initial size of the panel in which an applet runs. The applet directive must be closed with a </applet> tag. 4) a) With syntax explain three execute methods. Once you've created a Statement object, you can then use it to execute an SQL statement with one of its three execute methods. 5 boolean execute (String SQL): Returns a boolean value of true if a ResultSet object can be retrieved; otherwise, it returns false. Use this method to execute SQL DDL statements or when you need to use truly dynamic SQL. int executeupdate (String SQL): Returns the number of rows affected by the execution of the SQL statement. Use this method to execute SQL statements for which you expect to get a number of rows affected - for example, an INSERT, UPDATE, or DELETE statement. ResultSet executequery (String SQL): Returns a ResultSet object. Use this method when you expect to get a result set, as you would with a SELECT statement.
b) Explain i) Statement ii) Prepared Statement 5 Statement Objects Overview Once a connection is obtained we can interact with the database. The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enable you to send SQL or PL/SQL commands and receive data from your database. They also define methods that help bridge data type differences between Java and SQL data types used in a database. Statement-Used for the general-purpose access to your database. Useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters PreparedStatement-Use the when you plan to use the SQL statements many times. The PreparedStatement interface accepts input parameters at runtime. Creating Statement Objects The Statement Objects Creating Statement Object Before you can use a Statement object to execute a SQL statement, you need to create one using the Connection object's createstatement( ) method, as in the following example Statement stmt = null; try { stmt = conn.createstatement( );... catch (SQLException e) {... finally {... The PreparedStatement Objects The PreparedStatement interface extends the Statement interface, which gives you added functionality with a couple of advantages over a generic Statement object. This statement gives you the flexibility of supplying arguments dynamically. Creating PreparedStatement Object PreparedStatement pstmt = null; try { String SQL = "Update Employees SET age =? WHERE id =?"; pstmt = conn.preparestatement(sql);... catch (SQLException e) {... finally {... All parameters in JDBC are represented by the? symbol, which is known as
the parameter marker. You must supply values for every parameter before executing the SQL statement. 5) a) Explain J2EE multitier architecture with a neat diagram. 5
b) Explain four types of JDBC Drivers. 5 JDBC Driver types JDBC drivers are divided into four types: Type 1: JDBC-ODBC Bridge driver (Bridge) Type 2: Native-API/partly Java driver (Native) Type 3: All Java/Net-protocol driver (Middleware) Type 4: All Java/Native-protocol driver (Pure) Type 1 - JDBC-ODBC Bridge driver A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. Type 2 - Native-API/partly Java driver A native-api partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine. Type 3 - All Java/Net-protocol driver A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases.
Type 4 - Native-protocol/all-Java driver A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary, the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress. * * * * * *