简体   繁体   中英

Why can't connect MySQL with Java in local network using XAMPP

I want to connect MySQL with Java in a local network (between 3 computers) using XAMPP. But I can't do that with Java and tried to connect the database with PHP application, then connected. I think this is not XAMPP error. It may be in Java app.

My code:

Connection getcon() throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    String URL = "jdbc:mysql://192.168.8.101/checker";
    String username = "root";
    String password = "";
    //Connection
    con = DriverManager.getConnection(URL, username, password);
    return con;
}

Exception:

run: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException MESSAGE: java.net.ConnectException: Connection timed out: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection timed out: connect at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:276) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666) at com.mysql.jdbc.Connection.(Connection.java:1531) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266) at java.sql.DriverManager.getConnection(DriverManager.java:661) at java.sql.DriverManager.getConnection(DriverManager.java:247) at connectionchecker.JDBC.getcon(JDBC.java:45) at connectionchecker.JDBC.getdata(JDBC.java:68) at connectionchecker.Check.incremntno(Check.java:28) at connectionchecker.Check.jButton1ActionPerformed(Check.java:84) at connectionchecker.Check.access$000(Check.java:15) at connectionchecker.Check$1.actionPerformed(Check.java:50) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton .java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6522) at javax.swing.JComponent.processMouseEvent(JComponent.java:3322) at java.awt.Component.processEvent(Component.java:6287) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4878) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4700) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4872) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4528) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4457) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.ja va:2724) at java.awt.Component.dispatchEvent(Component.java:4700) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:743) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:716) at java.awt.EventQueue$4.run(EventQueue.java:714) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:713) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:220) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:135) at java.awt .EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:123) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:119) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:111) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

** END NESTED EXCEPTION **

last packet sent to the server was 43 ms ago. my msg is conn ok java.lang.NullPointerException

JDBC class:

public class JDBC {

    private static JDBC j;
    Connection con = null;

    private JDBC() {

    }

    public static JDBC getJDBC() {
        if(j == null){
            j= new JDBC();
        }
        return j;
    }

      Connection getcon() throws Exception {

          try {
               Class.forName("com.mysql.jdbc.Driver");
        //String URL = "jdbc:mysql://127.0.0.1/checker";
        String URL = "jdbc:mysql://192.168.8.101:80/checker";
        String username = "root";
        String password = "";
        //Connection
        con = DriverManager.getConnection(URL, username, password);

          } catch (ClassNotFoundException | SQLException e) {
              System.out.println(e);
          }

      return con;
    }

    void setdata(String sql) {
        try {
             if (con == null) {
                getcon();
            }
            con.createStatement().executeUpdate(sql);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    ResultSet getdata(String sql) throws Exception {
        if (con == null) {
            getcon();
            System.out.println("my msg is conn ok");
        }else{
            System.out.println("my msg is conn not ok");
        }
        ResultSet rset = con.createStatement().executeQuery(sql);
        return rset;
    }
}

You need to pass the port number of the XAMPP after the ip address.

The default port number is 80 for XAMPP.

String URL = "jdbc:mysql://192.168.8.101:80/checker";

Or try this one below

String URL = "jdbc:mysql://192.168.8.101:8080/checker";

Problem lies here

String URL = "jdbc:mysql://192.168.8.101:80/checker";

Note, this should represent your db server and port where your db is running. Above url is wrong as, port 80 is default port number for Apache server. But, you need to use mysql port, which is 3306 (default).

Instead, you should try

String URL = "jdbc:mysql://192.168.8.101:3306/checker";

I'm assuming checker is database name.

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