How do I connect to a remote servers MySQL database using Java? - java

Currently I have written a program to populate a mysql database on my own computer and I connect to it using JDBC using:
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/data?useSSL=false", "root", "your_new_password"); // MySQL
// Step 2: Allocate a 'Statement' object in the Connection
Statement stmt = conn.createStatement();)
Now I need to populate the database of a remote server which I have been given a username and password to access. So I have the IP address of the the remote server, a username and password for the server. Once I have connected to the server, then I need to access the MySQL database on that server a root user and there is no password for root.
How do I connect to a remote MySQL server given the requirements above?
I have tried the following way:
Connection conn = DriverManager.getConnection(
"jdbc:mysql://10.XXX.XX.XX:3306/data?useSSL=false", "serverusername", "serverpassword"); // MySQL
But I get the error:
message from server: "Host 'Connection conn = DriverManager.getConnection(
"jdbc:mysql://10.143.38.63:3306/Adwhere2?useSSL=false", "serverusername", "serverpassword");' is not allowed to connect to this MariaDB server"
I have tried to connect to the server via ssh on the terminal, using the username and password and it successfully connected. So the username and password is not the problem.
I am able to access the server via ssh on the terminal using:
ssh serverusername#10.XXX.XX.XX , and then typing the password.
Once connected to the server I can connect the the mysql database using:
mysql -u root

Related

i want to connect to SSMS database from eclipse but it throws SQLserverException

I want to connect to SSMS database from eclipse. when compiler reaches to DriverManager.getConnection(url) line it throws error.I enable TCP/IP also but it giving error.
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("driver loaded successfully");
connection= DriverManager.getConnection( "jdbc:sqlserver://localhost\\MSSQLSERVER;user=sa;password=coder182");
System.out.println("Connection created successfully");
connection.setAutoCommit( autoCommit);
statement=connection.createStatement();
String query ="insert into User_information(name, card_no,amount)" + "values ('"+name+"','"+card_no+"', '"+amount+"');";
statement.executeUpdate(query);
System.out.println(query);
}catch(Throwable th){
th.printStackTrace();
}
}
Error:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the
host localhost, named instance mssqlserver failed. Error:
"java.net.SocketTimeoutException: Receive timed out". Verify the
server and instance names and check that no firewall is blocking UDP
traffic to port 1434. For SQL Server 2005 or later, verify that the
SQL Server Browser Service is running on the host.
I want to connect it to database.
open SQL Server Configuration Manager
SQl Server Network Configuration
protocols for MSSQLSERVER
TCP/IP(Must be enabled already or enable it)
(Double Click)
IP Addresss
scroll down to IPALL
Enter port number 1433 or 1434 and leave dynamic ports empty and apply setting and restart your service
And you must specify databaseName you want to connect
"jdbc:sqlserver://localhost;instance=MSSQLSERVER;databaseName=name_of_database;user=sa;password=your_password;"
Mark answer as accepted if issue resolved
If "MSSQLSERVER" is the name of the database then try
"jdbc:sqlserver://localhost;databaseName=MSSQLSERVER;user=sa;password=coder182"
if it is the name of the instance try
"jdbc:sqlserver://localhost;instanceName=MSSQLSERVER;user=sa;password=coder182"
and of course verify that the server is running and using port 1434

Connect MSSQL DB as different Windows user in JAVA

I need to establish connection to MSSQL database from java using Windows Authentication as a different user
I getting SQL login failiure because windows authentication is using server username and password. I would like to change those.
this.connectionString = "jdbc:sqlserver://SERVER;DatabaseName=DATABASE;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.conn = DriverManager.getConnection(this.url);
this.statement = conn.createStatement();
I expect to log into remote database as a different windows user.
Many thanks in Advance

Access denied when trying to connect to mysql database via eclipse, java

I'm trying to connect to mysql database, but I get one and the same error: javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'user'#'localhost' (using password: YES)
I've already tried the following solutions:
Checked my username and password. MySQL connection is established via username = user1, password = 123. I use the same in my code, when I connect to the database: Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/employee","user1", "123");
Granted privileges to user1 in the following manner:
GRANT ALL PRIVILEGES ON * . * TO 'user1'#'localhost';
FLUSH PRIVILEGES;
By the way I have the same problem when I try to connect with 'root'#'localhost'.
Do you have any ideas why it doesn't work still?
MySQL JDBC is only able to connect via TCP/IP (on Unix, or TCP/IP or named pipe on Windows).
The MySQL JDBC driver cannot establish a connection using the local unix socket.
With MySQL on Unix, localhost has a different meaning that we might expect. It is not a hostname synonym for the 127.0.0.1 TCP/IP loopback address. On Unix, MySQL user
'user1'#'localhost'
specifies a user that can connect only via unix socket file; it's not possible to connect to that user via TCP/IP.
The above explains why MySQL is refusing a connection from JDBC: the connection attempt fails because a matching user does not exist.
To create a MySQL user that connect from TCP/IP loopback address, assuming that MySQL is started with --skip-name-resolve and without --skip-networking, we can specify user as:
'user'#'127.0.0.1'
That user would allow connection from JDBC.
(If MySQL DNS name resolution is enabled, then we would need to use a hostname that resolves to the loopback address; or we can consider using a wildcard '%' for the hostname portion of the user.)
Try this out might help:
1) goto mysql terminal. 2) mysql> use mysql; 3) mysql> select user, host from user; 4) There next to you user set the host as "%" instead of localhost
.
now in the connection instead of localhost tryp specifying the ip address of the server.
Hope this helps you. :)

how to connect JDBC postgres connection without the password when no password is set

I am trying to connect to postgres localhost using JDBC.
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost/mydb?user=postgres");
However the code keeps ask me for password when I have setup passwordless crendential.
For example, I can connect to psql on terminal by typing,
psql -U postgres -h localhost
And it connects without asking password. How can I connect to postgres without inputing password to the driverManager?
You can open a jdbc connection without specifying a password, but you probably should configure trust authentication for IP sockets. See more info here.

Couldn't connect to a MySQL DB via JWS app

Could someone help me with establishing connection with my DB on non-localhost server?
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://"+SERVER+"/javadb?user=javadb&password=*****");
How could I set-up the SERVER String for my domain www.lmntstudio.cz?
All required files are stored in one folder on the server.
I did it first on my localhost server and everything works well. But with the connection to another server is trouble.
I would recommend not to open MySQL connection from the client/user machine to the server. Because this would make your DB server open and prone to attacks.
Rather consider using web service. call that web service from your JWS application from client side and get the data.
Another point to consider is that opening a connection on a port say 3360 may be blocked by the firewall. If my firewall is set to block any out bound connection on port 3306 the connection could not be established.
Please ensure the MySQL is running in default port 3306 else you need change the port number accordingly.
Connection connection = DriverManager.getConnection(
"jdbc:mysql://www.lmntstudio.cz:3306/javadb", "username", "password");
instead of www.lmntstudio.cz you can use ip of the domain.
run the following in the command line to get the ip
cmd prompt> nslookup www.lmntstudio.cz
The Url syntax as follows
jdbc:mysql://(host/ip):port/databasename", "username", "password"

Categories

Resources