If I create a database in the db2 console (create database CONNTEST), where will the database be hosted? I know that my instance is pointing to localhost:50000, but in my JDBC application con = DriverManager.getConnection (url, user, password); where url = jdbc:db2://localhost:50000/CONNTEST is returning an invalid database address error. Is there a way to see where my database is hosted from the db2 console itself? Thanks!
If you can run the db2 command line, you can figure out how the database is cataloged, and work your way back to the host address. I'm assuming that your database is not a mainframe database... that adds some complexity to the directory that I won't go over.
First, you'll run db2 list database directory, and you'll get some output like this:
Database 1 entry:
Database alias = DB1
Database name = DB1
Node name = DB1
Database release level = 14.00
Comment =
Directory entry type = Remote
Catalog database partition number = -1
Alternate server hostname =
Alternate server port number =
The database alias field will be your CONNTEST name that you've been connecting to. You want to know what the node name field is. The database name field could be different, that field will be what the database is actually called on the server side.
Taking the node name, you will run db2 list node directory. You'll look for the node entry that matches the node name from your database entry:
Node 1 entry:
Node name = DB1
Comment =
Directory entry type = LOCAL
Protocol = TCPIP
Hostname = db2host.domain.local
Service name = 50000
Here, you'll see the hostname and service name fields, which give you the host and port that it is connecting to.
It might be that localhost isn't being mapped to your IP address OR 127.0.0.1. You can start by trying your machine's IP address.
Make sure your DB2 instance is listening on port 50000. That's the default, but it's worth checking.
If that doesn't work, perhaps it's a permission issue. The database itself might be restricting access by client IP, username, and password. That's how it works with MySQL, for example.
Related
I need to connect to an external database to copy data from there to my table. I have a TNS file for this external database, and I am trying to connect using JDBC like this:
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
host +
")(PORT=" +
port +
")))(CONNECT_DATA=(SERVICE_NAME=" +
service +
")))",
user,
password);
...
But when trying to connect, i get the error java.net.UnknownHostException (host is not recognized). I guess the problem is that this is an internal host and I don't have access to it.
How to connect to the database using TNS?
You should not need the full TNS text. The following should suffice
getConnection("jdbc:oracle:thin:myuser/mypass#//"+host+":"+port+"/"+service);
If you have a tnsnames.ora then, you can provide the TNS alias as part of your connection string. Make sure you try to login to the Oracle Database through sqlplus using the connection string present in tnsnames.ora.
// dbname_tnsalias - It is the TNS alias present in tnsnames.ora.
// TNS_ADMIN --> Absolute path where tnsnames.ora is present.
final String DB_URL="jdbc:oracle:thin:#dbname_tnsalias?TNS_ADMIN=/Users/test/";
I'm working on a project and I have put my database folder in project folder. How can I make a database connection to any directory rather than just default MySQL dir in Java?
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
String UserName = "root";
String Password = "admin";
Connection con = null;
try {
con = DriverManager.getConnection(MySQLURL,UserName,Password);
if (con != null) {
System.out.println("Database connection is successful !!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
When doing this, I get this error:
java.sql.SQLSyntaxErrorException: Unknown database 'c:\program files\snakegame'
Your connection URL is wrong
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
I am not sure why your MySQLURL contains C:\Program Files\SnakeGame
The connection URL for the mysql database is
jdbc:mysql://localhost:3306/[DatabaseName]
Where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running (we may also use the server's IP address here), 3306 is the port number, and [DatabaseName] is the name of the database created on the MySQL server.
Replace the [DatabaseName] name accordingly after creating the database in MySQL server
Combining localhost:3306/ with C:\\Program Files\\SnakeGame makes little sense for any database - either you're trying to connect to a file-based database (in which case the localhost... part makes no sense) or you're working with a server-based one (in which case the C:\... part makes no sense.
Also, this connection string would make little sense for a file-based database either because you didn't specify a specific file, just a path.
Incidentally, MySQL is server-based, not file-based. It's expecting a database name after the localhost:3306/ part, not a path (hence the error). The physical location of the actual database program is an installation/configuration issue - it has nothing to do with how you actually connect to the database server once it's already running.
Think about it this way: when you call an external database, web service, or web site, do you need to know which physical folder it's deployed to? Obviously not. The physical folders involved are completely irrelevant when calling MySQL or another database like this.
One of the comments pointed this out, but did you intend to use SQlite or some other file-based database here instead?
I have a lot of Lotus Notes / Domino (version 7) database to migrate to a new software.
On my workstation (with Lotus Notes installed), I'm using a standalone Java application to connect to a local replica an extract data.
However the replication of the distant database is still a manual process. I'd like to automatise it.
My java code basically looks like this :
Session localSession = NotesFactory.createSession(); // With Notes thread initialized
Session remoteSession = NotesFactory.createSession(SERVER, USER, PASSWORD);
Database localDb = localSession.getDbDirectory(null).openDatabase("local_name", true);
Database remoteDb = remoteSession.getDbDirectory(null).openDatabaseByReplicaID(REPLICA);
// ***EDITED CALLING INSTANCE BELOW***
remoteDb.createReplica(null, "local_name"); // Error thrown here
However the last line throws an exception (from memroy, but something like)
CN=****/***** does not have the right to create database on a server
How is it possible that I don't have the right to create database on my local computer ?
Is there any other way to programmaticly create a local replica from a distant database ?
Edit: changed calling instance of create replica to match my code causing the issue
My guess is that it's just giving you the wrong error message. One thing that's definitely wrong is that he first argument for createReplica should be an empty string, not a null pointer. I.e., try this:
localDb.createReplica("", "local_name");
Ok it looks like I found the answer.
AFAIU I had to open the database on the target server, using my local session, and run the createReplica() from here. This way, the createReplica is executed on my local Lotus Notes server, and the replica is created locally.
Session localSession = NotesFactory.createSession((String)null, (String)null, PASSWORD);
DbDirectory remoteDbDirectory = localSession.getDbDirectory(remoteSession.getServerName());
Database localSessionRemoteDatabase = remoteDbDirectory.openDatabaseByReplicaID(REMOTE_REPLICA_ID);
localSessionRemoteDatabase.createReplica("", LOCAL_FILE_NAME);
#Richard Schwartz Can you confirm this is ok ?
The only weird thing, is that it opens a prompt (like when it's expecting password) but the replica is created.
The process is executed within Eclipse.
I am trying to connect to sql server using by Java application. While connecting to H2 database, I appended schema=schema_name to the connection string in my java application and that worked.
However the same does not works for sqlserver.Connection is established but if my query does not contains the schema name prior to table name then query execution fails.
Please let me know of JDBC supports schema name in connection string or their is any other way.
Sadly, that is not possible. What you might want to do is set default schema for that user instead.
create login foo with password = "superstronkpassword";
create user foober for login foo with default_schema = "my super schema";
I have 2 database: harrington, which I created myself and pacsdb which I inherited. Both of these 2 are located in the same MySQL database. I am using Netbeans to debug things. In Netbeans I can connect to the database by
jdbc:mysql://192.168.0.100:3306/harrington?zeroDateTimeBehavior=convertToNull [fiji on Default schema]
jdbc:mysql://192.168.0.100:3306/pacsdb?zeroDateTimeBehavior=convertToNull [pacs on Default schema]
jdbc:mysql://localhost:3306/pacsdb?zeroDateTimeBehavior=convertToNull [pacs on Default schema]
To my own database, harrington, I have no problems either in Netbeans or in my own Java program. Pacsdb will not connect using 192.168.0.100, but it will connect using localhost. In my program it gives
java.sql.SQLException: Access denied for user 'pacs'#'192.168.0.100' (using password: YES)
To check what is going on I queried the sql database table 'user'. I got that both users use '%' as Host which is what I would expect from the code
sql = "create user 'pacs'#'%' identified by 'pacs'";
OK1 = executeStatement(stm1, sql);
if(OK1) {
sql = "grant all on pacsdb.* to 'pacs'#'%' identified ";
sql += "by 'pacs1' with grant option";
executeStatement(stm1, sql);
sql = "flush privileges";
executeStatement(stm1, sql);
}
I did find a difference when I queried the sql 'db' table. There I got 2 entries
Host Db User
% harrington fiji
localhost pacsdb pacs
I tried to delete the entry for pacsdb with localhost and insert a new entry for pacsdb with Host set to '%'. Apparently that isn't way to change from localhost, because I had difficulty to reach pacsdb. I deleted the entry from 'db' and inserted a new entry with '%' set back to 'localhost'. That got me back on line with the access denied error.
So my question is: how do I change from 'localhost' to '%'?
The problem is not the query, but the connection to the database.
MySql has the possibility to limit the access from users accessing it from an IP different from localhost.
Probably you have not the right to access the database from your ip.
Try to add the correct right with the following command
GRANT ALL PRIVILEGES
ON database.*
TO 'user'#'*'
IDENTIFIED BY 'newpassword';
Obviously you can't connect remotely to do that command. You need to enter via SSH on the remote machine and launch that command locally.