简体   繁体   中英

Jdbc - Connect remote Mysql Database error

[SOLVED]

I finally got solve the problem, is everything alright with the connection...

The problem was in a SQL Query, i was doing a select in table with name "arduinoData" and the really name is "arduinodata".

That way works locally, but when i tried online, it doesn't.

Because this, the return was 'null', and Java was trying to parsetoInt that and was showing error.

I'm so sorry for trouble, thank you to all.


I'm using JDBC to connect my program to a MySQL database.

I already put the port number and yes, my database have permission to access.

When i use localhost work perfectly, but when i try connect to a remote MySQL database, show this error on console.

java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at serial.BDArduino.<clinit>(BDArduino.java:25)
Exception in thread "main" Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo)

Thank you in Advance !

Note: This was originally a comment but got too long. It seems people see NFE and guess it's port that's null. Which is misleading. Here is a WORKING code showing port is not necessary. Please avoid misleading answers.

        try {
          Class.forName("com.mysql.jdbc.Driver");
          String sourceURL = "jdbc:mysql://localhost/sodb";

          Connection conn = DriverManager.getConnection(sourceURL, "naishe", "p2ssword");
          Statement st = conn.createStatement();
          ResultSet rs = st.executeQuery("select count(*) from test_tab");
          while(rs.next()){
              System.out.println(rs.getString(1));
          }
        } catch(ClassNotFoundException cnfe) {
          System.err.println(cnfe);
        } catch(SQLException sqle) {
          System.err.println(sqle);
        }

It prints 3 (which is number of records in the table test_tab) correctly.


If you are using standard driver , you do not need port , it defaults to 3306! See here http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html

JDBC URL Format

The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

jdbc:mysql://[host][,failoverhost...] [:port] /[database] » [?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

I think you have missed port number in your connection string.

static String url = "jdbc:mysql://mysql.mydomain.com.br:3306/myDB";

Instead of

static String url = "jdbc:mysql://mysql.mydomain.com.br/myDB";

Now if getting same error mean you might replace mysql.mydomain.com.br with IP address as follows:

Just supply the IP / hostname of the remote machine in your database connection string, instead of mysql.mydomain.com.br. For example:

jdbc:mysql://172.16.15.225:3306/yourdatabase

Also check there is no firewall blocking the access to port 3306.

Make sure the user you are connecting with is allowed to connect from this particular hostname. For development environments it is safe to do this by

'username'@'%'

This is for grant permission for users GRANT manual.

您应该在connection url as 3306使用port numbers connection url as 3306 ,如果没有授予用户访问数据库权限的权限,请确保该用户具有该权限,您应该授予该用户访问远程数据库的权限,请参考链接

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM