• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RMI application connection issue

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot resolve ConnectException i'm having with an RMI application I'm working on. The client application kept giving a phrase like "connection refused to host:127.0.0.1....ConnectException..after taking the appropriate steps such as
(1) starting the RMI registry
(2) starting the server application
(3) starting the client application to connect it with the server application.

I want to build and test the application first on my local system before doing anything further. Both Server and client programs reside in one computer and in the same folder. The OS is windows 7. what prompted my posting this is because i've tried the options i'm aware of like:
(1) referencing the system variable java.rmi.server.hostname when running the server application
(2) Editing the \host folder in my OS

I kept recieving the same error message from the client application. I'll appreciate is someone could give advice on the way out. There is no issue with compiling both programs: This application relies on the client calling the server program to send the data entered on the client side to a database:

The client app is shown below:

mport javax.swing.*;
import java.rmi.*;
import java.awt.event.*;
import java.awt.*;

public class Aclienty
{
static JFrame frame;
static JPanel panel,panel1;
JLabel labelAuthorID,labelLastName,labelFirstName,labelPhone,labelAddress,labelCity,labelState,labelZip;
JTextField textAuthorID,textLastName,textFirstName,textPhone,textAddress,textCity,textState,textZip;
JButton submit;
static String authorID;
static String lastName;
static String firstName;
static String phone;
static String address;
static String city;
static String state;
static String zip;

public Aclienty()
{
frame = new JFrame("Earnest Publishing House");
panel = new JPanel();
panel1 = new JPanel();
panel.setLayout(new GridLayout(8,2));
panel1.setLayout(new GridLayout(1,1));
frame.setVisible(true);
frame.setSize(400,400);
frame.getContentPane().setLayout(new BorderLayout());

labelAuthorID = new JLabel("Author ID");
labelLastName = new JLabel("Last Name");
labelFirstName = new JLabel("First Name");
labelAddress = new JLabel("Address");
labelPhone = new JLabel("Phone");
labelCity = new JLabel("City");
labelState = new JLabel("State");
labelZip = new JLabel("Zip");

textAuthorID = new JTextField(5);
textLastName = new JTextField(15);
textFirstName = new JTextField(15);
textPhone = new JTextField(10);
textAddress = new JTextField(50);
textCity = new JTextField(10);
textState = new JTextField(10);
textZip = new JTextField(6);

submit = new JButton("Submit");

panel.add(labelAuthorID);
panel.add(textAuthorID);
panel.add(labelLastName);
panel.add(textLastName);
panel.add(labelFirstName);
panel.add(textFirstName);
panel.add(labelPhone);
panel.add(textPhone);
panel.add(labelAddress);
panel.add(textAddress);
panel.add(labelCity);
panel.add(textCity);
panel.add(labelState);
panel.add(textState);
panel.add(labelZip);
panel.add(textZip);
panel.add(submit);
ButtonListener blisten = new ButtonListener();
submit.addActionListener(blisten);
frame.getContentPane().add(new JPanel(), BorderLayout.WEST);
frame.getContentPane().add(new JPanel(), BorderLayout.EAST);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.getContentPane().add(panel1, BorderLayout.SOUTH);
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JButton source = (JButton)evt.getSource();
MyDialog myDialog;
try
{
AuthorServer server = (AuthorServer)Naming.lookup("rmi://127.0.0.1/Authorserver";);
authorID = textAuthorID.getText();
lastName = textLastName.getText();
firstName = textFirstName.getText();
phone = textPhone.getText();
address = textAddress.getText();
city = textCity.getText();
state = textState.getText();
zip = textZip.getText();

String str = server.insertDetails(authorID, lastName, firstName, phone, address, city, state, zip);
System.out.println(str);
if(str.equals("success"))
{
myDialog = new MyDialog(frame,"Inserted Successfully");
}
else
{
myDialog = new MyDialog(frame,"No record inserted");
}
myDialog.setVisible(true);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String[]args)
{
new Aclienty();
}
}
class MyDialog extends Dialog implements ActionListener
{
MyDialog(Frame parent, String title)
{
super(parent,title,false);
setLayout(new FlowLayout());
setSize(400,400);
add(new JLabel(title));
JButton buok = new JButton("OK");
add(buok);
buok.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
dispose();
}
}

The server app is shown below:

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
import java.sql.Connection;

public class AuthorServerImpl extends UnicastRemoteObject implements AuthorServer
{
static ResultSet result;
static Connection con;
static PreparedStatement stat;

public AuthorServerImpl() throws RemoteException
{
super();
}
public String insertDetails(String authorID,String lastName,String firstName,String phone,String address,String city,String state,String zip) throws RemoteException
{
int rowsAffected = 0;
String sReturn = "fail";
try
{

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/publishing","thepubs","eansbok");
stat = con.prepareStatement("insert into authors(AuthorID,Last_Name,First_Name,Phone,Address,City,State,Zip)values(?,?,?,?,?,?,?,?)");
stat.setString(1, authorID);
stat.setString(2, lastName);
stat.setString(3, firstName);
stat.setString(4, phone);
stat.setString(5, address);
stat.setString(6, city);
stat.setString(7, state);
stat.setString(8, zip);
rowsAffected = stat.executeUpdate();
if(rowsAffected > 0)
{
sReturn = "success";
}
}
catch(Exception ex)
{
System.out.println("Error at value insert" + ex);
}
return sReturn;
}
public static void main(String[]args)
{
System.setSecurityManager(new RMISecurityManager());
try
{

AuthorServerImpl instance = new AuthorServerImpl();
Naming.rebind("Authorserver", instance);
System.out.println("Server Registered");

}
catch(Exception ey)
{
System.out.println(ey);
}
}
}
This code was actually adopted from an NIIT Manual that i had wayback 2006 when i was a complete newbie to java. Now i intend trying out the RMI samples i have in the manual. But i feel there is something i'm missing. I'm not sure if there should a restructuring of this code due to API enhancements or anything else. I'm appreciate a rapid response...

Olakunle Oladipo Oni
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question too difficult for “beginning”: moving.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code snippet is more about swing GUI. Question not clear, please rephrase it sorry !
reply
    Bookmark Topic Watch Topic
  • New Topic