[英]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:
这可能有几个原因:
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.