Connecting Java to mysql from and on localhost

Discussion in 'Programming/Scripts' started by James A, Jan 19, 2015.

  1. James A

    James A Member HowtoForge Supporter

    Hi, I'm writing a Java program that needs to connect to a local mysql database on the same device. The connection works but the issue I have is that the mysql database sees the connection coming from the devices LAN ip rather than from localhost. I can see this through running show processlist where the user is XYZ-Interface in the example below.

    mysql> show processlist;
    +------+---------------+------------------+-----------+---------+------+-------+------------------+
    | Id | User | Host | db | Command | Time | State | Info |
    +------+---------------+------------------+-----------+---------+------+-------+------------------+
    | 6814 | XYZ-Interface | 10.10.1.61:60038 | XYZ | Sleep | 3 | | NULL |
    +------+---------------+------------------+-----------+---------+------+-------+------------------+

    To create the connection I'm simply calling the following connection class
    Code:
    class MySql {
      private static Connection conn;
      public static void connect() {
        try {
          Class.forName("com.mysql.jdbc.driver");
          conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/XYZ","XYZ-Interface","PASSWORD");
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    Is this simply something I'm doing wrong or is there a reason it can't be done?
    Thanks for any help.
     

Share This Page