Connecting to MySQL from Java to WAMP / MAMP Pro - java

I am trying to connect from java to a MySQL database from an external computer on a machine hosting WAMP and other hosting MAMP Pro. The connection to WAMP is fine but on MAMP Pro, I get the following error.
String userName = "user";
String password = "pass";
String url1="jdbc:mysql://<IP Address>:3306/dbname";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn1=DriverManager.getConnection (url1, userName, password);
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
I had changed the privellages in both the server by going to Privellages in PhpMyAdmin and chaning host to any host under Login Information. Worked for WAMP but didn't work for MAMP Pro.
Any suggestions?

I'm answering my own question but after some struggle I found the solution. Hopefully it will help people like me in future.
I opened MAMP Pro and on Server - General tab I clicked
File => Edit Template => MySql my.conf and changed
line no 37 from
bind-address=127.0.0.1 to bind-address = <my server's IP Address>

Related

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?

Connecting Java app from a PC to XAMPP server on another PC

I'm working on a personal project that includes a Java app connecting to a XAMPP SQL database. The app connects to the database perfectly on the same PC as the database is being hosted on. The program uses a singleton class.
Code:
private Connector() {
String host = "jdbc:mysql://localhost/my_database";
String user = "root";
String pass = "";
try {
connection = DriverManager.getConnection(host, user, pass);
System.out.println("Connection success!");
} catch (SQLException ex) {
System.out.println("Connection failed!");
}
}
I wanted to take a step further and be able to put the app on my laptop and connect to the database on my PC. I went into XAMPP and changed the "httpd-xampp.conf" file so that it allows local connections, and when I went on my laptop I tried opening phpMyAdmin from the browser and it worked!
Now I put the app on my laptop but instead of the host being:
String host = "jdbc:mysql://localhost/my_database";
I found the local IP of my PC and on my laptop changed the host to be:
String host = "jdbc:mysql://192.168.1.8/my_database";
The connection failed so I tried setting up a different user that allows any host to connect and has full access just in case. Still the same error happens.
I checked my firewall, but there was no entry that disables the connection, but anyways I was able to connect to phpMyAdmin through the browser through my laptop.
Here is the part I changed in the Xampp config file:
<Directory "N:/xampp/phpMyAdmin">
AllowOverride AuthConfig
Require all granted
ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</Directory>
You could try port forwarding the application / port on the PC just to be sure it's not the firewall as other than that, I don't see what would prevent the connection.
Here is a link on port forwarding on Windows if you need it.
Hope this helps

Not able to connect local instance of SQL Server 2008 in a Java program

I am using sqljdbc4.jar in the Build path of eclipse project.
I am trying to connect to SQL Server 2008 in a java program
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=sis;user=sample;password=sample;";
connection = DriverManager.getConnection(connectionUrl);
But I am getting the following error.
The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
I tried the following
Firewall off
Enabled TCP/IP in SQL Server configuration manager --> CLient protocols
SQL Server browser service is also running
Please let me know what other things should I try.
Thanks
Firewall off which way, this way??? open/enable port localhost:1433
run cmd and paste
netsh firewall set portopening protocol = TCP port = 1433
name = SQLPort mode = ENABLE scope = SUBNET profile = CURRENT
2/ change Sql Server authentication from
Windows authe... to Sql Server and Windows authentification (Mixed mode)
3) your version is SQL Server 2008 or SQL Server 2008 Express, if Express, then search on downloaded package (contains excelent help inc examples) how to connect Express on Localhost, there is difference localhost\sqlexpress
here and here and here
4) no idea how to connected, there are TCP/IP and Names Pipes too
EDIT:
5) in Management Studio you have to add new User (SQL autenthification) and assign to the decision Database (whatever or Model too) is there ??? aren't you

connection string for phpMyAdmin database

If I have a mysql database on my own pc, I would use the following cnxn string to connect it to my java program :
String url = "jdbc:mysql://localhost:port/dbname";
But if my database is made over phpMyAdmin, what would be the connection string ?
one other note, the phpmyadmin database is not on my local pc now, it is on another one but both pc are in the same network.
Thanks for any help ..
phpMyadmin is administration web application developed on PHP platform. phpMyadmin connects to MySQL database that might be on other machine.
you need to check config.inc.php file of phpMyadmin to check the host and port of mysql database.
Once you get the IP and port of the mysql database you can connect using connect string like
jdbc:mysql://<ipaddress>:<port>/<database_created_through_phpMyAdmin>
Replace localhost with the IP of the machine that hosts your database:
String url = "jdbc:mysql://<ip_goes_here>:port/dbname";
As Rutesh mentions, this may or may not be the same machine on which phpMyAdmin runs.

Connecting to hosted MySQL server with Java

I've been recently trying to connect to a hosted MySQL using Java but can't get it to work. I can connect to a local MySQL with localhost using:
connect = DriverManager.getConnection("jdbc:mysql://localhost/lego?"
+ "user=******&password=*******");
(Replacing the astrisks withmy username and password)
I can connect to the hosted MySQL database fine with PHP using:
mysql_connect('mysql.hosts.co.uk','******','**********');
mysql_select_db('test');
My problem is, I cannot connect via Java. I have an Exception which is caught if the connection doesn't work and this is always printed out.
Any ideas why it isn't working? Am I doing something wrong?
Thanks for your time,
InfinitiFizz
since it works in php (i guess you didn't try to connect from a local place with php???) it shouldn't be a port problem... but you should check that port 3306 is open... and ask the hosts company about that.
Have you noticed that in the DriverManager
http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html
you have:
getConnection(String url)
but also:
getConnection(String url, String user, String password)
Perhaps it would work better...
My guess is that you need to select a non-standard port, since I'd imagine the hosting server is serving lots of MySQL instances and they can't all use the normal one. I don't see selection of a port here.
If that's not it, perhaps there is a firewall issue somewhere along the way that's blocking the port or connection.

Categories

Resources