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

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

Related

Cannot connect to SQL Server from Java

I have a problem when I try to connect to SQL Server with Java. I have the next error:
The connection to the host DESKTOP-C0SCI39, named instance 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.
My firewall is off and I don't know what is wrong(mssql-jdbc-8.4.1.jre14 is included). This is my Java code:
String url = "jdbc:sqlserver://DESKTOP-C0SCI39\\;databaseName=students";
try {
Connection connection = DriverManager.getConnection(url);
System.out.println("Connected to MS SQL");
} catch (SQLException throwables) {
System.out.println("Error!");
throwables.printStackTrace();
}
Try this solution, I have ran into similar problems in the past and have found that these help.
Check that the JDBC Driver is imported in the project. (Preferred - 5.1.48)
Check that your SQL Server is up and running (Check if SQL Service is running)
Check if you entered your connection string properly
eg: private String URL = "jdbc:mysql://localhost:3306/dbName"; //database url
Try adding a useSSL=false to the end of your connection string dbName?useSSL=false
Open your server with a server management software such as SQL Server Management Studio to see if your SQL Server is working as it should
Your database is not running where you think it is running. Verify the port and location of the server and try again.

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?

Connect to remote MySQL server using Java

I'm writing a program to push stuff into a remote database. My database is stored on a redhat server, while my program is written on a windows machine.
I dont want to give out my server addresses but lets say my Linux server is xx.xx.xx.xx8
MySQL Workbench in windows says that my mysql server host is 127.0.0.1:3306
I know there are a million similar questions but each one is pretty unique to the situation.
I've been using http://www.vogella.com/tutorials/MySQLJava/article.html#javaconnection as a guide but this looks like a local connection.
I have also been referencing this but it confuses me.
Here's a mock up of some code i think may work:
Connection connection = null;
String dburl = "jdbc:mysql://127.0.0.1:3306/Db_Name";
String userName = "user";
String passWord = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(dburl, userName, passWord);
Statement st = connection.createStatement();
String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
int rsI = st.executeUpdate(query);
System.out.println("Hi");
}catch (Exception e) {
System.out.println(e);
} finally {
if (connection != null) {
try {
connection.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */ }
}
}
that is based on the link i provided. All i changed was the address. I have no idea where the server address xx.xx.xx.xx8 should go.
In order to connect to the remote database you need to know the external ( or called public ) ip address of the server.
String dburl = "jdbc:mysql://85.65.85.222:3306/Db_Name"
Even if your database on server says it listens on localhost:3306, you can still connect to it using public ip with correct port. (providing you have sufficient access rights to the server)
Sometimes there may be situation that even if you know the remote server IP address you still won't be able to connect to remote database directly perhaps due to blocked port or other ACL issues ( and this is ofc applicable if you have no control over this). You could however create ssh tunnel to that server and create a port forward.
JSch jsch = new JSch();
Session session = jsch.getSession(user, host);
session.setPassword(password);
session.connect(timeout);
session.setPortForwardingL(listenPort, destHost, destPort);
example ssh connnection could look something like this:
ssh user#85.65.85.222 -Llocalhost:5050:127.0.0.1:3306 <- this will create a ssh tunnel with port forwarding, meaning on your local machine where you have your code and whole environment you will still be able to connect to remote database using
String dburl = "jdbc:mysql://localhost:5050/dbname"
Just to clarify for anyone looking at this later...
localhost is always the machine you are sitting at - always. There is no way you will ever connect to a remote box using localhost. Also, the IP address 127.0.0.1 is equivalent to localhost, so don't use that either.
You may see that the database binds to localhost or 127.0.0.1 on the database server. That is ok. Use the IP address of the server running the database in your database connection URL
mysql:jdbc://<IPOfServer>:<PortOfServer>/<DbName>).
You can find the IP address of the server by running the following command on the database server...
Windows: ipconfig
Linux: ifconfig
Good luck!
You can see what documentation says:
The method DriverManager.getConnection establishes a database
connection. This method requires a database URL, which varies
depending on your DBMS. The following are some examples of database
URLs:
MySQL: jdbc:mysql://localhost:3306/, where localhost is the name of
the server hosting your database, and 3306 is the port number
So, for MySQL Connector/J Database URL
The following is the database connection URL syntax for MySQL Connector/J:
jdbc:mysql://[host][,failoverhost...]
[:port]/[database]
[?propertyName1][=propertyValue1]
[&propertyName2][=propertyValue2]...
Where:
host:port is the host name and port number of the computer hosting your database. If not specified, the default values of host and port are 127.0.0.1 and 3306, respectively. So, every operation you do will be applied on this post
database is the name of the database to connect to. If not specified, a connection is made with no default database.
failover is the name of a standby database (MySQL Connector/J supports failover).
propertyName=propertyValue represents an optional, ampersand-separated list of properties. These attributes enable you to instruct MySQL Connector/J to perform various tasks.
Update: if you have a remote databases (your remote server) that you want to access to, you should have for example:
String dburl = "jdbc:mysql://<REMOTE_HOST>:3306/Db_Name"; //replace with your remote host
... // here all the code related to your remote host
Note: if you remote server has in its configuration "localhost" you have to figure out its public ip address by executing ifconfig. Then use its ip address on your string. As important note your remote server must be configured to allow remote connections.
if you connect to a server (ie. with the ip xx.xx.xx.xx8) you specify through the port (which is usually 3306) that you want to connect to the databaseserver on that machine. so simply use:
String dburl = "jdbc:mysql://xx.xx.xx.xx8:3306/Db_Name";
as your connectionstring.
If you dont know the port, mysql is running, have a look in your /etc/mysql/my.cnf file. There should be the port specified.
Also have a look at your firewallrules if you can't connect and make sure you enabled remoteconnections (here is a good tutorial to enable it: http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html).

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"

Unable to connect to MySQL after deploying my application

I have installed MySQL to my local computer. I can access MySQL from my Java Application which is running on my computer.
My connection string is private String url = "jdbc:mysql://localhost:3306/mydatabase" which allows me to connect successfully. But when I deploy my application to other computers on my LAN and try to connect to my MySQL databases from the other computers I can't access my database.
As others have mentioned in the comments, you issue is your connection string: private String url = "jdbc:mysql://localhost:3306/mydatabase"
In order for you to be able to connect to your database from other machines on your LAN you will need to change localhost to your IP address. For example:
private String url = "jdbc:mysql://192.168.0.10:3306/mydatabase"
Providing that the other machines can see 192.168.0.10 they will be able to connect (with the right credentials of course!)

Categories

Resources