Hello
I am actually developing a chat system using JDBC and mysql to be deployed on a LAN.When users enter the chat,their login name and their messages are saved in the database,and are also displayed on a textarea.When running the program in java and on localhost it works fine,but when am putting it in an applet and accessing it via the LAN,it is not working.I am not sure wt is the problem.
Do u think i should change the 'localhost' in the database connection to the ip address of the server am using or should it remain same?
Here are the codes am using:
import java.io.*;
import java.net.*;
import java.util.Enumeration;
import java.util.Vector;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.sql.*;
public class MyClient1 extends Applet implements ActionListener{
//declaration of variables
private static final long serialVersionUID = 1L;
private Connection c;
private boolean _clickMeMode = true;
final static private String _driver = "com.mysql.jdbc.Driver";
final static private String _user = "root";
final static private String _pass = "";
final static private String _url = "jdbc:mysql://localhost/athenaxpress";
JLabel meslabel,enterlabel;
JList mylist;
JTextField message,loginname;
TextArea display,userlist;
JButton send= new JButton("Send");
JButton enter= new JButton("Enter");
JToolBar uppertoolBar;
JToolBar toolBar;
public void start(){
}
public void stop(){
System.out.println("Applet stopping.");
}
public void destroy(){
System.out.println("Destroy method called.");
}
public void init()
{
setLayout(new BorderLayout());
setSize(300,400);
setBackground(Color.white);
setForeground(Color.black);
meslabel = new JLabel("Type your message",JLabel.RIGHT);
meslabel.setForeground(Color.blue);
enterlabel = new JLabel("Enter your login name to enter room",JLabel.RIGHT);
enterlabel.setForeground(Color.blue);
message = new JTextField(20);
loginname = new JTextField(20);
display = new TextArea("",60,50,TextArea.SCROLLBARS_BOTH);
display.setBackground(Color.white);
display.setEditable(false);
userlist=new TextArea(40,20);
userlist.setBackground(Color.white);
send.setForeground(Color.blue);
send.addActionListener(this);
enter.setSize(40, 65);
enter.addActionListener(this);
toolBar = new JToolBar();
toolBar.setBorder(new EtchedBorder());
toolBar.add(meslabel);
toolBar.add(message);
toolBar.add(send);
uppertoolBar = new JToolBar();
uppertoolBar.add(enterlabel);
uppertoolBar.add(loginname);
uppertoolBar.add(enter);
add(uppertoolBar,BorderLayout.NORTH);
add(toolBar, BorderLayout.SOUTH);
add(display,BorderLayout.CENTER);
add(userlist,BorderLayout.EAST);
}
public void actionPerformed(ActionEvent a)
{
try
{
Class.forName (_driver);
c = DriverManager.getConnection(_url, _user, _pass);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
JButton button=(JButton)a.getSource();
if (button==enter)
{
if(_clickMeMode)
{
String user=loginname.getText();
userlist.append(user+"\n");
//loginname.setText("");
}
}
if (button==send)
{
if(_clickMeMode)
{
try{
String user=loginname.getText();
String theText = message.getText();
String user1=userlist.getText();
message.setText(null);
Statement stmt = c.createStatement();
String updateString = "INSERT INTO chat VALUES ('" +user+ "','" +theText+ "')";
stmt.executeUpdate(updateString);
ResultSet results = stmt.executeQuery("SELECT * FROM chat");
while(results.next())
{
String s = results.getString("message");
String s1 = results.getString("loginname");
display.append(s1 + ":" + s + "\n");
//System.out.println(s1+":"+s);
}
stmt.close();
}
catch(java.sql.SQLException e)
{
System.out.println("Cannot create SQL statement");
}
}
}
}
}