Hello Everybody, I was making the following program and it gives me trouble. Objective of this program is to run in infinite loop after every 10 minutes and check which records has "n" tag and these needs to be send on another machine and after this make this tag as "y". But two problems comes: 1. While updating it updates one record only. For rest it gives following error: java.sql.SQLException: [JRun][SQLServer JDBC Driver]Syntax error at token 0, line 0 offset 0. 2. It hangs after sending one or two sometime. What is wrong here? I must be doing some basic mistake. Please help me. regards, Arun [ March 28, 2002: Message edited by: arun mahajan ]
arun mahajan
Ranch Hand
Joined: Dec 07, 2001
Posts: 304
posted
0
It seems there is no taker for this stupid and basic thread. I would be highly obliged if some one can help me to understand. Can I ask help from Moderator atleast? regards, Arun
arun mahajan
Ranch Hand
Joined: Dec 07, 2001
Posts: 304
posted
0
After struggling lot with my program and doing a lot of trials and errors I was able to run this but fails to understand why these changes making it runnable. This may sound stupid but would be highly obliged if you help me to understand this. Is it adviseable to call GC in this fashion? P.s.: All changes made by me is in bold regards, Arun
package test; import java.sql.*; import java.io.*; import java.net.*; public class Periodic extends Thread { boolean dbval = false; Connection conn=null,conn1=null; // //adding this flag here private boolean connectval=false; // //putting this object here instead inside SendPeriodic obj2 = null; public Periodic() { //this will provide the database connection dbconnect(); } // //making another method to achievce connectivity public synchronized void connectAnotherMachine() { obj2 = new SendPeriodic(); connectval = obj2.connect(); } public synchronized void send() { String var1=""; String var2=""; String var3="";
if(dbval) { 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()) { System.out.println("Result Set Found"); var1= result.getString("var1"); var2= result.getString("var2"); var3 = result.getString("var3"); var2=var2.trim(); String toSend = submit_txt(var1,var2); char ch = (char)32; toSend = toSend.replace('^',ch); if(toSend.length()>=1) { // //calling this here insteadconnectAnotherMachine(); 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); } } // //calling GC to clear data will it put more load on machine? System.gc(); }//while p_st.close(); result.close(); p_st=null; result=null; obj2=null; // //Again calling GC to clean up..Am i putting unnecessary load System.gc(); wait(2000); } catch(Exception e) { System.out.println("Error :"+e); } }//if }//send public synchronized String submit_txt(String val1,String val2) { String submit = ""; try { submit = "071:"+val1+"\t100:167\t083:"+val2; } catch(Exception e) { } return submit; }//Submit Txt public synchronized void updateAccount(String val1 ,String val2 ,String val3 ,int val) { try { System.out.println("From updating Account"); 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,val1); p_st1.setString(2,val3); p_st1.setString(3,val2); 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 dbconnect() { try{ System.out.println("Trying to get connected with Database..."); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 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 run() { //while(true) //{ try { send(); wait(10*1000); } catch(Exception e) { System.out.println("Error in run: "+e); } //} } public static void main(String[] args) { Periodic xx = new Periodic(); xx.start(); } }//main class SendPeriodic { Socket smsSocket=null; ObjectInputStream ois = null; ObjectOutputStream oos = null; public SendPeriodic() {} public synchronized boolean loop(String msg) { boolean ret = false; String xx = ""+msg.trim(); String val = ""; try { ret = write(xx); if(ret) { ret = read(); } } catch(Exception e) { ret=false; } return ret; }//loop
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 write(String val) { boolean ret = false;; try { oos.writeObject(val); oos.flush(); ret=true; } catch(Exception e) { ret=false; } return ret; }//write public synchronized boolean read() { boolean ret = false; try { 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 ]
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Originally posted by arun mahajan: Can I ask help from Moderator atleast?
You can. Unfortunately, although I try to visit the forum every day, this sometimes is not feasible I don't find it very easy to diagnose the problem. It does not correspond in an obvious way to an issue I know about -- other than that the JDBC-ODBC bridge has given me plenty of grief in the past and is definitely not fit for production use! Can you include a stack trace for the exception? The threading and the fact that System.gc() appears to help might indicate a timing related issue. I'll have another think. - Peter
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Threading seems an unlikely culprit - you spawn just a single thread. I did note however that you don't pay any attention to cleanup. When any of the database stuff throws an exception, statements and result sets are not closed. When you next create a new statement, the old one will still be open; this is a leak with any JDBC driver, and may be especially problematic with the JDBC-ODBC bridge as it doesn't properly support multiple open statements. If you are seeing exceptions, this might have something to do with your probem; otherwise, probably not. A minor matter, you close the statement before you close its result set; according to the javadoc, closing the statement will also close any open result sets that it might have. - Peter