I am trying to connect from Java to a DSN using MariaDB,
If I not use the DSN it works
Class.forName("org.mariadb.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mariadb://< host >:<port>/< database >", < user >,< password >);
But I would like that mariadb uses the host and port specified in the odbc.ini, but I can’t make it work.
I have tried many ways but it did not work, I could not find anything on internet about it.
eg:
Connection conn = DriverManager.getConnection("jdbc:mariadb:< DSN_NAME >", < user >,< password >);
The DSN is correctly configured in the odbc.ini, it works with isql
Can anyone help me?,
Thank you very much.
Related
I am frustrated seeing this error and not knowing the solution. I am trying to connect to a mysql db with a server url but it is giving my mysqlException(stacktrace below). The code works fine till here:
String dbUrl = "jdbc:mysql://server_url/db_name";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
String user = "user";
String password = "password";
conn = DriverManager.getConnection(dbUrl,user,password);
This is the error I'm getting
java.sql.SQLException: null, message from server: "Host '172.23.251.154' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
Is this because I'm using a different version of mysql-connector jar?
Please help me.
From Chapter 6. SQL Questions: How Do I Enable TCP Connections to MySQL?,
By default, MySQL won't allow ANY users access to any of the databases if they connect over a TCP connection. In order to permit the connection, you must create a entry in the user table of the mysql database (make sure you select the PASSWORD function to encrypt your password). In particular, the Host field needs to indicate which host(s) are permitted to connect. If you specify % (which I would not recommend), then a user would be able to connect from any host.
What about firewall on port 3306?
May be the user you are trying to access doesn't have enough privileges to access the database from the given server.
So try providing GRANTS to the user.
GRANT ALL PRIVILEGES ON database_name TO 'user'#'hostname' IDENTIFIED BY PASSWORD <password>;
FLUSH PRIVILEGES;
here hostname can be your host address "172.23.251.154". Dont forget to flush privileges and then try connecting the server.
If your MySql server is located on your host provider there is option in your control panel where you can set ip addresses from which you can connect to mysql server in your case 172.23.251.154 if this is your ip address
I have a Java application that needs to connect to a remote PostgreSQL database over a VPN. Here is the relevant code:
Class.forName("org.postgresql.Driver");
Connection con = null;
con = DriverManager.getConnection("jdbc:postgresql://" + sqlHost + ":" + sqlPort + "/mydb", username, password);
This throws the error
org.postgresql.util.PSQLException: FATAL: pg_hba.conf rejects connection for host "172.16.7.5", user "xxxxx", database "xxxxx", SSL off
The Host IP address in sqlHost is actually 192.168.12.55, but if you notice the error message says that it is connecting to host 172.16.7.5 (which is the IP address assigned by the VPN).
I am able to connect to this PostgreSQL database using the exact same connection parameters on the exact same VPN using PGAdmin and using Python's psocopg2 module. Here is the equivalent Python code:
conn = psycopg2.connect("dbname=mydb user="+username+" password="+password+" host="+sqlHost+" port="+sqlPort)
Why in the world is only Java having problems with this? Since the connection works over PGAdmin and Python, I assume there is some setting in Java that I am using incorrectly, but I can't find anything.
EDIT: After reading into PostgreSQL docs a little more, I found that the issue with it listing the wrong hostname is not part of the issue but rather just the way PostgreSQL sees my computer over the VPN. Problem is still not solved, however.
Okay, I fixed this myself. The problem had nothing to do with the VPN but rather with the fact that Java by default does not try any sort of SSL connection by default whereas PGAdmin and psycopg2 do.
The solution was to add the following parameters to my connection url:
ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
I'm not sure if this might be the problem, but I've experienced similar problems in the past trying to connect to a database with VPN turned on.
Try running your application with this JVM argument passed at application launch time:
-Djava.net.preferIPv4Stack=true
See also this answer for a more permanent solution.
I have two computers on one network. I have entered the ip of another computer, it works fine in the browser, but when using this ip on java I found the database connected to my localhost database not from the other computer!
my code jdbc
public Connection MakeConnect() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:http://192.168.1.109:5560/isqlplus", "school",
"sch"); // first : user(hr) second pass(hr) .!
return connection ; // return connetion of database
I think your JDBC connection string may be in a wrong format. You should provide this in the following way:
jdbc:oracle:thin:#[HOST][:PORT]:SID
jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE
Instead it looks you specified ISQLPLus link. I haven't tested it but my guts say me it is the reason. I think in your case it should be something like that:
jdbc:oracle:thin:#192.168.1.109/SERVICEorSID
And you have to find the service name by yourself. In a generic case it is a name of the database. By default it is usually set to ORCL but possibly you set it to another name.
Make sure you read a following document: http://www.orafaq.com/wiki/JDBC
If the connection succeeds without error and if this 192.168.1.109 ip address is not of your local machine then it must connect to some other machine.
What made you think it's connecting to the local DB instance?
Check firewall settings of the other computer. Sometimes it may not allows for the external connections
If you want to allow a specific client ip-address (for example: 192.168.1.4) to access the mysql database running on a server, you should execute the following command on the server that is running the mysql database.
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root#'192.168.1.4' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Also, update firewall rules to make sure port# 3306 is open on the server that is running the mysql database.
I have a remote MySQL database that I can connect to with MySQL Workbench (screen shot below), but I ultimately need to connect to it via JBDC and everytime I try to connect, it throws an exception. I'm new to this, so could anyone provide me some insight on what could be wrong?
String host = "testdb.db.10682960.hostedresource.com";
String datab = "testdb";
String url = "jdbc:mysql://" + host + ":3306/" + datab;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, datab, "password");
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Edit: Not sure if this is relevant or not, but I'm running this on Android.
It looks like you're missing a user name. Try adding "?user=testdb" to the end of your url.
Edit: I didn't realize this was an Android app. Technically, it should be possible, but not advisable. Your network connection would be much less reliable, and you would be pushing server credentials out to your client. Use of a REST API to communicate from your Android to a web server in the same data center as the database is a much safer option.
I don't have experience trying this from Android... so I don't know what exception would be thrown if, for example, you can't reach port 3306 from the Android's connection to the network. Even if you get it working, though, do look into using a web service instead.
According to the jTDS FAQ (http://jtds.sourceforge.net/faq.html) the format of a JDBC URL is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
If I were you, I would replace the '?' with a ';' to adhere to the above format. The "query string" is not like that of a "normal" URL.
This, of course, assumes that you are using the open source JDBC connector found via the link above.
I want to connect to my MySQL database with Java.
I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)
The only problem is that I keep getting an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last
packet sent successfully to the server was 0 milliseconds ago.
Here is my code:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password");
I am not positive how to compose the URL (and where I get my username and password) but I have done A LOT of research.
I am the only person with acess to my database and domain, so there's no use asking the admin.
I use phpMyAdmin to create the database(s) and manage them. Do I use my phpMyAdmin username and password or what?
By the way, my site is hosted on Yahoo! Small Business.
So my questions are:
How do I make the connection URL?
What is my username and password?
I would say you are missing a forward slash on your URL.
Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password");
Or I have a feeling that there is something wrong with your access privileges. This same thing happened to me also and it was a problem of Firewall blocking the port on the server. So verify this is not the case.
How do I make the connection URL?
Are you missing a forward slash in your URL? I would've assumed it would be something like:
jdbc:mysql://server/database
Load the drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
connect with the data base
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");
.....jdbc:mysql://this is use as it is
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of "127.0.0.1"
.....dbuser is the data base user
.....dbpassword is the password of the dbuser
.....3306 is the default port used for database