Setting up a connection to Online MySQL database - java

all of the examples, unfortunately on the net are consumed of downloading a MySQL database to your computer rather than taking advantage of Online Database.
What should I replace the following code to get data from an online db rather than a 3306 typical desktop MySQL;
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "usrname", "pswd") ;

Since not too much information was given, we will use an example - let's say the database is located at user3129325.com, and is hosted by a web hosting provider that allows remote SQL. If you wanted to connect to the database named "DBNAME" (using the same name as in your example), you would want to use the following command:
Connection conn = DriverManager.getConnection("jdbc:mysql://user3129325.com:3306/DBNAME", "usrname", "pswd") ;
Just simply replace localhost with the URL of the remote MySQL database.

Related

How to create a proper mysql connection

I'm still new to programming. I have created a mysql database. And i'm connecting to it in my java app using local host link like this:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataTest?allowPublicKeyRetrieval=true&useSSL=false", "root","password");
However, I want my java program to be able to connect to my database on any device.
What should I do? how can I make this happen?
You can use the connection string like below
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root","root");
For more details, you can read the oracle docs from the below link
https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

I can connect to AWS MySQL RDS instance from workbench but not in my JDBC application

I am trying to create a Java application that connects to a MySQL database and I am using AWS to host it. So I created the AWS RDS instance and I got it to connect to the MySQL workbench just fine. My problem arises when I am trying to use JDBC to connect to it.
I have security groups that allow traffic from anywhere, but I also tried just allowing my IP.
I had it working when I was using a localhost but now I'm trying to move it to a server so keeping it localhost isn't an option (used localhost to test the application and such)
I made sure that my user was a remote user by doing
SELECT * from mysql.user;
And made sure that the host was a '%' and that it had all the privileges
So my code I'm trying to connect with in Java is
String connectionURL = "jdbc:mysql:/{hostname}:{port}/{database name}/?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
con = DriverManager.getConnection(connectionURL, username, password);
Of course I have the actual host, port, and such in there just changed it for posting online.
When I try running it on Eclipse it says this java.lang.Exception: Database not found
I looked up some tutorials on AWS docs to make sure it lined up, which it all did.
Anyone have any idea why it might be connecting to MySQL workbench and not the JDBC?

Command line for using remote SQL database

I'm currently working on a project for conducting online test for our college,
I have successfully performed the test using our internal servers, now I need to host online through private hosting services, everything is ready except for the Database.
The database is created using the same service provider but I don't know how to modify the command line for remote hosting.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","username","password");
This is the command line I used in our internal server, now how to modify it for remote SQL database
The remote db name is "epiz_22735410_placement_db"
The host name is "sql104.epizy.com"
You could follow this pattern:
String url = "jdbc:mysql://HOST:PORT/DATABASE";
Connection con = DriverManager.getConnection(url, "username", "password");
Instead of localhost use sql104.epizy.com everything else should stay the same if it's working on local (assuming your credentials etc are the same as local)
e.g.
mysql://sql104.epizy.com:3306/epiz_22735410_placement_db","username","password");

Problem in establishing DB2 connection in Java with wrong username/password

I have a problem in establishing DB2 connection with wrong user-name/password. We have an application which runs on LAN on many systems using DB2 database located on my system as well as other systems.
Firstly I use this URL to create other system DB2 connection:
Connection con = DriverManager.getConnection("jdbc:db2://Rahulkcomputer:50000/XAN4", "rahulk", "dbirs#35");
this returns proper Connection object. Now when I change the URL to access my system DB2 connection with same user-name/password as (using same user-name/password is intensely done for checking error handling):
Connection con = DriverManager.getConnection("jdbc:db2://127.0.0.1:50000/XAN4", "rahulk", "dbirs#35");
This time it again returns the Connection object instead of throwing an SQLException specifying wrong user-name/password (due to my system's DB2 authentication is totally different from Rahulkcomputer's system)
After getting connection, I execute this query to check proper user name as explained in post:
Simple DB2 Query for connection validation
SELECT CURRENT SQLID FROM SYSIBM.SYSDUMMY1
(this returns "rahulk" in both cases)
Why DB2 created connection in 2nd case with wrong user-name/password (moreover when we close all the services of DB2 on Rahulkcomputer, even then I get the connection in 2nd case)?
Thanks in Advance.
You either created your database with the restrictive option or revoked the select right to sysibm from PUBLIC. The connection you had was fine, the access rights not. 42704 is DB2's way of saying "huh?", it did not recognize sysibm because you had no rights to see it.

Connecting to local instance of PostgreSql with JDBC

I have a running local instance of PostgreSql on a linux machine. When I use psql command from the shell I success to log in without any problem. I need to connect to the PostgreSql via the JDBC, but I don't know what exactly should I pass as url parameter to DriverManager.getConnection().
It should start with jdbc:postgresql: but what's going next?
I was told by the system group that a database with was created like user name. e.g. if my user is jutky a db named jutky was created, but when I try to open a connection to jdbc:postgresql:jutky I get an error
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "jutky"
:(
Additional info
When I login via the psql I'm not prompted for the password, so when I try to login via JDBC I pass an empty string as a password - is it correct, or should I pass null or something?
When I type psql --help in the shell I see among the rest this line:
Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "/var/run/postgresql")
So I understand that I connect to PostgreSql via a socket directory, does that matters something to the URL string in the JDBC?
EDIT
First thanks for the answers.
Second: its not first time I'm using JDBC and in particular not the first time I'm connecting to the PostgreSql from JDBC, so I know the general rules and I have read the documentations. However in the described case I'm not sure how exactly should I construct the connection string if the instance is running via the socket directory and what password should I provide. Because when I login via the psql I'm not prompted for password at all.
Thanks in advance.
In addition to other answers note that by default Postgres is configured to accept connections via Unix sockets with authentication based on your operating system account, that's why psql works fine and doesn't require the password.
JDBC connections are made over TCP/IP with password authentication, so you need to modify pg_hba.conf accordingly. For example, this line allows TCP/IP connections from the same machine to all databases for all users with password authentication:
host all all 127.0.0.1/32 md5
After adding this line jdbc:postgresql:databasename should work.
EDIT: You can't create a JDBC connection over Unix socket since PostgreSQL JDBC driver can only work over TCP/IP. The password you use when creating JDBC connection is the password assigned to your user. If you don't have it, you can assign it, for example, using ALTER USER command. See 19.3. Authentication methods.
See also:
19.1. The pg_hba.conf file
It's all explained in official documentation.
This is the relevant part:
String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);

Categories

Resources