Page 1 of 1

One question on Java

Posted: Fri Oct 01, 2010 12:39 pm
by achilless
if it works ELSE connection to the database this remains open? ie is not closed?

Code: Select all

 def buildHtml(buffType):            HTML_MESSAGE = "<html><head><title>"+TITLE_NAME+"</title></head><body><center><br>"            if FREE_BUFFS == True : HTML_MESSAGE += "All buffs are for <font color=\"LEVEL\">free</font>!"                      else :                price = 0                if buffType == "buff" : price = BUFF_PRICE                                if buffType == "resist" : price = RESIST_PRICE                if buffType == "song" : price = SONG_PRICE                if buffType == "dance" : price = DANCE_PRICE                if buffType == "chant" : price = CHANT_PRICE                if buffType == "others" : price = OTHERS_PRICE                if buffType == "special" : price = SPECIAL_PRICE                if buffType == "cubic" : price = CUBIC_PRICE                HTML_MESSAGE += "All special buffs cost <font color=\"LEVEL\">"+str(price)+"</font> adena!"            HTML_MESSAGE += "<table>"            conn=L2DatabaseFactory.getInstance().getConnection()            buffCount = 0; i = 0            getList = conn.prepareStatement("SELECT * FROM buffer_buff_list WHERE buffType=\""+buffType+"\" AND canUse=1")            rs=getList.executeQuery()            while (rs.next()) :                try : buffCount += 1                except : buffCount = 0            if buffCount == 0 : HTML_MESSAGE += "No buffs are available at this moment!<br>"            else :                availableBuffs = []                getList = conn.prepareStatement("SELECT buffId,buffLevel FROM buffer_buff_list WHERE buffType=\""+buffType+"\" AND canUse=1  ORDER BY Buff_Class ASC, id")                rs=getList.executeQuery()                while (rs.next()) :                    try :                        bId = rs.getInt("buffId")                        bLevel = rs.getInt("buffLevel")                        bName = SkillTable.getInstance().getInfo(bId,bLevel).getName()                        bName = bName.replace(" ","+")                        availableBuffs += [bName+"_"+str(bId)+"_"+str(bLevel)]                    except: HTML_MESSAGE += "Error loading buff list...<br>"                try : conn.close()                except : pass                avBuffs = len(availableBuffs)                format = "0000"                for avBuffs in availableBuffs :                    buff = avBuffs                    buff = buff.replace("_"," ")                    buffSplit = buff.split(" ")                    name = buffSplit[0]                    id = int(buffSplit[1])                    level = buffSplit[2]                    name = name.replace("+"," ")                    if id < 100 : format = "00"+str(id)                    elif id > 99 and id < 1000 : format = "0"+str(id)                    else :                        if id > 4698 and id < 4701 : format = "1331"                        elif id > 4701 and id < 4704 : format = "1332"                        else: format = str(id)                    i += 1                    HTML_MESSAGE += "<tr><td><img src=\"Icon.skill"+format+"\" width=32 height=32></td><td><button value=\""+name+"\" action=\"bypass -h Quest " + QUEST_LOADING_INFO + " giveBuffs "+str(id)+" "+str(level)+" "+buffType+"\" width=190 height=32 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>"                                               HTML_MESSAGE += "</table><br><button value=\"Back\" action=\"bypass -h Quest " + QUEST_LOADING_INFO + " redirect main 0 0\" width=100 height=20 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">"            HTML_MESSAGE += "<br><font color=\"303030\">"+TITLE_NAME+"</font></center></body></html>"            return HTML_MESSAGE 

Re: One question on Java

Posted: Fri Oct 01, 2010 1:13 pm
by Probe
that's not java, that's python.
and it's quite simple, you must enclose each connection open with a try: clause, and after it put another try\pass clause to close the connection

Code: Select all

 try:  con = L2DatabaseFactory.getInstance().getConnection()  ... do your thing ...except:  ... log your exceptions ...try:  con.close()except: pass

Re: One question on Java

Posted: Fri Oct 01, 2010 1:35 pm
by achilless
that is correct to be so
def buildHtml(buffType):
HTML_MESSAGE = "<html><head><title>"+TITLE_NAME+"</title></head><body><center><br>"
if FREE_BUFFS == True : HTML_MESSAGE += "All buffs are for <font color=\"LEVEL\">free</font>!"
else :
price = 0
if buffType == "buff" : price = BUFF_PRICE
if buffType == "resist" : price = RESIST_PRICE
if buffType == "song" : price = SONG_PRICE
if buffType == "dance" : price = DANCE_PRICE
if buffType == "chant" : price = CHANT_PRICE
if buffType == "others" : price = OTHERS_PRICE
if buffType == "special" : price = SPECIAL_PRICE
if buffType == "cubic" : price = CUBIC_PRICE
HTML_MESSAGE += "All special buffs cost <font color=\"LEVEL\">"+str(price)+"</font> adena!"
HTML_MESSAGE += "<table>"
conn=L2DatabaseFactory.getInstance().getConnection()
buffCount = 0; i = 0
getList = conn.prepareStatement("SELECT * FROM buffer_buff_list WHERE buffType=\""+buffType+"\" AND canUse=1")
rs=getList.executeQuery()
while (rs.next()) :
try : buffCount += 1
except : buffCount = 0
try : conn.close()
except : pass

if buffCount == 0 : HTML_MESSAGE += "No buffs are available at this moment!<br>"
else :
availableBuffs = []
conn=L2DatabaseFactory.getInstance().getConnection()
getList = conn.prepareStatement("SELECT buffId,buffLevel FROM buffer_buff_list WHERE buffType=\""+buffType+"\" AND canUse=1 ORDER BY Buff_Class ASC, id")

rs=getList.executeQuery()
while (rs.next()) :
try :
bId = rs.getInt("buffId")
bLevel = rs.getInt("buffLevel")
bName = SkillTable.getInstance().getInfo(bId,bLevel).getName()
bName = bName.replace(" ","+")
availableBuffs += [bName+"_"+str(bId)+"_"+str(bLevel)]
except: HTML_MESSAGE += "Error loading buff list...<br>"
try : conn.close()
except : pass

avBuffs = len(availableBuffs)
format = "0000"
for avBuffs in availableBuffs :
buff = avBuffs
buff = buff.replace("_"," ")
buffSplit = buff.split(" ")
name = buffSplit[0]
id = int(buffSplit[1])
level = buffSplit[2]
name = name.replace("+"," ")
if id < 100 : format = "00"+str(id)
elif id > 99 and id < 1000 : format = "0"+str(id)
else :
if id > 4698 and id < 4701 : format = "1331"
elif id > 4701 and id < 4704 : format = "1332"
else: format = str(id)
i += 1
HTML_MESSAGE += "<tr><td><img src=\"Icon.skill"+format+"\" width=32 height=32></td><td><button value=\""+name+"\" action=\"bypass -h Quest " + QUEST_LOADING_INFO + " giveBuffs "+str(id)+" "+str(level)+" "+buffType+"\" width=190 height=32 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>"
HTML_MESSAGE += "</table><br><button value=\"Back\" action=\"bypass -h Quest " + QUEST_LOADING_INFO + " redirect main 0 0\" width=100 height=20 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">"
HTML_MESSAGE += "<br><font color=\"303030\">"+TITLE_NAME+"</font></center></body></html>"
return HTML_MESSAGE

Re: One question on Java

Posted: Fri Oct 01, 2010 1:49 pm
by Probe
Im not gonna do your work for you, learn and implement what you've learned