• 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

JDBC through Servlet

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Archana Moorthy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic