Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please Help:Access to Sql Server from servlet  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
Robert Brunner
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!