• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet-jdbc

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this to JDBC, you may receive additional help there
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this thread:
http://www.javaranch.com/ubb/Forum7/HTML/001304.html
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic