my dog learned polymorphism*
The moose likes Servlets and the fly likes JDBC through Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JDBC through Servlet" Watch "JDBC through Servlet" New topic
Author

JDBC through Servlet

Archana Moorthy
Greenhorn

Joined: Aug 23, 2004
Posts: 6
Hai Friends,
While designing a Login servlet I could not get connection with the ODBC datasource. My code is as follows:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Simple extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");

String title = "Welcome to MailServer";
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
out.println("Class done<br>");

Connection conn = DriverManager.getConnection("jdbc dbc:mail");
out.println("Connection done<br>");

Statement st = conn.createStatement();
st.executeUpdate("insert into User(loginname,password) values('"+request.getParameter("lname")+"','"+request.getParameter("pass")+"')");

st.close();
conn.close();
}
catch(Exception e)
{
out.println("Exception Has Been Caught <br>" + e);
}
out.println("</body>");
out.println("</html>");
}
}

The error which i get is:

Exception Has Been Caught
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I tried the same connection in a simple java program and there is no problem with it. Is there some thing to be added while using in Servlet. I am using TOMCAT server.

Archana S
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
Check you have created a DSN with the name "mail".Cross check your data source name in your ODBC Datasource in your OS

Chandrasekhar S
SCJP


"Luck is when opportunity meets preparation"
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 631
Make sure you have driver in class path of Tomcat.

usually it is classes111.zip.put this file in tomcat_home/comman/lib

Cheers
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

As another suggestion, I highly highly recommend that you move the JDBC code from the Servlet an place it in a plain old Java object that the Servlet could call.

By having JDBC code in your Servlet, you are tightly coupling JDBC with your Servlet. Unless this is a class homework or one-off quick test. You would not want the code you currently have in production.

And what Praful said.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Archana Moorthy
Greenhorn

Joined: Aug 23, 2004
Posts: 6
Hi Chandrasekhar S I dont get the meaning of "Cross check your data source name in your ODBC Datasource in your OS ". Can you please explain me abt it.

Hi Praful Thakare i have placed the classes111.zip file. And still it is not working. Should i unzup the contents in it.

Hi Mark Spritzler Cnd you just tell how to call a java object from a servlet. Please. I think this will work as my ordinary java file of connecting the database works correctly.

Regards,
Archana S
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 631
Hi,
Write Java File With Following Code.(This is just for testing puropose..you much use different approch for production).

No Need to unzip that zip file,just check the logs after you execute following program.and lets us know the sysouts

[ August 27, 2004: Message edited by: Praful Thakare ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well a Servlet is Java code right? So to call a plain old Java object, you just create an instance of it in your Servlet, then call one method that will bo your Database stuff for you and return the information that you want, that you then put into the Response object to send back to the client in any way you wish.

Good Luck

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC through Servlet