• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet-jdbc

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me to write a jdbc connection from a servlet.
I just want to display 3 column's from the database on to the web page , I recently migrated from ASP to servlets..so still hanging with the old microsoft tech.
:-)
advance thanks
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the tutorial JDBC Basics from Sun. Once you have the basics of JDBC, intergrating that with a servlet is eay, the only other thing that has to be designed is concurrensy. This is accomplished with connection pooling. Do a search on the forums, it's been discussed in some detail throughout.
Hope this helps
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to JDBC, you may receive additional help there
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this thread:
http://www.javaranch.com/ubb/Forum7/HTML/001304.html
 
Satish Kasala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranch plower,
Try this out. Change the name of the dsn and the sql statement.
/**
* @(#) DBServlet.java
*
* (C) Copyright Satish Kumar Kasala
*
*/

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class DBServlet extends HttpServlet{
Connection con;
public void init(ServletConfig sc) throws ServletException
{
super.init(sc);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
String TestValue=req.getParameter("TestValue");
out.println("<html>");
out.println("<head><title>Database Servlet</title></head>");
out.println("<body>");
//Establish connection to the Database (JDBC-ODBC Bridge)
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc racledsn","scott","tiger");
}
catch(Exception e){
out.println(e.toString());
}
//Execute SQL Statement
try{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from tab");
out.println("<h3>Table Names : </h3>");
while(rs.next()){
String TableName = rs.getString(1);
out.println("
"+TableName+"
");
}
}
catch(Exception e){
out.println(e.toString());
}
//Calling a Stored Procedure
try{
CallableStatement cs = con.prepareCall("{call TestProcedure(?)}");
cs.setString(1,TestValue);
cs.execute();
}
catch(Exception e){
out.println(e.toString());
}
out.println("</body></html>");
try{
con.close();
}
catch(Exception e){
out.println(e.toString());
}
}
}

Satish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic