The Artima Developer Community
Sponsored Link

Java Answers Forum
Client/Server writing to database

1 reply on 1 page. Most recent reply: Aug 13, 2003 7:05 AM by tim foulkes

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
tim foulkes

Posts: 3
Nickname: timfoulkes
Registered: Aug, 2003

Client/Server writing to database Posted: Aug 13, 2003 7:03 AM
Reply to this message Reply
Advertisement
Hello,
I’m doing an e-mail server/client assignment.
I’m 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 it’s own working fine, but I am now trying to put that into the server program.
The example I’m working from previously had the messages stored as strings, and I’m 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.*;
 
public class EmailServer2
{
	private static ServerSocket servSock;
	private static final int PORT = 1234;
	private static final String client1 = "Dave";
	private static final String client2 = "Karen";
	static Connection link;
	static Statement statement;
	static ResultSet results;
 
	public static void 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);
	}
 
	private static void 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))
				throw new InvalidClientException();
			if (!sendRead.equals("send") && !sendRead.equals("read"))
				throw new 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();
		}
	}
 
	private static void 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);
	}
}


tim foulkes

Posts: 3
Nickname: timfoulkes
Registered: Aug, 2003

Re: Client/Server writing to database Posted: Aug 13, 2003 7:05 AM
Reply to this message Reply
SORRY,
AND A BIG "THANKYOU" to anyone who can help!

Tim

Flat View: This topic has 1 reply on 1 page
Topic: Determining The Mouse State Previous Topic   Next Topic Topic: jbutton problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use