SQL Server Java Database connects to the master only

dluptydi  于 2023-08-02  发布在  Java
关注(0)|答案(1)|浏览(96)

I have a java application that needs to connect to 2 databases on the same server. The first one works fine when I connect using server/databasename/userid/password as I connect to the server and a specific database instance.

The second data base is set up on the same server but through SSMS I connect only connect to the server and it automatically connects me to Database2. i.e. I use

Server Type: Database Engine
SerevrName: xxx.yyy.z.1
Authentication SQL Server Authentication
Login:
password:

This works fine in SSMS, recognizes all the tables etc. But if I use ServerName/databaseName, I get an error that the database cannot be found. Server Type: Database Engine

SerevrName: xxx.yyy.z.1/Database2
Authentication SQL Server Authentication
Login:
password:

So similarly in java I cannot use the instance name. I leave the database blank. It conencts to master and I get a object not found error. If I change my query to include the database select * from database2.dbo.table, also I am getting an error.

I am not sure how to use and connect to Dataabse2, like I can do it in sql. USE Database 2; GO;

This database is created on a SQL Server 2012 and is very old. None of the people who set this up are with us the current time.

Any help with connecting to the second database will help. Thanks,

woobm2wo

woobm2wo1#

This question can be marked as answered.

I found out what the real issue was. I realized that while connecting to the server, the Master DB was set as the default. Then providing an instance name in the connection string gave an error that the database cannot be found. Connecting to the server and providing blank for an instance name, connects to the Master DB and obviously cannot find the table that you are trying to query. The DBA had to change the setting (change the default DB). In my case since this was an old DB trying to get hold of the credentials for sa was a challenge!

相关问题