This is for people who are new to java (advanced) n want to have a very simple picture of what JDBC,servlets n JSPs are like. Im also a beginer with no much knowledge in java.
If you are struck, first check for is “path” this can kill a beginers time n put them into loops thinking !!!…where is the error..!!
Here goes an example that starts with a form(xml) passes some information to a servlet which queries by calling a program that uses JDBC concepts(using bridge driver) and return
values to the servlet. The servlet inturn prints the values.
NOTE:This is a very simple programe just to illustrate the flow .
/* this is to be in a pakage …. JDBC part*/
package abc;
import java.sql.*;
import java.io.*;
public class atj
{
public String a(String name)
{
String eno=”not present”;
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con=DriverManager.getConnection(“jdbc:odbc:ora”,”scott”,”tiger”);
String q =”select empno from emp where ename= ?”;
PreparedStatement stm = con.prepareStatement(“select empno from emp where ename= ?”);
stm.setString(1,name);
ResultSet rs=stm.executeQuery();
rs.next();
eno = rs.getString(1);
}
catch(ClassNotFoundException cnfe)
{
System.out.println(cnfe);
}
catch(SQLException sql)
{
System.out.println(sql);
}
return eno;
}
}
/*Servlet*/
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import abc.atj;
public class STW extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String msg = req.getParameter(“username”);
atj b=new atj();
String eno= b.a(msg);
res.setContentType(“text/html”);
PrintWriter l = res.getWriter();
l.println( eno );
l.println( msg);
l.close();
}
}
/* jsp form*/
<HTML>
<BODY>
<FORM METHOD=Post ACTION=”simpleservlet/”>
What’s your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
/*XML file for servlet*/
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web-app>
<display-name>myExamples</display-name>
<servlet>
<servlet-name>mysimpleservlet</servlet-name>
<servlet-class>STW</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mysimpleservlet</servlet-name>
<url-pattern>/simpleservlet/</url-pattern>
</servlet-mapping>
</web-app>

No comments yet
Comments feed for this article