Hello, Im doing an e-mail server/client assignment. Im trying to get the in boxes for the clients to read from a database and the sent messages to write to the database. I have got the reading and writing part on its own working fine, but I am now trying to put that into the server program. The example Im working from previously had the messages stored as strings, and Im trying to alter it.
apologies for the large amount of code, it's just in case you want to compile it yourself. the problem seems to be with the "doSend" function and it's arguments
import java.io.*;
import java.net.*;
import java.sql.*;
publicclass EmailServer2
{
privatestatic ServerSocket servSock;
privatestaticfinalint PORT = 1234;
privatestaticfinal String client1 = "Dave";
privatestaticfinal String client2 = "Karen";
static Connection link;
static Statement statement;
static ResultSet results;
publicstaticvoid main(String[] args)
{
System.out.println("Opening connection...\n");
try
{
servSock = new ServerSocket(PORT);
}
catch(IOException e)
{
System.out.println("Unable to attach to port!");
System.exit(1);
}
do
{
try
{
run();
}
catch (InvalidClientException icException)
{
System.out.println("Error: " + icException);
}
catch (InvalidRequestException irException)
{
System.out.println("Error: " + irException);
}
}while (true);
}
privatestaticvoid run()
throws InvalidClientException, InvalidRequestException
{
try
{
Socket link = servSock.accept();
BufferedReader in =
new BufferedReader(
new InputStreamReader(
link.getInputStream()));
PrintWriter out = new PrintWriter(
link.getOutputStream(),true);
String name = in.readLine();
String sendRead = in.readLine();
if (!name.equals(client1) && !name.equals(client2))
thrownew InvalidClientException();
if (!sendRead.equals("send") && !sendRead.equals("read"))
thrownew InvalidRequestException();
System.out.println("\n" + name + " " + sendRead + "ing mail...");
if (name.equals(client1))
if (sendRead.equals("send"))
{
doSend();
}
else
{
doRead();
}
else //From client2.
if (sendRead.equals("send"))
{
doSend();
}
else
{
doRead();
}
link.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
privatestaticvoid doSend()
throws IOException
{
//Client has requested 'sending', so server must read message
//from this client and then place message into message box for
//other client
String message;
BufferedReader input =
new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Enter message...");
message=input.readLine();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
link = DriverManager.getConnection("jdbc:odbc:db1","","");
}
catch(ClassNotFoundException e)
{
System.out.println("*Unable to load driver *");
System.exit(1);
}
catch(SQLException e)
{
System.out.println("*Cannot connect to database*");
System.exit(1);
}
try
{
String update = "INSERT INTO message"
+ " VALUES ( ,'" + name +"','" + message +"'";
statement = link.createStatement();
results = statement.executeQuery(update);
}
catch(SQLException e)
{
System.out.println("*Cannot execute query*");
e.printStackTrace();
System.exit(1);
}
}
//private static void doRead(String[] //mailbox, int messagesInBox,
// PrintWriter out) throws IOException
//{
//Client has requested 'reading', so server must read messages
//from other client's message box and then send those messages //to the first client.
//System.out.println("\nSending " + messagesInBox + " message(s).\n");
//out.println(messagesInBox);
//for (int i=0; i<messagesInBox; i++)
// out.println(mailbox[i]);
//}
}
class InvalidClientException extends Exception
{
public InvalidClientException()
{
super("Invalid client name!");
}
public InvalidClientException(String message)
{
super(message);
}
}
class InvalidRequestException extends Exception
{
public InvalidRequestException()
{
super("Invalid request!");
}
public InvalidRequestException(String message)
{
super(message);
}
}