File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC through Servlet

 
Archana Moorthy
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Praful Thakare
Ranch Hand
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 642
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic