Hi, This is the code for connection to a database using the ODBC Driver for Java.
The code explanation is as follows:- public Connection getConnection() { *this defines the start of the method i.e. the connection code is written in this method.* Connection con = null; try { *here you declare the Connection object . INitially it is set to null. Next the Try block is defined.* Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); *This line loads the Driver i.e. the Java ODBC driver in this case.* con = DriverManager.getConnection("jdbc:odbc:TSMS","TSMS","TSMS"); *Using the loaded driver, next the connection is made using the getConnection method of the DriverManager Class. Credentials that are to be supplied to this method are DSN Name , user name and password. i.e. ( DSN Name, username ,password)* System.out.println("Con : "+con); *Here we just print the connection details to the system console* return con; *Here we return the Connection object to the program where this user-defined method getConnection was called.* } catch(Exception e) { *This defines the Exception handling. If any error occurs, then this exception handler thorws the details of this error to the system console using the statement below.* System.out.println("Exception "); e.printStackTrace(); *The above line prints the entire stack of the flow in which Error was generatred by Java. It gives details as to the reason behind the error* } return null; *Close the function and return null in such a case* *I suggest you place the return null statement in the Exception block rather than at the end of execution. The code seems fine for now. But it is better you keep it inside to make it modular.* Hope this helps, Jitesh Dundas On 8/15/09, jatti <jatin_john1...@yahoo.com> wrote: > > > public Connection getConnection() > { > Connection con = null; > try > { > Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); > con = > DriverManager.getConnection("jdbc:odbc:TSMS","TSMS","TSMS"); > System.out.println("Con : "+con); > return con; > } > catch(Exception e) > { > System.out.println("Exception "); > e.printStackTrace(); > } > return null; > > > > -- Thanks & Regards, Jitesh Dundas Research Associate, DIL Lab, IIT-Bombay(www.dil.iitb.ac.in), Scientist, Edencore Technologies(www.edencore.net) Phone:- +91-9860925706 http://jiteshbdundas.blogspot.com "No idea is stupid,either its too good to be true, or its way ahead of its future"- GEORGE BERNARD SHAW. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Java EE (J2EE) Programming with Passion!" group. To post to this group, send email to java-ee-j2ee-programming-with-passion@googlegroups.com To unsubscribe from this group, send email to java-ee-j2ee-programming-with-passion-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en -~----------~----~----~----~------~----~------~--~---