File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes testCode runs GREAT on Local comp, fails on network comp. using mySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "testCode runs GREAT on Local comp, fails on network comp. using mySQL" Watch "testCode runs GREAT on Local comp, fails on network comp. using mySQL" New topic
Author

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

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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

Joined: Aug 13, 2004
Posts: 1081



change the localhost to name of machine or ip address of machine.


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 15, 2001
Posts: 4
>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


Boyet Alcudia<p>IBM CS RPG Programmer <br />IBM CS RPG Developer <br />SCJP<br />IBM VAJ-AD <br />IBM CS WAS Std. Ed. V3.5
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: testCode runs GREAT on Local comp, fails on network comp. using mySQL