Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC connection

 
Tolu Ajayi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to establish a connection to mysql data base using my system ip address and I get the error below.


import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JDBCExample {

public static void main(String[] argv) {

System.out.println("-------- MySQL JDBC Connection Testing ------------");

try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}

System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;

try {
connection = DriverManager
.getConnection("jdbc:mysql://169.254.2.23:3306/hospital","root", "");

} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}

if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}






-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
Connection Failed! Check output console
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fai
lure

The last packet sent successfully to the server was 0 milliseconds ago. The driv
er has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1
117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2
498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:317)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at JDBCExample.main(JDBCExample.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:208)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.ja
va:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 15 more
Press any key to continue . . .
 
Nam Ha Minh
Ranch Hand
Posts: 504
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
Connection Failed! Check output console
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fai
lure


First, make sure you can ping the IP address of the server from your computer.
I think there is no connection, or the server is not accessible from your computer.
Also check firewall blocking port 3306.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Is this question connected to your earlier topic?

The Web start technology uses HTTP protocol to download application to the client computer. HTTP protocol is much more probable to come through various firewall, while connections to database servers are usually much more restricted. Opening the access to your MySQL database to all users of your application might be problematic (especially if the users are further away than on a local network), because it generally allows these people to connect directly to MySQL, which in turn means you need to pay much more attention to the security of your database.

The usual solution these days is to use a three-tier architecture. You'd have an application server, which would communicate with the database using the JDBC protocol and with clients using web services (these run on the HTTP/HTTPS protocols and can therefore get through firewalls well). If you have a two tier application now, it would certainly mean big architectural overhaul, though.
 
Tolu Ajayi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:Welcome to the Ranch!

Is this question connected to your earlier topic?

The Web start technology uses HTTP protocol to download application to the client computer. HTTP protocol is much more probable to come through various firewall, while connections to database servers are usually much more restricted. Opening the access to your MySQL database to all users of your application might be problematic (especially if the users are further away than on a local network), because it generally allows these people to connect directly to MySQL, which in turn means you need to pay much more attention to the security of your database.

The usual solution these days is to use a three-tier architecture. You'd have an application server, which would communicate with the database using the JDBC protocol and with clients using web services (these run on the HTTP/HTTPS protocols and can therefore get through firewalls well). If you have a two tier application now, it would certainly mean big architectural overhaul, though.


Yes it is connected to my earlier question.The project is actually my final year project in school and what I want is just to be able to connect about 2 or 3 systems for testing. What I don't know is if 127.0.0.1 can connect my database to a client's system or I must use my system's IP address.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
127.0.0.1 always refers to the local computer. You need to use a "real" IP to allow other computers to connect to your computer. You also need to make sure your computer is reachable from them - see Nam Ha Minh's post.
 
Tolu Ajayi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:127.0.0.1 always refers to the local computer. You need to use a "real" IP to allow other computers to connect to your computer. You also need to make sure your computer is reachable from them - see Nam Ha Minh's post.


Thanks, my IP address can be pinged from other computers so I think the problem is firewall blocking port 3306, do you have any idea on how I can remove this
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most operating systems have a built in firewall nowadays. In Windows it can be managed via Control Panel/Windows Firewall. It is also possible that you've got a separate firewall installed; many antivirus solutions come with their own firewall.
 
Tolu Ajayi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:Most operating systems have a built in firewall nowadays. In Windows it can be managed via Control Panel/Windows Firewall. It is also possible that you've got a separate firewall installed; many antivirus solutions come with their own firewall.



Thank you but I also want to be sure my connection URL is right:

try {
connection = DriverManager
.getConnection("jdbc:mysql://169.254.2.23:3306/hospital","root", ""); where 169.254.2.23 is my IP
 
Tolu Ajayi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tolu ajayi wrote:
Martin Vajsar wrote:Most operating systems have a built in firewall nowadays. In Windows it can be managed via Control Panel/Windows Firewall. It is also possible that you've got a separate firewall installed; many antivirus solutions come with their own firewall.



Thank you but I also want to be sure my connection URL is right:

try {
connection = DriverManager
.getConnection("jdbc:mysql://169.254.2.23:3306/hospital","root", ""); where 169.254.2.23 is my IP


My windows firewall and antivirus are disabled already and it still does not work or could it be that I am using root as a user which is set to connect via local host only.
 
Tolu Ajayi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tolu Ajayi wrote:
tolu ajayi wrote:
Martin Vajsar wrote:Most operating systems have a built in firewall nowadays. In Windows it can be managed via Control Panel/Windows Firewall. It is also possible that you've got a separate firewall installed; many antivirus solutions come with their own firewall.



Thank you but I also want to be sure my connection URL is right:

try {
connection = DriverManager
.getConnection("jdbc:mysql://169.254.2.23:3306/hospital","root", ""); where 169.254.2.23 is my IP


My windows firewall and antivirus are disabled already and it still does not work or could it be that I am using root as a user which is set to connect via local host only.



I was able to fix it by changing the host of my database user under xampp from localhost to anyhost.....................Happy days
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic