Trying to connect to mysql database with jdbc - java

I have ubuntu server running on virtual machine locally and I need to connect to mysql database there.
Database is in place and jdbc driver too. The only problem is that only way at the moment for my connection can be something like http://local/phpmyadmin/index.php?db=sandbox and that can't be used in jdbc connection string.
Hope, that somebody can advise a solution.

String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url,"username", "pwd");
Replace localhost with the IP of your VM. You have to use something other than NAT for networking in your VM (like Host-only, internal or bridged assuming VirtualBox).
You probably have to configure your Ubuntu firewall to let the connection through.
Also, you have to set mysql up to accept connections from the outside.
In the file /etc/mysql/my.cnf edit bind-address to your ip:
bind-address = your-vms-ip

Related

how to connect java with wamp mysql server

I am new java programmer, I try to setup jdbc connnectivity with wamp mysql server.
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection( "jdbc:mysql://182.73.151.36:3306/sbtsystem?user=sbtuser&password=Pm#dmin&useUnicode=true&characterEncoding=UTF-8");
182.73.151.36 this is server IP address. I can't understand how to connect java with wamp mysql server
Try This:
First run these commands
ping 182.73.151.36
telnet 182.73.151.36 3306
And make 182.73.151.36 mysql eligible for remote connection
After that change your connection line and run this program
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://182.73.151.36:3306/sbtsystem","sbtuser","Pm#dmin");
You need to first download the jdbc driver files for mysql
(download from here)
The code looks fine. You need to check the network configurations. Try the following commands to see if the server is accessible.
ping 182.73.151.36
telnet 182.73.151.36 3306
supposing your network configuration is correct,
I guess your mysql server on 182.73.151.36 is not configured for remote connection,

In jdbc, where do you specify host credentials?

According to this link, to connect to a mysql db you use the following string:
jdbc:mysql://localhost:3306/sakila?profileSQL=true
Instead of localhost, I have a remote linux vm that hosts my mysql database. Where would the credentials to log into that vm be?
Just replace your localhost with the machine name of your remote linux. You don't need to login into the remote vm as you will be connecting directly to the exposed 3306 port. Just fill out the credentials of your DB user in the connection string, that should be sufficient.
try this
jdbc:mysql://localhost/test?user=root&password=root

Database connection string for a remote database server named 'localhost'

I have a remote mysql database server setup on a machine myuniversity.edu and server is named 'localhost'. On it I have the database named 'MyDatabase'.
I want to connect it through Java.
The connection urls that I have tried are:
jdbc:mysql://myuniversity.edu/localhost
jdbc:mysql://myuniversity.edu/localhost/MyDatabase
jdbc:mysql://myuniversity.edu:3306/MyDatabase
but I get the `Connection refused: connect` exception.
Could someone please tell what the connection url should be in this case?
Not really sure if your machine name is myuniversity.edu, you can instead try the IP Address with the connection string, Localhost is the name for loopback network interface and accessible on that machine only. Also make sure if your default port for mysql (may be 3306) is open. With IP address your connection string would look like:
jdbc:mysql://192.168.0.123/MyDatabase
With IP and port it would be:
jdbc:mysql://192.168.0.123:3306/MyDatabase
(You need to replace your IP in the above string)
I'ts impossible to connect remotely without (IP) address
try this approach
if you want to connect it via internet :
OPEN CMD on your computer
in CMD write ping myuniversity.edu (for example ping google.com)
then you will get an ip address of the website and you can copy the ip
then try this approach :
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://THE IP ADDRESS :3306/DatabaseName");
System.out.println("CONNECTED");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
Ok so here's what I did to fix the issue:
In my.cnf file, I changed the bind-address from '127.0.0.1' to the
'host ipaddress'. This allows connecting to the remote mysql server
but would not allow access for any remote host trying to connect to
it.
To fix that, I added an entry in user table with host '%'. This allows remote hosts to connect to the database.
Now I can connect to the database with jdbc:mysql://serverIpAddress:3306/MyDatabase

Connection JDBC SQL Server Error

I've tryed to create a connection with a Microsoft SQl Server 2008 database through JDBC on Eclipse SDK. I've dowloaded JDBC driver from microsoft and I've installed it, then I've added at my System environment variables CLASSPATH the path of sqljdbc4.jar file. After icluding in the Eclipse project the jar file I've tryed to create the connection using:
String connectionUrl = "jdbc:sqlserver://localhost;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
but it doesn't works, and launch me this exception:
com.microsoft.sqlserver.jdbc.SQLServerException: TCP/IP connection
at host localhost, port 1433 failed. Error: "Connection refused:
connect. Verify connection properties and make sure an instance of SQL
Server is running on the host and is accepting TCP/IP connections at
the port. Be sure no firewall blocks connections at the port.
I'm working on a JRE 1.6 so a sqljdbc4 should work, and I've created a working ODBC, so the server is responding, and the error should be in java command or JDBC installation.
Can anyone help me?
At the risk of stating the obvious, this looks to me as if TCP connections haven't been enabled on SQL Server. You have to manually enable them, they don't come enabled by default.
There's an article on MSDN here which explains how to enable TCP protocols for SQL 2005/2008.
Following are the quick trouble shooters:
Try to connect to your server using external front end.
Check if your firewall blocks the connection to the port
Check to see if server is really up.
A Suggestion :
If you are using eclipse , you don't need to add the jar into CLASSPATH variable , you can just add it in library of your project to make it available at runtime

Connect to MySQL with JDBC over network

I am trying to connect to MySQL database over a network. I have installed MySQL, and the service is running on the default port. I have also install the SQL connector to the jar file and added java JDK to the server machine. I am able to connect to my local database using the code:
private String dbUrl = "jdbc:mysql://localhost/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";
But when I try and connect to it over the network, with the IP address (eg: 192.168.1.45):
private String dbUrl = "jdbc:mysql://192.168.1.45/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";
I get the connection error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Does anyone know what this issue is? Do I need to add a different address?
I have added the default port with the address but cannot get it to work.
Thanks for any help.
By default, MySQL doesnt allow remote access and only allow local access.
You will have to modify your /etc/mysql/my.cnf config (on Linux) with:
bind-address = 192.168.1.45 // Your Server IP address
# skip-networking // This should be commented .
See the whole procedure here.
Check the my.cnf [mysqld] settings for the parameters port, bind-address, socket, to make sure these aren't causing problems.
Check the files /etc/hosts, /etc/hosts.deny to make sure everything is ok.
Check firewall applications
Check to make sure whatever directory mysqld's sockets are have the appropriate permissions.
Check to make sure that security settings within the mysql database (user table) permit access from your remote host.
Make sure you can telnet OK to localhost 3306, 127.0.0.1 3306, and whatever other IP address your machine is configured to (use ifconfig to find out).
You can test the server setup using the MySQL Workbench or mysql client which will narrow down the problem. It's also sometimes useful to just see if the server's there:
telnet host 3306
It'll tell you the version number of the server and some other binary junk. Enough to know your host is listening.

Categories