JDBC Basics - Part II

DSN

In the first article we have discussed about Type 1 JDBC driver. In this article we will discuss about the practical configurations essential for the successful execution of the JDBC program. According to the previous article "sun.jdbc.odbc.JdbcOdbcDriver" is the driver class name of the JDBC-ODBC driver. This driver is included in the JDK run time environment.Therefore we need not specify any JAR file in the classpath. To load this driver we need to call Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"). To test JDBC-ODBC Bridge we need to create a DSN (Data Source Name) for a flat file, and then use this DSN in the JDBC connection URL.

It is very common that the DSN you entered in the connection URL is not defined or defined incorrectly. In this case, the JDBC-ODBC Bridge driver will raise an exception on the DriverManager.getConnection() method. Here is an example showing this:

This is a login page . The user needs to enter username and password to go to the next page. Here HTML tags are used like <form></form> tag where two input text fields and a submit button are inserted. When the user enters its username and password and clicks on the submit button he/she is directed to the next JSP page named “next.jsp” using get method.

In this page the username and password entered in the previous page is retrieved using the appropriate method:Var=request.getParameter(name_of_input_field);
Then the sun.jdbc.odbc.JdbcOdbcDriver is loaded and connected with this program through the DSN “harsh” set up. Then the preparedStatement is used to retrieve the username and password from the table in the database. preparedStatement is used as we pass dynamic values in the query at runtime. If there is a user then he/she is redirected to another JSP page “success.jsp” else to a error page “error.jsp”.

In this page the actual insertion of the details of the record entered in the previous page in the backend database is done. First the details are retrieved from the previous page. Then the same is inserted using prepareStatement as dynamic values are entered at runtime which changes as per the user.

In this page the actual deletion of the record entered in the previous page in the backend database is done. First the details are retrieved from the previous page. Then the corresponding record is deleted using prepareStatement as dynamic values are entered at runtime which changes as per the user.