connect database to another computer - java

I have two computers on one network. I have entered the ip of another computer, it works fine in the browser, but when using this ip on java I found the database connected to my localhost database not from the other computer!
my code jdbc
public Connection MakeConnect() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:http://192.168.1.109:5560/isqlplus", "school",
"sch"); // first : user(hr) second pass(hr) .!
return connection ; // return connetion of database

I think your JDBC connection string may be in a wrong format. You should provide this in the following way:
jdbc:oracle:thin:#[HOST][:PORT]:SID
jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE
Instead it looks you specified ISQLPLus link. I haven't tested it but my guts say me it is the reason. I think in your case it should be something like that:
jdbc:oracle:thin:#192.168.1.109/SERVICEorSID
And you have to find the service name by yourself. In a generic case it is a name of the database. By default it is usually set to ORCL but possibly you set it to another name.
Make sure you read a following document: http://www.orafaq.com/wiki/JDBC

If the connection succeeds without error and if this 192.168.1.109 ip address is not of your local machine then it must connect to some other machine.
What made you think it's connecting to the local DB instance?

Check firewall settings of the other computer. Sometimes it may not allows for the external connections

If you want to allow a specific client ip-address (for example: 192.168.1.4) to access the mysql database running on a server, you should execute the following command on the server that is running the mysql database.
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root#'192.168.1.4' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Also, update firewall rules to make sure port# 3306 is open on the server that is running the mysql database.

Related

replacing localhost by ip address in connection string

public Connection conn() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
{
Connection all_connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
all_connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/dbname","username","password");
return all_connection;
}
Above code is my connection code. Its working fine. If i replace localhost by my ip address, its not working. Actually i want to replace localhost by ip address. Replacing localhost by 127.0.0.1 works fine. Why replacing ip address is not working????
This is probably an issue with your database user. (I can't tell for sure because you didn't post the actual error)
You'll need to verify that the user you're logging in as is allowed to access is allowed to access the database from an external IP. It seems likely that you're using the root user, which is allowed access from localhost, 127.0.0.1, and ::1, but not from external IPs.
If you are, make sure you set up a new DB user with only the required access, and allow access from the specific IP, or % (any host)
I have faced same issue while I am new to this.
IP address means other DB server's IP ?
If yes then check firewall that is it allow Incoming connection for 3306 port ?
If it is not then ad new rule in that server machine with 3306 connection post allow.
and which OS in server machine ?
I have done like following in postgresql :
jdbc:postgresql://192.168.1.9:5432/dbName
In your case it would be :
jdbc:mysql://192.168.1.9:3306/dbName
I suggest you to first of all check that machine's firewall port rules then go for next.
And be sure that you have written true user name and password.
First check whether your machine is configured for the IP address you are trying.
Try to ping it from other machine.
Try to nslookup for the same IP address.
If both the results are positive, means that your machine is configured for the IP address,
If any one fails, try to configure your ip, and then try again.
Connection object = DriverManager.getConnection("jdbc:mysql://127.xx.xx.x:3306/dbname","username","password");
use above code after configuration.
By default in some database server remote access is disabled. You have to enable remote access.
If you are using mySQL here is the solution.
1.Go to my sql bin folder or add it to PATH
2.Login to root by mysql -uroot -proot (or whatever the root password is.)
3.On success you will get mysql>
4.Provide grant access all for that user.
query to enable access.
GRANT ALL PRIVILEGES ON *.* TO 'username'#'IP' IDENTIFIED BY 'password';
Here IP is IP address for which you want to allow remote access, if we put % any IP address can access remotely.

MySQL not able to connect from remote client

I have my MySQL server running on machine with IP address 181.2.3.5, and I have a database called affablebean running on it. I am able to connect via from same machine. But when I try to access it remotely to deploy java application but I am getting below error:
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Access denied for user 'root'#'181.2.3.4' (using password: YES)
Both systems are on LAN. I tried below queries to grant privellages:
GRANT ALL ON *.* to 'root'#'%' IDENTIFIED BY 'sql123';
flush privileges;
also below thing:
GRANT ALL ON *.* to 'root'#'181.2.3.4' IDENTIFIED BY 'sql123';
flush privileges;
But still I am getting same error, I tried restarting mysql service but still it's not accepting connections.
Btw Server is running Windows XP and client is running on Windows 7. I also tried below query:
mysql> use mysql;
mysql> SELECT host FROM user WHERE user = 'root';
it returns my ip address correctly. But still same error. Can anyone guide me? I tried above solutions from various SO questions.
Note: Above IP address and user name are just a place holder to illustrate.
Edit: I am able to access from Workbench, but I am not able to access from Java program.
You have to specify the port name with your server IP.
Like hostIP:portNumber
Workbench and your java programs are like any other clients. So what you need to see if there are any settings difference in between these two clients, maybe wrong credentials.

Connecting to a MySQL Database with Java

I want to connect to my MySQL database with Java.
I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)
The only problem is that I keep getting an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last
packet sent successfully to the server was 0 milliseconds ago.
Here is my code:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password");
I am not positive how to compose the URL (and where I get my username and password) but I have done A LOT of research.
I am the only person with acess to my database and domain, so there's no use asking the admin.
I use phpMyAdmin to create the database(s) and manage them. Do I use my phpMyAdmin username and password or what?
By the way, my site is hosted on Yahoo! Small Business.
So my questions are:
How do I make the connection URL?
What is my username and password?
I would say you are missing a forward slash on your URL.
Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password");
Or I have a feeling that there is something wrong with your access privileges. This same thing happened to me also and it was a problem of Firewall blocking the port on the server. So verify this is not the case.
How do I make the connection URL?
Are you missing a forward slash in your URL? I would've assumed it would be something like:
jdbc:mysql://server/database
Load the drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
connect with the data base
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");
.....jdbc:mysql://this is use as it is
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of "127.0.0.1"
.....dbuser is the data base user
.....dbpassword is the password of the dbuser
.....3306 is the default port used for database

Trying to connect to mysql database with jdbc

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

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