jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes servlet-jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "servlet-jdbc" Watch "servlet-jdbc" New topic
Author

servlet-jdbc

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 13, 2000
Posts: 3340
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

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I'm going to move this to JDBC, you may receive additional help there
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Take a look at this thread:
http://www.javaranch.com/ubb/Forum7/HTML/001304.html


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Satish Kasala
Greenhorn

Joined: Dec 19, 2000
Posts: 16
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
 
wood burning stoves
 
subject: servlet-jdbc