0

I want to run my .jar file in another computer that does not have MYSQL or netbeans installed. In my computer it is working fine. I am using wamp MYSQL database. I have tried it on another machine and it says:

com.mysql.jbbc.exceptions.jdbc4.communicationsException: communication link failure.I am trying this to see if it would work for other people who do not have IDE or WAMP installed if I gave them my app.

Here is my sample code:

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection   con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"
                + "employee_certificate","admin","jkvf");
        String sql="select * from credentials where username=? and password=?";
        stmt= con.prepareStatement(sql);
        stmt.setString(1,username.getText());
        stmt.setString(2,password.getText());
        rs=stmt.executeQuery();

        if(rs.next()){
            //JOptionPane.showMessageDialog(null, "username and pasword correct");
            JfrmInsertUpdateSearch s =new JfrmInsertUpdateSearch();
            s.setVisible(true);
            setVisible(false);

        }
        else{JOptionPane.showMessageDialog(null, "username and pasword incorrect");}
    }
    catch(Exception e){JOptionPane.showMessageDialog(null,e);}
}                                        
Elated Coder
  • 312
  • 1
  • 16

1 Answers1

0

Your connection URL is pointing to localhost which means that you are connecting to a database server running on teh same machine as your jar file, on the other server where you run your jar file you should point it to your MySQL server using its hostname or its IP address.

Usually DJBC configuration is read from an external file (properties or yaml) and not hard coded in your program.

Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49