Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!

Glenny Dsilva

Ranch Hand
+ Follow
since May 09, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Glenny Dsilva

How do we show assertions on the console.

14 years ago
I am creating a program to store Categories and SubCategories

like main category is having subcategories and each subcategory can have subcategories like a heirarchy.

Main Category1
| |
Sub Category1 Sub Category 2
|
Sub Category 1a

I want to store this heirarchy structure in a collection can anybody explain which type of collection to use and how will i retrieve the collection.



15 years ago
I am not clear with call back methods. Can anybody explain with a simple example. Are methods defined in the interface are called callback methods?.

Callback methods are used in event handling can anyone give me an example of such type.

Thanks
15 years ago
Hi passed the scjp with 62%

Thank u guys for your endless support ..
16 years ago
I am not clear with what does Class.forName().

I read that Class.forName is used to dynamically load a class.
For e.g. the jdbc driver that we use is loaded when u

write

Class.forName("com.mysql.jdbc.Driver");

but one can also do it with extracting the jar file and importing the package


Using the code below i have connected to the database so what is the benefit of using
Class.forName().


package foo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import com.mysql.jdbc.Driver;

public class l
{
public static void main(String args[])
{
try
{
Driver.class.newInstance();

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/effectiveinfo","root","rkn09876");
Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("Select * From EmpMst Limit 10");

while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.close();
System.out.println(con.getCatalog());
}catch(Exception e)
{
System.out.println("Exception" + e.toString());
}
}
}
16 years ago
Thanks a lot its working fine now

One more doubt I wanna clear is that on mysql server i get the process list

as given below

Id UserHostdbCommand Time

142root localhost:1531javatestSleep10

I cannot understand why do i get the above process list i.e. in Sleep mode

Is it okay or what is the procedure to kill the process or close the connection

I have closed the connection in the class file using con.close()

I have also added the abandoned db connection in the xml code i.e. below

<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
I have tried connection pooling according to the documentation given
in tomcat 5.0.18, its working fine.

But in my project I need to connect to 2 databases from one context.
According to a given parameter can i connect to a particular
database using a connection pool.
Connection

I am maintaining a project on servlet database is mysql.

I have a connection instance variable con.

I am using the same connection in many methods. (The code is below just typed one of the methods for explanation)

Is this the correct way to use the connection object in all the methods

or should i have seperate connections in all the methods ?

I have observed that the page does not get properly loaded if i use the instance variable con in all the methods.


But if i open and close connections in all the methods there will be lot of overhead so can anyone suggest a better way...

public class DCRDoctorData extends HttpServlet
{

Connection con;

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
con = ConnectionHandler.getConnection(dbname);

add1();

}

private void add1()
{
Statement stmt = con.createStatement(); // using the instance variable

}
}
The connection object used is an instance variable. so i synchronized all the methods in the file where the connection object is used.

But sometimes the page is not getting downloaded fully.

Is it because i have synchronized all the methods in the file ?.

Should i just synchronize the Connection instance variable.
1. can i put this code inside a method to synchronize a particular block
2. does synchoronize(this) block works only on instance variables.

for e.g.


here stmt and con are local variables


private void method1()
{

synchronize(this)
{
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO table1 values
(1,2);
stmt.close();
con.close();
}
}
I am interested in giving the scjp paper 1 and anyone give me the syllabus for paper 1.
1. how does one know which jdbc driver is being used.
i.e. type 1 type 2,type 3 or type 4

2. And the version of jdbc.
Is there any way to count the number of rows in the result set.
Still unclear Pls. explain these points.

1. Even after removing the abstract method getDescription() in CondimentDecorator Class I get the same output so
whats the point in having that method i.e. getDescription() in CondimentDecorator;

2. The output for
System.out.println(beverage2.getDescription() + beverage2.cost());

is Dark Roast,mocha,mocha 2.39
Pls. explain how mocha string is appended twice.


The complete code is below



// The Beverage class

package test;

public abstract class Beverage
{
String description = "Unknown Beverage";

public String getDescription()
{
return description;
}

public abstract double cost();
}

// The CondimentDecorator Class

package test;

public abstract class CondimentDecorator extends Beverage
{
public abstract String getDescription(); // why this method

}

// The Dark Roast Class

package test;

public class DarkRoast extends Beverage
{

public DarkRoast()
{
description = "Dark Roast";
}

public double cost()
{
return 1.99;
}

}

// The Mocha Class

package test;

public class Mocha extends CondimentDecorator
{
Beverage beverage;

Mocha(Beverage beverage)
{
this.beverage = beverage;
}

public String getDescription()
{
return beverage.getDescription() + ",mocha";
}

public double cost()
{
return .20 + beverage.cost();
}
}

// The main Class

package test;

public class StarBuzzCoffee
{
public static void main(String args[])
{
Beverage beverage2 = new DarkRoast();

System.out.println(beverage2.getDescription() + "$" + beverage2.cost());


beverage2 = new Mocha(beverage2);

beverage2 = new Mocha(beverage2);

System.out.println(beverage2.getDescription() + beverage2.cost());

}
}
I am reading Head First Book on Design patterns

Found the decorator pattern a bit confusing . The code for decorator pattern in the book is below

Beverage code

public abstract class Beverage
{
String description = "Unknown Beverage";

public String getDescription()
{
return description;
}

public abstract double cost();
}


Condiments code

public abstract class CondimentDecorator extends Beverage
{
public abstract String getDescription();
}


public class HouseBlend extends Beverage
{
public HouseBlend()
{
description = "House Blend Coffee";
}
public double cost()
{
return .89;
}

}

// and finally ...

public class Mocha extends CondimentsDecorator
{
Beverage beverage;

public Mocha(Beverage beverage)
{
this.beverage = beverage;
}

public String getDescription()
{
return beverage.getDescription() + ", Mocha";
}

public double cost()
{
return .20 + beverage.cost()
}
}

public static void main(String args[])
{
Beverage beverage = new HouseBlend(); // here we get HouseBlend not U nknown beverage.



}


}

1. I would like to know why the getDescription() method in Condiment Decorator is marked abstract.

2. and does the instance variable gets overridden when i instantiate beverage class. (I was under the impression that instance variable are not overridden then what happens here).



Pls anyone clear my doubt..