Saturday, February 25, 2012

[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name

I have a ms sqlserver 2000 database installed on my Windows NT4 PC.

I want to access this database from a java program using JDBC - ODBC

Here are the relavent lines of code from my java source code file.

Note : I am attempting to access the sample database "Northwind Traders" that came with the installation.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
datasource = "NORTHDS";
url = "jdbc:odbc:" + datasource;
con = DriverManager.getConnection(url,username,password) ;
query = "SELECT * from " + datasource + "." + username + "." + tablename;
ResultSet rs = stmt.executeQuery(query);

The connection succeeds but the query fails with :
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL
Server]Invalid object name 'NORTHDS.username.Customers'.

Using the program "Microsoft Access" I was able to verify that the "Customers" table is intact.

Any ideas ?Take a look at the following:

In your select statement there are a couple of potential problems that you need to verify.

select * from datasource.username.table ... which is returning 'NORTHDS.username.Customers'

should be

select * from Northwind.dbo.Customers ... (unless you renamed the database ...)

In other words, check to make sure your are referencing the correct database/owner/table in your select statement. It appears that the variables you are using are incorrect. tablename appears to be ok, but the username variable is returning username. Also, the datasource variable may not be your database name.

No comments:

Post a Comment