Hello Everybody,
I have some basics questions to ask. I made the following program and marked my questions as bold with in the program.
Could you please spare a moment and let me know thier answers.
regards,
arun
CODE:-
***********
package test;
import java.net.*;
import java.io.*;
import java.sql.*;
public class testEfficency extends Thread
{
//
//will the following primitive should be made null at the end of program to let memory get free or primitive are not taking any space?
//
private boolean dbval = false;
private boolean connectval=false;
private Connection conn=null,conn1=null;
//is this the right way to decalre object?
SendPeriodic obj2 = null;
public testEfficency()
{
dbconnect();
}
public synchronized void dbconnect()
{
try
{
System.out.println("Trying to get connected with Database...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
//will making more than one object of connection makes a differance on memory or otherwise
//
conn=DriverManager.getConnection("Jdbc dbc:test","xy","xyz");
conn1=DriverManager.getConnection("Jdbc dbc:test","xy","xyz");
dbval=true;
System.out.println("Database Connected...");
}
catch(Exception e)
{
System.out.println("Error in Database Connectivity: "+e.toString());
dbval=false;
System.exit(1);
}
}//connect
public synchronized void connectAnotherMachine()
{
obj2 = new SendPeriodic();
connectval = obj2.connect();
}
public synchronized void send()
{
//
//Are following primitive or objects
//
String var1="";
String var2="";
String var3="";
//or shall this object declare here instead outside and through another method?
//SendPeriodic obj2 = null;
//obj2 = new SendPeriodic();
//boolean connectval = obj2.connect();
if(true)
{
try
{
System.out.println("Starting retrieving data...");
String data_q= "select * from table1 where var4='n' and var5='5' order by var6 desc";
PreparedStatement p_st = conn.prepareStatement(data_q);
ResultSet result = p_st.executeQuery();
while(result.next())
{
var1= result.getString("var1");
var2= result.getString("var2");
var3 = result.getString("var3");
var2=var2.trim();
if(var2.length()>160)
var2=var2.substring(0,160);
String toSend = submit_txt(var1,var2);
char ch = (char)32;
toSend = toSend.replace('^',ch);
System.out.println("toSend: "+toSend);
if(toSend.length()>=1)
{
connectAnotherMachine();
boolean loop_ret = obj2.loop(toSend);
if(loop_ret)
{
System.out.println("Send Successful");
updateAccount(var1,var3,var2,1);
}
else
{
System.out.println("Failed");
updateAccount(var1,var3,var2,0);
}
}
obj2=null;
//Is this right way of calling GC
System.gc();
System.out.println("wait for 10 seconds");
System.out.println("\n*******************************\n");
wait(10*1000);
}//while
p_st.close();
result.close();
p_st=null;
result=null;
//Can I call it here also
System.gc();
//wait(2000);
}//try
catch(Exception e)
{
System.out.println("Error :"+e);
}
} //if
else //in case connect fails
{
System.gc();
System.out.println("Error in connecting");
System.exit(1);
}
}//send
public synchronized String submit_txt(String mobileno,String message)
{
String submit = "";
try
{
submit = "071:"+var1+"\t100:167\t083:"+var2;
}
catch(Exception e)
{}
return submit;
}//Submit Txt
public synchronized void updateAccount(String val1 ,String val2 ,String val3 ,int val)
{
try
{
String data_q="";
if(val>0)
{
data_q= "update table1 set var4='y' where var1=? and var3=? and var2=?";
}
else
{
data_q= "update table1 set var4='f' where var1=? and var3=? and var2=?";
}
System.out.println("data_q: "+data_q);
PreparedStatement p_st1 = conn1.prepareStatement(data_q);
p_st1.setString(1,var3);
p_st1.setString(2,var2);
p_st1.setString(3,var1);
p_st1.executeUpdate();
System.out.println("*****Record Updated*****");
p_st1.close();
p_st1=null;
}
catch(Exception e)
{
System.out.println("*****Error in updating: "+e);
}
}
public synchronized void run()
{
while(dbval)
{
try
{
send();
System.out.println("*****Let me sleep...\n*****will wake up after 20 minute*****\n");
wait(20*60*1000);
}
catch(Exception e)
{
System.out.println("Error in run: "+e);
}
}
}
public static void main(String[] args)
{
testEfficency obj1 = new testEfficency();
obj1.start();
}
}
//
//**************************************************
//
class SendPeriodic
{
Socket smsSocket=null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
public SendPeriodic()
{}
public synchronized boolean connect()
{
boolean ret = false;
try
{
smsSocket = new Socket("192.168.100.100",6666);
oos = new ObjectOutputStream(smsSocket.getOutputStream());
ois = new ObjectInputStream(smsSocket.getInputStream());
ret=true;
}
catch (Exception e)
{
ret=false;
}
return ret;
}//connect
public synchronized boolean loop(String msg)
{
boolean ret = false;
try
{
ret = write(msg);
if(ret)
{
ret = read();
}
}
catch(Exception e)
{
ret=false;
}
return ret;
}//loop
public synchronized boolean write(String val)
{
boolean ret = false;;
try
{
//oos = new ObjectOutputStream(smsSocket.getOutputStream());
oos.writeObject(val);
oos.flush();
ret=true;
}
catch(Exception e)
{
ret=false;
}
return ret;
}//write
public synchronized boolean read()
{
boolean ret = false;
try
{
//ois = new ObjectInputStream(smsSocket.getInputStream());
String ans = (String)ois.readObject();
if((ans.indexOf("900:")>0)||(ans.toLowerCase().indexOf("exception")>0))
ret = false;
else
ret=true;
}
catch(Exception e)
{
ret=false;
}
return ret;
}//read
}//send
[ March 30, 2002: Message edited by: arun mahajan ]
I have some basics questions to ask. I made the following program and marked my questions as bold with in the program.
Could you please spare a moment and let me know thier answers.
regards,
arun
CODE:-
***********
package test;
import java.net.*;
import java.io.*;
import java.sql.*;
public class testEfficency extends Thread
{
//
//will the following primitive should be made null at the end of program to let memory get free or primitive are not taking any space?
//
private boolean dbval = false;
private boolean connectval=false;
private Connection conn=null,conn1=null;
//is this the right way to decalre object?
SendPeriodic obj2 = null;
public testEfficency()
{
dbconnect();
}
public synchronized void dbconnect()
{
try
{
System.out.println("Trying to get connected with Database...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
//will making more than one object of connection makes a differance on memory or otherwise
//
conn=DriverManager.getConnection("Jdbc dbc:test","xy","xyz");
conn1=DriverManager.getConnection("Jdbc dbc:test","xy","xyz");
dbval=true;
System.out.println("Database Connected...");
}
catch(Exception e)
{
System.out.println("Error in Database Connectivity: "+e.toString());
dbval=false;
System.exit(1);
}
}//connect
public synchronized void connectAnotherMachine()
{
obj2 = new SendPeriodic();
connectval = obj2.connect();
}
public synchronized void send()
{
//
//Are following primitive or objects
//
String var1="";
String var2="";
String var3="";
//or shall this object declare here instead outside and through another method?
//SendPeriodic obj2 = null;
//obj2 = new SendPeriodic();
//boolean connectval = obj2.connect();
if(true)
{
try
{
System.out.println("Starting retrieving data...");
String data_q= "select * from table1 where var4='n' and var5='5' order by var6 desc";
PreparedStatement p_st = conn.prepareStatement(data_q);
ResultSet result = p_st.executeQuery();
while(result.next())
{
var1= result.getString("var1");
var2= result.getString("var2");
var3 = result.getString("var3");
var2=var2.trim();
if(var2.length()>160)
var2=var2.substring(0,160);
String toSend = submit_txt(var1,var2);
char ch = (char)32;
toSend = toSend.replace('^',ch);
System.out.println("toSend: "+toSend);
if(toSend.length()>=1)
{
connectAnotherMachine();
boolean loop_ret = obj2.loop(toSend);
if(loop_ret)
{
System.out.println("Send Successful");
updateAccount(var1,var3,var2,1);
}
else
{
System.out.println("Failed");
updateAccount(var1,var3,var2,0);
}
}
obj2=null;
//Is this right way of calling GC
System.gc();
System.out.println("wait for 10 seconds");
System.out.println("\n*******************************\n");
wait(10*1000);
}//while
p_st.close();
result.close();
p_st=null;
result=null;
//Can I call it here also
System.gc();
//wait(2000);
}//try
catch(Exception e)
{
System.out.println("Error :"+e);
}
} //if
else //in case connect fails
{
System.gc();
System.out.println("Error in connecting");
System.exit(1);
}
}//send
public synchronized String submit_txt(String mobileno,String message)
{
String submit = "";
try
{
submit = "071:"+var1+"\t100:167\t083:"+var2;
}
catch(Exception e)
{}
return submit;
}//Submit Txt
public synchronized void updateAccount(String val1 ,String val2 ,String val3 ,int val)
{
try
{
String data_q="";
if(val>0)
{
data_q= "update table1 set var4='y' where var1=? and var3=? and var2=?";
}
else
{
data_q= "update table1 set var4='f' where var1=? and var3=? and var2=?";
}
System.out.println("data_q: "+data_q);
PreparedStatement p_st1 = conn1.prepareStatement(data_q);
p_st1.setString(1,var3);
p_st1.setString(2,var2);
p_st1.setString(3,var1);
p_st1.executeUpdate();
System.out.println("*****Record Updated*****");
p_st1.close();
p_st1=null;
}
catch(Exception e)
{
System.out.println("*****Error in updating: "+e);
}
}
public synchronized void run()
{
while(dbval)
{
try
{
send();
System.out.println("*****Let me sleep...\n*****will wake up after 20 minute*****\n");
wait(20*60*1000);
}
catch(Exception e)
{
System.out.println("Error in run: "+e);
}
}
}
public static void main(String[] args)
{
testEfficency obj1 = new testEfficency();
obj1.start();
}
}
//
//**************************************************
//
class SendPeriodic
{
Socket smsSocket=null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
public SendPeriodic()
{}
public synchronized boolean connect()
{
boolean ret = false;
try
{
smsSocket = new Socket("192.168.100.100",6666);
oos = new ObjectOutputStream(smsSocket.getOutputStream());
ois = new ObjectInputStream(smsSocket.getInputStream());
ret=true;
}
catch (Exception e)
{
ret=false;
}
return ret;
}//connect
public synchronized boolean loop(String msg)
{
boolean ret = false;
try
{
ret = write(msg);
if(ret)
{
ret = read();
}
}
catch(Exception e)
{
ret=false;
}
return ret;
}//loop
public synchronized boolean write(String val)
{
boolean ret = false;;
try
{
//oos = new ObjectOutputStream(smsSocket.getOutputStream());
oos.writeObject(val);
oos.flush();
ret=true;
}
catch(Exception e)
{
ret=false;
}
return ret;
}//write
public synchronized boolean read()
{
boolean ret = false;
try
{
//ois = new ObjectInputStream(smsSocket.getInputStream());
String ans = (String)ois.readObject();
if((ans.indexOf("900:")>0)||(ans.toLowerCase().indexOf("exception")>0))
ret = false;
else
ret=true;
}
catch(Exception e)
{
ret=false;
}
return ret;
}//read
}//send
[ March 30, 2002: Message edited by: arun mahajan ]