aspose file tools*
The moose likes Servlets and the fly likes Regarding database connectivity in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Regarding database connectivity in servlets" Watch "Regarding database connectivity in servlets" New topic
Author

Regarding database connectivity in servlets

Manoj Kumar Ravikanti
Greenhorn

Joined: Aug 18, 2006
Posts: 11
I m trying to access database(Sql server) from servlet class using type 1 driver(odbc) but it is giving the following exception

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

please someone tell me how to fix this problem

Thankyou
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Where did you you put the driver library file (jar)?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Manoj Kumar Ravikanti
Greenhorn

Joined: Aug 18, 2006
Posts: 11
hello Mr Souther,

Thanks for replying, I haven't used any driver class(jar)I used Odbc from data sources in the control panel to establish the link for database using a dsn name.

Should I use libary classes for connecting

Regards
Manoj Ravikanti
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Yes, no jar is required in case of type 1 driver.

How did you create the DSN? Can you post the whole step?
Manoj Kumar Ravikanti
Greenhorn

Joined: Aug 18, 2006
Posts: 11
hi rathi ji,

Thanks for replying I have created the dsn name in the following way

I have selected DataSources from control panel then
I have selected add then I have selected the driver as Sql Server,clicked finish
then i have given data source name as link
then i have selected the my system server and the database name from which i have implemented, then i have given the authentication from sql
then i said ok
then i have tested the data source which gave me Data Source test successfull

which I have implemented in my servlet class as following

Yes, no jar is required in case of type 1 driver.

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

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

int id=Integer.parseInt(req.getParameter("id"));

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:NIKE","sa","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from myemp where eid= "+id);
}
catch (Exception e)
{
out.println(e);
}
..............................
...........................

Thankyou,

Regards
Manoj Ravikanti


How did you create the DSN? Can you post the whole step?
--------------------
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830

Connection through DSN:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbcdbcSN_Name","username","password");

To create DSN:

programs -> settings -> control panel -> administrative tools -> data sources (ODBC)

create a new DSN here.

[ August 23, 2006: Message edited by: rathi ji ]
Manoj Kumar Ravikanti
Greenhorn

Joined: Aug 18, 2006
Posts: 11
thanks for your quick reply,

But can you help me how to create a DSN in the middle of the code..

Thankyou

Regards,

Manoj Ravikanti
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding database connectivity in servlets
 
Similar Threads
slow swing application
SCEA mock question from Whizlabs
C/ODBC and ODBC for Navision
Connecting Java Application with a Database
ODBC & JDBC Connection