Connect to remote mySQL(cPanel,phpMyAdmin) using Netbeans - java

I have created MySQL database for my website using cPanel and phpMyAdmin on remote host and now i want to connect to this database my simple java program.I try this code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
public static void main(String[] args) {
try{
Connection con=DriverManager.getConnection("jdbc:mysql://sfg.ge/sfgge_mysql", "my_username", "my_password");
Statement state=(Statement) con.createStatement();
String name="shota";
int id=3;
String insert = "INSERT INTO student VALUES ("+ id +",'"+ name +"')";
state.executeUpdate(insert);
}
catch(Exception ex){
}
}
}
In this code
DriverManager.getConnection(String url, username, pass)
SQLException throws such error:
null, message from server:
"Host '188.129.228.176' is not allowed to connect to this MySQL server"
Any suggestions appreciated.

Add new account:
'user1'#'exact_host' - host is specified
CREATE USER 'user1'#'exact_host'
or
'user1'#'%' - for any host.
CREATE USER 'user1'#'%'
Grant privileges you need and connect with new account.
Also look at this:
Make sure that the server has not been configured to ignore network connections or (if you are attempting to connect remotely) that it has not been configured to listen only locally on its network interfaces. If the server was started with --skip-networking, it will not accept TCP/IP connections at all. If the server was started with --bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections

Related

How can I share my database using IP Address?

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class Database {
public static Connection con = null;
public static Connection connectDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/registerdb", "root", "");
//JOptionPane.showMessageDialog(null, "Connection Successful");
return con;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
How I make my finish program share to others locally or using the internet and how to use the IP address for it? I'm using NetBeans for my java programming and XAMPP for my database.
Please help me.. THanks
Install db server on any computer, get servers IP address and port and pass that information to DriverManager.getConnection("jdbc:mysql://IP:port/dbname")
Its best to use property file to store IP and port and read it when creating connection so it can be easily changed without changing the code.
Your program should work as it is on multiple machines as long as they are in the same local network (as you are using local IP address).
If they are not able to connect to DB, following can be the possible reasons:
DB is not configured to allow connection from another machine. You
can grant it using following syntax:
grant all on db-name.* to username#'%' identified by 'password'
Your firewall is blocking income 3306 port requests. Open the port
for incoming requests.

Connect Java remotely to MYSQL DB on another network

I want to write a java application that uses a mysql database to store and retrieve information. I am still just a beginner and I do not have a lot of knowledge on web hosting providers and server architecture. In this application, several clients will have to access this remote database located on a server machine maybe.
Currently, I can connect to the database from the same computer that runs the mysql server (I am using workbench by the way). Any suggestions on what should I do?
public static Connection openDatabase()
{
Connection myConn;
try {
myConn =
DriverManager.getConnection(Configs.getProperty("DatabaseURL"));
return myConn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
The url is:
jdbc:mysql://Atomic-PC:3306/test?autoReconnect=true&rewriteBatchedStatements=true&useSSL=false&user=root&password=password
First of all you have to find connection URL, port and credential from MySql workbench you are using. If hosting provider provides a public IP for DB server you should be able to access it from anywhere by using following code in Java.
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] argv) throws Exception {
String driver = "com.mysql.jdbc.Driver";
String connection = "jdbc:mysql://localhost:3306/YourDBName";
String user = "root";
String password = "root";
Class.forName(driver);
Connection con = DriverManager.getConnection(connection, user, password);
if (!con.isClosed()) {
con.close();
}
}
}
For this reason you have to import mysql connection driver to your application first.
If you are trying to connect to remote database then yo need to change the database url from localhost to remote server ip address.
jdbc:mysql://Atomic-PC:3306/test
to
jdbc:mysql://<db-server-ip-address>:<db-server-port>/<db-name>
Assuming, remote server ip address is 10.234.05.123 and database port number is 3300 and database name is remoteDB. Then,
jdbc:mysql://10.234.05.123:3300/remoteDB
your web hosting company should be able to provide you with the url:port which you can use in your connection string to connect to MySQL(or any other remote db for that matter)

Connecting a remote database that is behind a firewall via SSH

