0

When I connect Java to Mssql using JDBC ODBC driver

try 
{
    ps=conn.prepareStatement("UPDATE products SET stock=? WHERE id=?");

    ps.setInt(1, prods.getStock());
    ps.setInt(2, prods.getId());

    int b = ps.executeUpdate();

    if(b!=0)
    {
        System.out.println("success");
    }
    else
    {
        System.out.println("Fail");
    } 
}
catch(SQLException e)
{
    System.out.println(e);
}

This catch block rises this exception

java.sql.SQLException [Microsoft] [ODBC SQL Server Driver]
  [SQL Server] Invalid object name 'products'

I am working on this part from last 2 days. How I will solve this exception?

6
  • 1
    are you sure that you have table products in your database? Commented May 16, 2012 at 11:38
  • try UPDATE [products] and see if it works Commented May 16, 2012 at 11:38
  • Maybe you selected a wrong DB? Can you show how the connection is created? Commented May 16, 2012 at 11:39
  • yes i am sure my database having products table Commented May 16, 2012 at 11:55
  • i already used that UPDATE[products] i am getting the same error Commented May 16, 2012 at 12:08

4 Answers 4

5

This error message is thrown when the table can't be found. That can have several reasons:

  • you are connecting to a different DB
  • the table was deleted
  • the table is in another DB schema
  • your user does not have read permissions on that table any more
Sign up to request clarification or add additional context in comments.

5 Comments

table is exists and database is also same , but still it is giving error.
here i am using windows authentication is there any problem with that?
@pardhu: Does your windows user have rights in your MSSQL Server? You can check that if you connect with that user to your DB using MS SQL Studio and try to browse the table you need.
i am browsing that table and watching those data is all possible, then which rights i need here?
@pardhu: Could you show your connection string? Also are you sure that the table is in the default schema of your user?
1

I'd imagine that your connection string has no default database - you are probably connecting to 'master' in which case the above won't work

You can test this by qualifying your query with the database and schema name:

e.g.

ps=conn.prepareStatement("UPDATE [YourDatabaseName].[schema].products SET stock=? WHERE id=?"); 

put your values in where needed (standard default schema is 'dbo' eg. ProductsDatabase.dbo.products)

If this works then your connection string is incorrect

2 Comments

i write using this query "master.dbo.products" here also i am getting same error.please help me
Is the product table in the 'master' database or another database? I would expect that you created a new database for the table, in which case you should not put master.dbo.products but <productsdatabasename>.dbo.products - can you show your connection string?
0

Normally this exception is thrown if the table does not exist, or that there is an error with the connection string. Maybe not connecting to the correct database?

1 Comment

here i am using windows authentication is there any problem with that?
0

I ran into similar issue. Cause of the issue in my case was the user running the report has the DEFAULT_DATABASE pointing to the master database, after changing it to the right database it was able to locate the object.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.