• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

testCode runs GREAT on Local comp, fails on network comp. using mySQL

 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Below is my test code that runs perfect on my local computer (I call it the server becasue it has MySQL database).

When I run it on my servre computer - it's a success. but when I run it on a different computer (connected to the server computer with a network drive - Drive z) I get the following error:

unable to connect to any host due to exception:java.net.ConnectException: Connection refused: connect

Note: I setup the classpath for: mysql-connector-java-3.0.16-ga-bin.jar

Any ideas/???
Thank you all



import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestMysql implements ActionListener
{
protected JButton testButton = new JButton("Test db connection");
protected JTextArea jta = new JTextArea( );
protected JScrollPane jsp = new JScrollPane();
protected static int counter=0;

public TestMysql()
{
JFrame f = new JFrame("This is a test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 250);
Container content = f.getContentPane();
content.setLayout(new BorderLayout());
content.add("Center",jta);
content.add("South",testButton);
testButton.addActionListener(this);
f.setVisible(true);
jta.setBackground(Color.WHITE);
}


public void actionPerformed(ActionEvent e)
{

if(e.getSource()==testButton)
{
try{

jta.setText("");
jta.append("Try number "+ ++counter+"\n");
String driver = "com.mysql.jdbc.Driver";

jta.append( "=> loading driver:\n" );
Class.forName( driver ).newInstance();;
jta.append( "OK\n" );

/* Test the connection */

String url = "jdbc:mysql://localhost/test";

jta.append( "=> connecting:\n" );
DriverManager.getConnection( url, "root", "cardinal" );
jta.append( "OK\n" );
jta.append("** SUCCESS ** \n");

}
catch (SQLException sqle)
{
Component c = SwingUtilities.getRoot((Component)e.getSource());
JFrame frame = (JFrame)c;
JOptionPane.showMessageDialog(frame,"SQL EXCEPTION\nmessage: "+sqle.getCause(), "SQL EXCEPTION", JOptionPane.PLAIN_MESSAGE);
}
catch( Exception ee )
{
Component c = SwingUtilities.getRoot((Component)e.getSource());
JFrame frame = (JFrame)c;
JOptionPane.showMessageDialog(frame, "Error Genral Exception","Err in establishing connection to db", JOptionPane.PLAIN_MESSAGE);
}


}
}


public static void main(String args[])
{

new TestMysql();
}

}
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


change the localhost to name of machine or ip address of machine.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you granted permission to the user to connect from a remote machine? By default MySQL only allows local connections.

Although looking at the error you reported, it may be the reason above, or if your JDBC conection was correct it mat be a network error.

As always, I recommend the SquirrelSQL client for debugging and testing JDBC details before moving them to you application.

Dave
 
Boyet Alcudia
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>change the localhost to name of machine or ip address of machine.


I am very newbie in this field...how can i check the name of machine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic