File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes MySql in not accessing remotely Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySql in not accessing remotely" Watch "MySql in not accessing remotely" New topic
Author

MySql in not accessing remotely

Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
Hi
I am trying to connect mysql remotely through jdbc but the connection is not happening.
steps followed:---
1>Installed mysql in one computer(ip address 192.168.0.101)
2>open mysql and run this command
"grant all on *.* to root@'%' identified by 'abhra' with grant option;"
where abhra is the password

3>jdbc code---
static Connection connection = null;
static Statement statement = null;

public static void main(String[] s) {
String createTableSql = "CREATE TABLE REGISTRATION202 " +
"(id INTEGER not NULL, " +
" first VARCHAR(255), " +
" last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
System.out.println("JDBC TEST CONNECTION");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Required jdbc class have not found");
}
System.out.println("MYSQL JDBC Driver registered");
try {
connection = DriverManager.getConnection(
"jdbc:mysql://192.168.0.101:3306/testdb", "root", "abhra");
} catch (SQLException sqe) {
System.out.println("Connection failed! Check output consol");
}
try {
statement = connection.createStatement();
statement.executeUpdate(createTableSql);
}
.......................................................................................................


Output:----

JDBC TEST CONNECTION
MYSQL JDBC Driver registered
Connection failed! Check output consol

windows firewall is disabled in both mysql containing computer(192.168.0.101) and jdbc code containing computer(192.168.0.100)
From 192.168.0.100 ping result is successful for 192.168.0.101 and "testdb" is an existing database.

Please help me to find out the problem.

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Why don't you print the entire stack trace? Your connection fails, but the exact reason isn't clear. It could be a networking issue, or a rights issue, but without the error message we can't tell.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5


You will not find any extra info on the exception as you are not doing anything with it here. At the very least, add the line

to this catch block.
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
Hi after putting the stack trace I have got this error messages------

JDBC TEST CONNECTION
MYSQL JDBC Driver registered
Connection failed! Check output consol
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)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.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:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.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:416)
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 jdbctest.JdbcExample.main(JdbcExample.java:33)
Caused by: java.net.ConnectException: Connection timed out: 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.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 15 more

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
Check the "bind-address" and "skip-networking" settings in your MySql config file: http://endpoint.co/technology/enable-remote-access-mysql


Ping & DNS - my free Android networking tools app
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
Hi,
may be the mysql-connector wasn't added properly in the build path , the program is executing properly now.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySql in not accessing remotely
 
Similar Threads
Unable to register Derby driver
JDBC connection
MySql connection error
connection problem
Problems with .sql(script files) and .properties files. [Using Netbeans]