This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 755
  • 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();
}

}
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


change the localhost to name of machine or ip address of machine.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 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
 
Greenhorn
Posts: 4
  • 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
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!