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/";
Related
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?
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.
I'm having this problem when trying to connect to my database. Change the value of the variable but still this error persists
Value mysql
##max_allowed_packet
16777216
Java Code
Properties connProps = new Properties();
connProps.put("user", Config.DB_USER);
connProps.put("password", Config.DB_PASS);
this._conn = DriverManager.getConnection("jdbc:" + Config.DB_DBMS + "://" + Config.DB_HOST + ":"
+ Config.DB_PORT + "/" + Config.DB_NAME, connProps);
Error :
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
Modify the my.cnf file on my server
The jdbc client, also has a "maxAllowedPacket" setting.
You may set your jdbc url like:
jdbc:mysql://192.168.15.1/dbname?useUnicode=true&characterEncoding=UTF-8&maxAllowedPacket=16777216
Had the same error, trying to connect to a MySQL database using DataGrip 2016.1 (from JetBrains).
Troubleshooting further, I've realised that my records entry were wrong; the database password was not correct.
Upon rectification using the correct database password, I no longer encountered the error and was able to connect successfully.
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.
I am using sqlserver with Windows authentication, with a Windows server.
I am trying to connect to a remote MS SQLSERVER on my local network using java eclipse but keep getting this error:
Error : com.microsoft.sqlserver.jdbc.SQLServerException: The port number 64038 databaseName = Data is not valid.
Here is the code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("# - driver loaded");
String server = "moddbs169d.network.local\\Moddbs169d\\SQL2008";
int port = 64038;
String database = "Data";
String jdbcUrl = "jdbc:sqlserver://"+server+":"+port+" databaseName = "+database+";integratedSecurity=true";
Connection con = DriverManager.getConnection(jdbcUrl);
System.out.println("# - Connection obtained");
If all is successful it should tell me connection obtained. The local connection and name of the database are both correct, so that is not the issue. The jdbc driver is also installed and working properly.
You are missing a semicolon before the "databaseName=xxx" property.
Without the semicolon, you are setting the port number to "64038 databaseName = Data". Admittedly, the error message could have used brackets to make it a bit clearer.
See (http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx) for the form of the connection URL.