I am trying to write some Java code that connects to a remote database from my personal computer. I can currently ssh to these machines using a private/public key authorisation. I can access the GUI of these machines by creating SSH tunnels (with Putty).
I don't think the problems is with my code as I can create a DB using MySQL Workbench and can successfully query the db. When trying to access the db I use the same tunnell address that I use for the GUI. So my questions is down to how should I connect to the DB? Can anyone shed some light to my question? My code is posted below.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLConnector {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//Connection URL Syntax: "jdbc:mysql://ipaddress:portnumber/db_name"
String dbUrl = "jdbc:mysql://localhost:9092/db";
//Database Username
String username = "root";
//Database Password
String password = "root";
//Query to Execute
String query = "select * from employee;";
//Load mysql jdbc driver
Class.forName("com.mysql.jdbc.Driver");
//Create Connection to DB
Connection con = DriverManager.getConnection(dbUrl,username,password);
//Create Statement Object
Statement stmt = con.createStatement();
// Execute the SQL Query. Store results in ResultSet
ResultSet rs= stmt.executeQuery(query);
// While Loop to iterate through all data and print results
while (rs.next()){
String myName = rs.getString(1);
String myAge = rs.getString(2);
System. out.println(myName+" "+myAge);
}
// closing DB Connection
con.close();
}
}
The error I get is:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
This should probably be a comment, but my reputation doesnt allow for comments. I would venture to guess that the port on the server's network is blocking that port (whatever port you're using) but is allowing for ssh. What is this server/where/can you change the settings on any firewall to allow for connection?
If I get you right, the mysql is listening at port 9092 at the host you are connecting with ssh.
Then use this ssh command to connect:
ssh -L9092:localhost:9092 username#192.168.1.233
It creates a tcp listening socket at your local machine and tunnel any connections to port 9092 at local machine to the port 9092 at remote machine
Edit:
I missed that you are using putty. Then open connection settings dialog, then browse to ssh and find port forwarding. the notation is nearly the same: local port, to some ip:port at remote site: here you can find a step-by-step guide with screenshots

How to access database in cPanel from home computer

I have created a database in cpanel using MySQL® Database Wizard. I have created a java class to access the database. For remote access I've added my IP to Remote MySQL® allow section & I have also allowed all privileges to a specific username with a password. Keeping all that settings, from my home computer I still can not access the database. I am running this java application in NetBeans. As the errors say:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The source code goes like this:
package remoteserverconnection;
import java.sql.Connection;
import java.sql.DriverManager;
public class RemoteServerConnection {
public static void main(String[] args) {
Connection conn = null;
try
{
String url = "jdbc:mysql://domainIP:3306/DBNamne";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"UserName","password");
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Am I doing it in wrong way? Or is there any other way to connect that database from home pc?
It's possible that your host (judging from cPanel in the title) has a firewall rule set up that may be blocking access. That's very likely the case if it's a shared host which is the sort of service that typically uses cPanel.

is it possible to connect java application to online database

hello i am trying to connect my application to online database using the following code
import javax.swing.*;
import java.sql.*;
public class javacon {
Connection con = null;
private static final String url ="jdbc:mysql:\\sql204.byethost9.com:3306\\";
private static final String dbName = "b9_16134488_db";
private static final String userName = "b9_16134488";
private static final String password = "123.321";
public static Connection connectDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://sql204.byethost9.com:3306/b9_16134488_db/",userName,password);
JOptionPane.showMessageDialog(null, "connection is succseful");
return con;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
but i am getting error says the driver has not received any packet
from the server
i found one answer this one says i have to go to my cpanel in my host and add my ip address there but i need every user to be connect to the database to login and logout
Mysql database has file my.cnf where bind-address might be specified. If that is not there, then mysql bind itself to all interfaces, otherwise only to specified one. More about this Bind-address
Also I guess user must have permissions to connect to database. 'user'#'ipaddress'. And more you can read here Connection access
Your code seems fine, your problem probably is related with your MySQL database not allowing connections from your host. The easiest way to find if this is the problem is trying to connect to the database from the same machine with the same credentials with MySQL Workbench.
There are three main things that can block your MySQL from receiving external connections:
1) Configuration
Edit my.cnf removing the following line:
bind-address = 127.0.0.1
2) User Host
In MySQL all users have defined from which host the connection can home from. 'user'#'%' would allow you to connect from any host.
3) Firewall
Make sure that the port 3306 is open.

Categories

Resources