如何解决 com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.Table2'

Posted

技术标签:

【中文标题】如何解决 com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name \'dbo.Table2\'【英文标题】:How to solve com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.Table2'如何解决 com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.Table2' 【发布时间】:2015-07-22 11:31:20 【问题描述】:

我通过从第一个数据库 [WBSEDCL].[dbo].[Table1] 读取数据,然后将其插入到第二个数据库 [ OrganizationMaster].[dbo].[Table2] . 我在我的项目中使用 sqljdbc41.jar。 插入代码如下 -

private static Connection getNewDBConnection() 
        System.out.println("************************Inside Get DB Connection**************************");
        Properties props = new Properties();
        if (connection != null)
            try 
                if (!connection.isClosed())
                    return connection;
             catch (SQLException e) 
                e.printStackTrace();
            

        try 
            String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String url = "jdbc:sqlserver://142.168.0.112:1733;DatabaseName=OrganizationMasterDB";
            String user = "sa";
            String password = "Dsdf@123";
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
            if (!connection.isClosed())
                System.out.println("---------------------DB Connection is Established----------------------");
         catch (ClassNotFoundException e) 
            System.out.println("Class Not Found Exception Thrown ");
            e.printStackTrace();
         catch (SQLException e) 
            System.out.println("SQL Exception Thrown");
            e.printStackTrace();
        

        return connection;
    


     public static void insertDetailsList(PersonalLedger pl) 
        Connection conn = getNewDBConnection();
        PreparedStatement statement = null;
        DetailsList dl = null;
        int temp=0,shareAmount=0,shareBalance=0,theiftFundAmount=0,theiftFundInterest=0,GAmtDepo=0,GInterest=0,ShareWithdrawn=0;
        String EmNo=null,MemberNo=null, fundString = "Share",status="Unknown",remarks="OtherAmount for Share is The Withdrawn Share Amount",sql=null;
        boolean flag= false;
        Date sdate = pl.SDate,gdate=pl.Gdate,tdate=pl.TDate;
         EmNo = pl.EmNo;
         MemberNo = pl.MemberNo;
         shareAmount = pl.SAmtDepo;  
         shareBalance = pl.balance;
         ShareWithdrawn = pl.ShareWithdrawn;
         theiftFundAmount = pl.TAmtDepo;
         theiftFundInterest = pl.TInterest;
         GAmtDepo = pl.GAmtDepo;
         GInterest = pl.GInterest;
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        try
            System.out.println("*****************INSERTING SHARE FUND DETAILS******************");
            sql = "INSERT INTO [dbo].[DetailsList] VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
            statement = conn.prepareStatement(sql);
            statement.setString(1, pl.EmNo);
            statement.setString(2, pl.MemberNo);
            statement.setString(3,"Share");
            statement.setLong(4, shareAmount);
            /*Date share_date = (Date) formatter.parse(new Date());*/
            statement.setLong(5,0);
            statement.setLong(6,pl.ShareWithdrawn);
            statement.setString(7,"Unknown");
            statement.setString(8,"OtherAmount for Share is The Withdrawn Share Amount");
            statement.setDate(9, pl.SDate);
            statement.setDate(10,null);
            statement.setLong(11, shareBalance);
            temp = statement.executeUpdate();
            if (temp != 0) 
                flag = true;
                System.out.println("ROW INSERTED SUCCESSFULLY");
            
         catch (Exception e) 
            System.out.println("Exception in Insert Details List Items");
            e.printStackTrace();
        
        

每当我运行代码时都会出现问题,我得到 SQLException。 stackTrace 如下:

Exception in Insert Details List Items
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.DetailsList'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:215)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:426)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:372)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5846)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1719)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:315)
at Test.insertDetailsList(Test.java:203)
at Test.main(Test.java:290)

我完全被这个错误吓傻了,在网上找不到任何相关的解决方案! 任何帮助将不胜感激。谢谢。 编辑: 在做了一些研究后,我将 Table2 移至第一个数据库,对连接字符串进行了必要的更改并执行了程序。代码执行没有任何错误。所以问题仍然存在 - 1.为什么当 Table2 在第二个数据库下时未找到较早的模式? 2.数据库中是否需要任何配置才能使用 java连接正确的模式并访问表,如果是,那又是什么?

【问题讨论】:

【参考方案1】:

使用[dbname].[dbo].[tblname] 查询,例如select * from [dbname].[dbo].[tblname]

【讨论】:

欢迎来到 Stack Overflow。不鼓励仅使用代码的答案,因为它们没有解释它如何解决问题。此外,您的代码似乎没有解决这个问题 - 您建议的格式是用户已经拥有的格式。请编辑您的答案,以说明您的答案如何帮助解决问题。【参考方案2】:

假设你的 INSERTTable2 那么它应该改变

sql = "INSERT INTO [dbo].[DetailsList] VALUES "
        + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

包括目标数据库名称。使用类似的东西(并且您不需要查询中的分号)

sql = "INSERT INTO [OrganizationMaster].[dbo].[DetailsList] VALUES "
        + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

【讨论】:

【参考方案3】:

检查表DetailsList是否存在并且用户对该表具有权限,您可以检查我从该用户登录到SQL Server Management Studio并尝试访问该表。

【讨论】:

以上是关于如何解决 com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.Table2'的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Ajax 跨域请求不到的问题

如何解决包冲突问题

如何解决包冲突问题

如何解决ajax跨域问题

MySQL 的 10048问题,如何解决?

如何解决smartgit的冲突问题