import java.sql.*; public class MySqlConnect { public static void main (String[] args) { Connection connection = null; try { String userName = "root"; String password = "bunty"; String url = "jdbc:mysql://localhost:3306/systdb"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); connection = DriverManager.getConnection (url, userName, password); System.out.println ("Database connection established"); } catch (Exception e) { System.err.println ("Connection to database server cannot be establish"); } finally { if (connection != null) { try { connection.close (); System.out.println ("Database connection terminated"); } catch (Exception e) { System.out.println ("These connection errors can be ignored."); } } } } }
Wednesday, October 24, 2007
How do you connect to a MySql Database using JDBC?
Sunday, October 21, 2007
Explain different inheritance mapping models in Hibernate.
1. Table per concrete class with unions
2. Table per class hierarchy
3. Table per subclass
Example:
We can take an example of three Java classes like Vehicle, which is an abstract class and two subclasses of Vehicle as Car and UtilityVan.
1. Table per concrete class with unions
In this scenario there will be 2 tables
Tables: Car, UtilityVan, here in this case all common attributes will be duplicated.
2. Table per class hierarchy
Single Table can be mapped to a class hierarchy
There will be only one table in database named 'Vehicle' which will represent all attributes required for all three classes.
Here it is be taken care of that discriminating columns to differentiate between Car and UtilityVan
3. Table per subclass
Simply there will be three tables representing Vehicle, Car and UtilityVan
Thursday, October 11, 2007
Explain Facade Design Pattern in Java
JDBC design is a good example of Façade pattern. A database design is complicated. JDBC is used to connect the database and manipulate data without exposing details to the clients.
A client is exposed to certain set of methods like just getting a single instance of database connection or closing it when not required.
Another possibility is designing security of a system with Façade pattern. Clients' authorization to access information may be classified. General users may be allowed to access general information, special guests may be allowed to access more information,administrators and executives may be allowed to access the most important information. These subsystems may be generalized by oneinterface. The identified users may be directed to the related
subsystems.
interface PublicMethod{
public void accessPublicMethod();
}
interface SpecialMethod extends PublicMethod{
public void accessSpecialMethod();
}
interface PrivateMethod extends PublicMethod {
public void accessPrivateInfo();
}
class PublicInfo implements PublicMethod{
public void accessPublicMethod() {
//...
}
//...
}
class SpecialInfo extends PublicInfo implements SpecialMethod {
public void accessSpecialMethod() {
//...
}
}
class PrivateInfo extends SpecialInfo implements PrivateMethod {
public void accessPrivateMethod () {
// ...
}
//...
}
The above code example illustrates that the whole system is not exposed to the clients. It depends on the user classification.
When a person is exposed to special information, that person is allowed to access public information also. When a person is exposed to private information, that person is allowed to access public and special information as well.
Monday, October 8, 2007
Give an example where upcasting takes advantage of polymorphism.
class Parent { }
class Child extends Parent { }
Child can be referred as an object of type Parent or Child.
Upcasting works like this:
Child aChildObj = new Child();
Parent aParentObj = (Parent)aChildObj;
As aChildObj can be referred to by the class names Child and
Parent,you can say that this upcasting and type extension is the
implementation of polymorphism in Java.
Wednesday, October 3, 2007
Java Tools
To increase the efficiency and productivity of Java Developers,they all need a set of tools which equip them to design,code and test their work in consolidated ways.Here I tried to summarize various essentials or must have Java tools for all those who are fascinated with this language.
Eclipse.org - It is an open source development platform for Java based applications, as well as other languages.Hibernate.org - An open source persistent classes development tool; due to licensing, it can be included in to other open source projects.
JUnit.org - An open source testing platform for Java that works towards understanding your intentions.
PMD - A Sourceforge project that scans Java apps and looks for bad code.SpringFramework.org - A Java/JEE application framework with a large support system and training sessions all over the world.
Idevelopment Java Examples - A large collection of examples of Java programming along with documentation.
JAD - A non-commercial use Java decompiler.Java.net - A site for Java programmers and enthusiasts to gather, chat, and collaborate on projects together.
Java PathFinder - A verifier for executable Java bytecode hosted by Sourceforge.Java-Source.net - A directory to just about every open source Java project you could dream of.
Java-Tips.org - A huge collection of hundreds of tips related to Java programming.
Java2s.com - Tons of examples of Java code.
JavaBlackBelt.com - Take online courses, followed up by tests that let you climb the colored belt tree (like you do in martial arts) until you become a Java Master.
Javalobby.org - Articles, forums, and more to learn all sorts of tricks for working with Java.
JavaPractices.com - Tons of examples of different functions with snippets of source code provided.
JavaRanch.com - A user community for Java programmers with a ranch-flavor. Forums, tools, code snippets and more.
JDocs.com - A huge resource of libraries, packages, and classes.
JExamples.com - A large collection of examples of Java from various open source projects that you can study.
JGoodies.com - Tools focusing on the look of Java in an attempt to make it look more elegant.
JODE - Free Sourceforge project that contains a decompiler and optimizer for Java code.
PHP/Java Bridge - A Sourceforge project that aids in bridging the gap between PHP and Java to make them work together.
The Grinder - Another Sourceforge project that is a Java load testing framework.
TheServerSide.com - A Java developer community with a focus on enterprise-class projects.
Emma - A Sourceforge project that provides you with a free code coverage tool.
Groovy - Created to run on the Java Virtual Machine but adds in features inspired by Python, Ruby on Rails and Smalltalk.