简体   繁体   English

java.sql.SQLException [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]无效的对象名“表名”

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

When I connect Java to Mssql using JDBC ODBC driver 当我使用JDBC ODBC驱动程序将Java连接到Mssql时

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 这个catch块引发了这个异常

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

I am working on this part from last 2 days. 我从最近2天开始从事此工作。 How I will solve this exception? 我将如何解决此异常?

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 您的用户不再具有对该表的读取权限

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 我以为您的连接字符串没有默认数据库-您可能正在连接到“ master”,在这种情况下,上述方法将无法工作

You can test this by qualifying your query with the database and schema name: 您可以通过使用数据库名称和模式名称来限定查询来进行测试:

eg 例如

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) 将值放在需要的位置(标准默认架构为'dbo',例如ProductsDatabase.dbo.products)

If this works then your connection string is incorrect 如果可行,则您的连接字符串不正确

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? 也许没有连接到正确的数据库?

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. 在我的情况下,问题的原因是运行报表的用户将DEFAULT_DATABASE指向主数据库,将其更改为正确的数据库后,便能够找到该对象。

Thanks 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解决“java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index”错误 - unable to resolve "java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index" error java.sql.SQLException 无效的对象名称 - java.sql.SQLException Invalid object name java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]找不到数据源名称,也没有默认驱动程序 - java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]找不到数据源名称,未指定默认驱动程序 - java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序] - java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 无法连接到数据库错误:java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]找不到数据源名称并且未指定默认驱动程序 - Cannot connect to database Error: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified Java / MSSQL:java.sql.SQLException无效的对象名称'TableName' - Java/MSSQL: java.sql.SQLException Invalid object name 'TableName' java.sql.SQLException:无效的列名 - java.sql.SQLException: Invalid column name run:java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]无效的连接字符串属性 - run: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid connection string attribute java.sql.SQLException:[Microsoft][ODBC Driver Manager] 描述符索引无效 - java.sql.SQLException:[Microsoft][ODBC Driver Manager] Invalid descriptor index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM