db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From musky <musk...@gmail.com>
Subject Re: Using derby as a chat server
Date Thu, 14 Feb 2008 17:04:15 GMT

here is the code for the chat application:

it is very very crude and is only experimental code:

import java.util.Timer;

import java.util.TimerTask;
import java.sql.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/ public class TestTime {
	  DBClass dbobject1=new DBClass();
	  static MainClass mc;
	  Statement stmt;
	  static Connection conn=null;
	  PreparedStatement pstmt=null;
	  static PreparedStatement pstmt1=null;

	  ResultSet rs=null;
	  ResultSet rs1=null;
	  
	  static Display d;
	    static Shell s;
	    static Button OK;

	  //int id=0;
	  
public static void main(String[] args) throws Exception {
TestTime t = new TestTime();


DBClass dbobject1=new DBClass();
conn=dbobject1.createConnection("ChatDB1");
conn.setAutoCommit(false);
t.stmt = conn.createStatement();
try
{
  t.stmt.executeUpdate( "DROP TABLE chattable" );
  System.out.println(
    "Table chattable was removed.");
}
catch ( Exception e ) {}

try
{

// may need modification for your database
  t.stmt.executeUpdate( "CREATE TABLE chattable(TID INT,Tchat VARCHAR
(255))" );

  System.out.println(
    "Table chatable was created.");
  
  
  
 
}
catch( Exception e ) 
{
	System.out.println("could not create chattable"+e);
}


/*
 try
{
t.stmt.executeUpdate("insert into chattable values(0,'HI')");
System.out.println("INSERTED");
t.stmt.executeUpdate("insert into chattable values(1,'HI MAN')");
System.out.println("INSERTED");

t.stmt.executeUpdate("insert into chattable values(2,'HI MAN HOW ARE
YOU')");
System.out.println("INSERTED");
}
catch( Exception e ) 
{
	System.out.println("could not insert into chattable"+e);
}

*/


/*try { t.stmt.close(); }
catch( Exception e ) {}*/




t.execute();
//mc=new MainClass();

d = new Display( );
s = new Shell(d);
s.setSize(250,250);
s.setText("A Text Field Example");

final Text text1 = new Text(s, SWT.MULTI | SWT.BORDER);
text1.setBounds(0,0,250,150);
OK = new Button(s, SWT.PUSH);
OK.setBounds(80,170,75,40);
OK.setText("OK");
OK.addSelectionListener(new SelectionAdapter( ) {
	   public void widgetSelected(SelectionEvent e) 
	   {
		   String contents = text1.getText( );
		   text1.setText("");
		   


	         System.out.println("Push Me Was Pushed");
	         //System.out.println(contents);
	         try
 	        {
 	        pstmt1 = conn.prepareStatement("insert into chattable
values(?,?)");
 	        pstmt1.setInt(1,0);
 	        pstmt1.setString(2,contents);
 	        
            pstmt1.executeUpdate();
 	       System.out.println("inserted "+contents+" into table");
 	        }
	         catch(Exception e1)
  	        {System.out.println("could not insert"+e1);}
 		   
     	   }
     	   
     	});

 	        
s.open();

while(!s.isDisposed( )){
    if(!d.readAndDispatch( ))
        d.sleep( );
}

System.out.println("Main Method is finishing");

} 

private void execute() throws Exception {
System.out.println(" Tasks scheduled");
int initialDelay = 1000; // start after 30 seconds
int period = 2000; // repeat every 5 seconds
Timer timer = new Timer();
System.out.println(" Timer set");


//dbclass db=new dbclass();

timer.scheduleAtFixedRate(new TimerTask ()
{
public void run()
{
System.out.println("Task Run");

//conn=dbobject1.createConnection("ChatDB1");


try
{
	//int id=mc.returnflag();
	
	//System.out.println(id);
	  // System.out.println("Before ExecuteQuery");

   
PreparedStatement pstmt=conn.prepareStatement("select * from chattable where
TID=0");
  //pstmt.setInt(1,id);

  rs=pstmt.executeQuery();
  stmt.executeUpdate("Update chattable set TID=1 where TID=0");
  //rs1=pstmt.executeQuery();
 
  
  while(rs.next())
   {
	   String str=rs.getString(2);
	   int str2=rs.getInt(1);
	   System.out.println(str2+"          "+str);
   }
	
  
}

catch ( Exception e ) {System.out.println("mhjfdshmnjh  "+e);}

}
}, initialDelay, period);
}
}

any suggestions.

thanks.

musky wrote:
> 
> i solved it it was because of:
> id=mc.returnflag();
> 
> sorry for this.
> 
> 
> musky wrote:
>> 
>> i have the following code fao a thread that will select from a table
>> periodically.
>> here it is:
>> import java.util.Timer;
>> import java.util.TimerTask;
>> import java.sql.*;
>> /**
>> * Simple demo that uses java.util.Timer to schedule a task to execute
>> * once 5 seconds have passed.
>> */ public class TestTime {
>> 	  DBClass dbobject1=new DBClass();
>> 	  MainClass mc;
>> 	  Statement stmt;
>> 	  static Connection conn=null;
>> 	  PreparedStatement pstmt=null;
>> 	  ResultSet rs=null;
>> 	  int id=0;
>> 	  
>> public static void main(String[] args) throws Exception {
>> TestTime t = new TestTime();
>> 
>> //MainClass mc=new MainClass();
>> DBClass dbobject1=new DBClass();
>> conn=dbobject1.createConnection("ChatDB1");
>> t.stmt = conn.createStatement();
>> try
>> {
>>   t.stmt.executeUpdate( "DROP TABLE chattable" );
>>   System.out.println(
>>     "Table chattable was removed.");
>> }
>> catch ( Exception e ) {          
>> /* don't care */ }
>> 
>> try
>> {
>> 
>> // may need modification for your database
>>   t.stmt.executeUpdate( "CREATE TABLE chattable(TID INT,Tchat VARCHAR
>> (255))" );
>> 
>>   System.out.println(
>>     "Table chatable was created.");
>>   
>>   
>>   
>>  
>> }
>> catch( Exception e ) 
>> {
>> 	System.out.println("could not create chattable"+e);
>> }
>> 
>> 
>> try
>> {
>> t.stmt.executeUpdate("insert into chattable values(0,'HI')");
>> System.out.println("INSERTED");
>> t.stmt.executeUpdate("insert into chattable values(1,'HI MAN')");
>> System.out.println("INSERTED");
>> 
>> t.stmt.executeUpdate("insert into chattable values(2,'HI MAN HOW ARE
>> YOU')");
>> System.out.println("INSERTED");
>> }
>> catch( Exception e ) 
>> {
>> 	System.out.println("could not insert into chattable"+e);
>> }
>> 
>> 
>> 
>> 
>> try { t.stmt.close(); }
>> catch( Exception e ) {}
>> 
>> 
>> 
>> t.execute();
>> System.out.println("Main Method is finishing");
>> 
>> } 
>> 
>> private void execute() throws Exception {
>> System.out.println(" Tasks scheduled");
>> int initialDelay = 1000; // start after 30 seconds
>> int period = 2000; // repeat every 5 seconds
>> Timer timer = new Timer();
>> System.out.println(" Timer set");
>> 
>> 
>> //dbclass db=new dbclass();
>> 
>> timer.scheduleAtFixedRate(new TimerTask ()
>> {
>> public void run()
>> {
>> System.out.println("Task Run");
>> //conn=dbobject1.createConnection("ChatDB1");
>> 
>> 
>> try
>> {
>> 	id=mc.returnflag();
>> 	//System.out.println(id);
>> 	  // System.out.println("Before ExecuteQuery");
>> 
>>    pstmt=conn.prepareStatement("select * from chattable");
>>   //pstmt.setInt(1,id);
>> 
>>   rs=pstmt.executeQuery();
>>   
>>   while(rs.next())
>>    {
>> 	   String str=rs.getString(2);
>> 	   int str2=rs.getInt(1);
>> 	   System.out.println(str2+"          "+str);
>>    }
>> 	
>>   
>> }
>> catch ( Exception e ) {System.out.println("mhjfdshmnjh  "+e);}
>> 
>> }
>> }, initialDelay, period);
>> }
>> }
>> 
>>  am getting nullpointerexception.
>> 
>> please help.
>> thanks
>> 
>> 
>> Donald McLean-3 wrote:
>>> 
>>> Yes, you would use a timer. You'll have to experiment with the timing
>>> (too
>>> often and it thrashes).
>>> 
>>> You would select any message newer than the last message that was
>>> retrieved.
>>> 
>>> On Feb 13, 2008 7:49 PM, musky <muskvar@gmail.com> wrote:
>>> 
>>>>
>>>> then how do i have a thread periodically select the latest message from
>>>> the
>>>> table?
>>>> should i use a timer or something
>>>>
>>>> Donald McLean-3 wrote:
>>>> >
>>>> > You might also want to have a messageID and maybe a messageTimestamp.
>>>> >
>>>> > If everyone is connecting directly to the database server then you're
>>>> just
>>>> > going to have to do a select periodically to see if anyone posted
>>>> anything
>>>> > new.
>>>> >
>>>> > I don't know what kind of connection limits a database server has,
>>>> but
>>>> if
>>>> > your target audience is large enough, your architecture could break
>>>> down
>>>> > (or
>>>> > even just bog down).
>>>> >
>>>> > On Feb 13, 2008 10:06 AM, musky <muskvar@gmail.com> wrote:
>>>> >
>>>> >>
>>>> >> also i dont want to use any sockets.
>>>> >> all the chat clients connect to the database server.
>>>> >>
>>>> >> musky wrote:
>>>> >> >
>>>> >> > i want to use derby as a chat server,
>>>> >> > my idea is to insert whatever a user types into a table in
the
>>>> >> > database.This table can have two fields UserID and the
>>>> >> > message(VARCHAR).how do i convey the inserted message to the
other
>>>> >> party??
>>>>
>>>>
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Using-derby-as-a-chat-server-tp15459468p15481670.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Mime
View raw message