Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help:Access to Sql Server from servlet

 
R R P
Greenhorn
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 java.io.*;
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=(http://info2.graphiced.com:1433);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();
resp.setContentType("text/html");
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.
Class.forName(dbDriverName).newInstance();
// 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.
out.println("<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(JdbcOdbc.java:6031)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6188)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:2458)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3
20)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:163)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:199)
at DbTest.doGet(DbTest.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155
)
at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155
)
at com.sun.web.core.Context.handleRequest(Context.java:414)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:139)
 
Frank Carver
Sheriff
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 http://www.javaranch.com/name.jsp . 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.
Thanks.
 
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic