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 15:59:41 GMT

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-tp15459468p15481237.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Mime
View raw message