File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Please Help:Access to Sql Server from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Please Help:Access to Sql Server from servlet " Watch "Please Help:Access to Sql Server from servlet " New topic

Please Help:Access to Sql Server from servlet


Joined: Jul 17, 2001
Posts: 1
when i try to access sql server database i get the following error. I think the problem is in the syntax....can anybody please let me know about this!!
Here's part of the code:
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DbTest extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
// First, set things up.
// JDBC driver.
String dbDriverName = "sun.jdbc.odbc.JdbcOdbcDriver";
// Connection URL.
//String dbConnectionURL = "jdbc dbc ractice";
//use this for remote access!!
String dbConnectionURL="jdbc dbc:;DRIVER={SQL SERVER};SERVER=(;DATABASE=Northwind;UID=guest;PID=guest" ;

// Connection object.
Connection dbConnection = null;
// Statement object.
Statement dbStatement = null;
// SQL statement to execute.
String sqlStatement = "Select * from ORDERS";
// Result set object.
ResultSet dbResultSet = null;
// Start the servlet output.
PrintWriter out = resp.getWriter();
out.println("<html><head><title>DB Test</title></head><body>");
out.println("<h1>Database Test</h1>");
// Start the db access code.
try {
// Create JDBC driver instance.
// Create db connection.
dbConnection = DriverManager.getConnection(dbConnectionURL);
// Create Statement object.
dbStatement = dbConnection.createStatement();
// Execute the query.
dbResultSet = dbStatement.executeQuery(sqlStatement);
// Print column headers.
ResultSetMetaData rsMetaData = dbResultSet.getMetaData();
int colCount = rsMetaData.getColumnCount();
// Displays the results in a table.
// Start the table.

and here's the error:
java.sql.SQLException: [unixODBC][Driver Manager]Data source name not found, and
no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(
at sun.jdbc.odbc.JdbcOdbc.standardError(
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(
at sun.jdbc.odbc.JdbcOdbcDriver.connect(
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(
at DbTest.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at com.sun.web.core.ServletWrapper.handleRequest(
at com.sun.web.core.InvokerServlet.service(
at javax.servlet.http.HttpServlet.service(
at com.sun.web.core.ServletWrapper.handleRequest(
at com.sun.web.core.Context.handleRequest(
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
"R R P",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Robert Brunner
Ranch Hand

Joined: Jul 18, 2001
Posts: 49
The exception looks to be saying that your JDBC URL is not valid. To use the JDBC-ODBC bridge, you need to register the ODBC datasource. Start up ODBCAD32 (in the control panel, or in the system32 folder) and set up a system DSN so that mySQLServer points to northwind (or whatever).
Then use a URL like jdbcdbc:mySQLServer, and pass the username and password to the getConnection statement.
I agree. Here's the link:
subject: Please Help:Access to Sql Server from servlet
It's not a secret anymore!