how to process a simple loop in WWW::Mechanize to be more efficient?

Need to get this Java application work correct?

  • Hi people. Can you help me to resolve a problemm. I have a java application, that works perfect in cmd, but i need to run it when windows is loaded. But problemm is that a while loop working only one time. For example: computer can log off or stay logged in. Code is below: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package lockerjava; import java.sql.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; /** * * * @author Timofei */ public class LockerJava { public static String now(String dateFormat) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); return sdf.format(cal.getTime()); } public static int getIndeficator(int hours, int minutes){ minutes=minutes/10*10; int ident=1; int hoursFind=11; int minutesFind=40; for(int i=1;i<=68;i++){ if(hoursFind==hours&&minutes==minutesFin… ident=i; minutesFind+=10; if(minutesFind>=60){ minutesFind=0; hoursFind++; } } return ident+70; } static String url = "jdbc:mysql://www.myprtf.com/"; static String dbName = "timofei91_gameclub"; static String driverName = "org.gjt.mm.mysql.Driver"; static String userName = "timofei91"; static String passWord = "123"; static String pcNumber="(3) i_3_pc"; private class LoopTask{ public void run() { } } public static void main(String[] args) throws InterruptedException, SQLException { Thread.sleep(10000); String date=LockerJava.now("dd.MM."); date=date+LockerJava.now("y"); //String date="02.03.2012"; int ident=LockerJava.getIndeficator(Integer.… System.out.println(ident); String time; int i=0; Connection con=null; Statement stmt=null; int h=0; while(h<100000){ try { Class.forName(driverName).newInstance(); con=DriverManager.getConnection(url+dbNa… userName, passWord); System.out.print("succesfully"); stmt=con.createStatement(); System.out.println (date); ResultSet game=stmt.executeQuery("SELECT pur_time_indicator FROM purchase WHERE pur_choos_comp='"+pcNumber+"' AND pur_rent_date='"+date+"'"); if(game.first()){ game.first(); time=game.getString(1); String[] allIdent=time.split(","); for(int g=0;g<allIdent.length;g++){ if(Integer.valueOf(allIdent[g]).equals(i… System.out.println("ok"); i=1; } } while(game.next()){ time=game.getString(1); allIdent=time.split(","); System.out.println("da"); for(int g=0;g<allIdent.length;g++){ if(Integer.valueOf(allIdent[g]).equals(i… System.out.println("ok"); i=1; } } game.next(); } } System.out.println(i); } catch (Exception e) { System.err.println ("Cannot connect to database server"); System.out.println("Failed: Driver Error: " + e.getMessage()); } con.close(); if(i==0){ System.out.println ("restart"); try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("shutdown -l"); int exitVal = proc.exitValue(); System.out.println("Process exitValue: " + exitVal); } catch (Throwable t) { t.printStackTrace(); }

  • Answer:

    I'd look to create a singleton class to manage your database connection. You probably don't want to repeatedly connect to the database every time through the loop. Your loop also creates the ResultSet fresh every time through so the game. Unless something else is writing to the table that you need to query it repeatedly, this seems like a waste of processing. Where you have: if(game.first()){ This line is always resetting the position of the results to the first object. I suspect you want game.isFirst(). The copy and paste into Eclipse didn't work very well for me (some longer lines truncate in Y!A). Can you verify that your "catch" block is properly lined up with your "try"? It looks like it is outside the while loop and your try block is inside.

Jim Maryland at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.