wood burning stoves*
The moose likes Beginning Java and the fly likes Basic Questions...Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Questions...Please help" Watch "Basic Questions...Please help" New topic
Author

Basic Questions...Please help

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
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 ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244

//
//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;

Because they are boolean primitives they can not have the value of null (only true or false). When the object that they belong to is gc'd the memory will be released, you dont need to do anything with them yourself.
//is this the right way to decalre object?
SendPeriodic obj2 = null;
There is nothing wrong with doing it that way and assigning null explicitly.
//
//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");
Creating more than one object will take up more memory. The question to ask is whether or not you need more than one connection object. If you're not using connection pooling or making multiple connections at one time or using more than one different database you might not need more than one connection.
//
//Are following primitive or objects
//

String var1="";
String var2="";
String var3="";
Strings are not primitives, they are objects of the String class. What you are creating here though are 3 Strng references that all reference empty String literals. You can find mroe on the litterals and non-literals by using the search funtion at the top of the page.
//or shall this object declare here instead outside and through another method?
//SendPeriodic obj2 = null;
//obj2 = new SendPeriodic();
//boolean connectval = obj2.connect();

I dont know what you're asking here

//Is this right way of calling GC

System.gc();
yes

//Can I call it here also
System.gc();
Yes, but keep in mind that it is a request not guaranteed to actually invoke the GC.
[ March 30, 2002: Message edited by: Dave Vick ]

Dave
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Basic Questions...Please help
 
Similar Threads
Why this code snippet does not send the msg ?
Why this error StreamCorruptedException
How to prompt if there is no match in database using servlet?
Why it fails to update record
Why this stand alone app cannot communicate with servlet