How to create a proper mysql connection - java

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

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?

Unable to connect Java application to SQL Server database

I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.
The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.
When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:
try{
String
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
Connection conn = DriverManager.getConnection(URL,"","");
System.out.println("connected");
}catch(Exception e){
System.out.println("Oops\n"+e);
}
What do I need to change to fix this?
You might need to reconfigure your connection string into this format.
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
HOST in this case is most likely to be "localhost" since you are connecting on a local machine.
DATABASE will be the name of your database
Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver

connecting java application with online hosted mysql database

I have build an application in java, application is one and will be used on 3 different systems,And therefore the database of that application must be online to keep all 3 applications with up to date database...
In starting I developed my application based on localhost (wampserver) and used database in "PhpMyAdmin", and hopefully application is fully developed and ready to run.. but the problem is online database connectivity!
I have uploaded my database on a Site in PhpMyAdmin and they provided below information:
and the for connecting my app to this DB is:
String url = "jdbc:mysql://fdb12.biz.nf:3306/";
String dbName = "1738412_wstore";
String driver = "com.mysql.jdbc.Driver";
String userName = "1738412_wstore";
String password = "Password";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
now when I run my application it shows below error:
I dont know what the problem is there, please help me out through this..
I faced the same issue and the following helped me to solve.
http://support.hostgator.com/articles/cpanel/how-to-connect-to-the-mysql-database-remotely
Hopefully, your hosting provider should have same type of cpanel to configure MySQL database for remote connections.
Check for firewall.
Check if mysql is running.
2 things you can try:
Install MySql client locally on your machine and connect like: mysql -h fdb12.biz.nf -u 1738412_wstore -P<password>
This should work before you try anything else.
Make sure you're using the right imports in your code. See [here][1].
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
I think your DB server doesn't allow remote connections.
Try hosting in:
http://www.freesqldatabase.com/freemysqldatabase/
It's free and allow remote DB connections.
Good Luck!!!
Considering the Biz.nf FAQ:
How can I connect to my MySQL/PostgreSQL database? What settings
should I use for my script/software?
MySQL/PosgreSQL database connection can be established with script/software hosted only on your
web hosting account (meaning no remote access is allowed due to
security reasons). The following settings are needed:
So, since biz.nf does not allow remote access, it will be difficult for you to interact with their MYSQL server for your application.
The best solution and the most simple is probably to find a new provider which will allow the remote access. Heroku.com works very well.
If you really want to use the biz.nf services, it will be more difficult.
It's not a script or credential problem.
Maybe you can (probably not), try to login by SSH to configure your server to enable remote access to your MYSQL database server.
From the free plan, you will not have any access to the online SSH tool and you will need to configure your domain for a SSH connection.
In SSH, and it probably will not work considering the FAQ, you could try to simply modify your /etc/mysql/my.cnf file by commenting the bind-address line wich by default only allows local access (127.0.0.1). Then, try to restart your MYSQL service with: a simple service mysql restart to verify if the remote connection works.
If the remote connection still doesn't work after this, the only way I can find for the moment (unless you completely change your hosting provider) would be to create a kind of API hosted directly on the server and your Java program could interacts with your services (In JSON, by example).

Setting up a connection to Online MySQL database

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.

Connecting to a MySQL database

I have created a MySQL database. I am building a GUI in java. How can I connect this Java software to the database?
First, you need to add the mysql jdbc driver jar to your project, then you would create a connection as follows:
String url = "jdbc:mysql://yourhost:port/dbname";
Class.forName ("com.mysql.jdbc.Driver"); // to load the driver
Connection conn = DriverManager.getConnection (url, userName, password);
Here yourhost is the name or ip address of the server, port is the port number to which the mysql server is bound, dbname is the name of your database.
using any of the miriad of Java/SQL API, so you can perform normal SQL queries. JDBC is a start
Start using JDBC, it's best for start. Take a look on some tutorials
http://www.tutorialspoint.com/jdbc/jdbc-quick-guide.htm
http://www.jdbc-tutorial.com/
this post has helped me quite alot!
I'm quite 'disapointed' and 'suprised' I suppose that Java doesn't have the fuctionatly of connected to online databases built in.
One thing that would make it better, and please tell me the followings true: the fact that users of my software don't have to do anything special like install any drivers?
I've already added one answer - asking a question but the comments/reply's I got seem to be a bit off topic and now I've commented back asking for the answer to my original question no one seems to be answering (Have a look for your self - see what I mean)
So, the orignal question :: The only thing I want to know;
Do the users of my software I made using JDBC have to do anything special regarding JDBC, or is just me who needs to have JDBC while writting the program?
PS Sorry if I'm a bit pushy or doing things that your not supposed to do - I'm new to this community!

Categories

Resources