Common Java Methods

Source: Internet
Author: User
Tags date empty sql mysql string split stmt tostring
<%!
Stc_rsyb_name stcrsyname1[];
Stc_syb_inf stcsybinf1[];
%>

<%/*
String p = (string) session.getvalue ("Access");
if (P==null | |!p.equals ("1")) {
Response.sendredirect ("error.htm");
Return
}*/
%>
<%!
file://get the name of the machine or IP
String Get_myhostname (Javax.servlet.http.HttpServletRequest request) {
String myhostname = null;
try{
Myhostname = Request.getremotehost ();
Myhostname = Myhostname.touppercase ();
}catch (Exception e) {}
return myhostname;
}
file://processing an empty string
String Dealnull (String str) {
String returnstr = null;
if (str = null) RETURNSTR = "";
else returnstr = str;
return returnstr;
}
file://handles empty objects
Object Dealnull (Object obj) {
Object returnstr = null;
if (obj = = null) Returnstr = (Object) ("");
else returnstr = obj;
return returnstr;
}

int Dealempty (String s) {
s = dealnull (s);
if (S.equals ("")) return 0;
return Integer.parseint (s);
}

String Replace (string str,string substr,string restr) {
string[] tmp = Split (STR,SUBSTR);
String returnstr = null;
if (tmp.length!=0) {
Returnstr = tmp[0];
for (int i = 0; i < tmp.length-1; i++)
Returnstr =dealnull (RETURNSTR) + restr +tmp[i+1];
}
Return Dealnull (RETURNSTR);
}

String HtmlEncode (String txt) {
txt = replace (TXT, "&", "&");
txt = replace (TXT, "&", "&");
txt = replace (txt, "" "," "");
txt = replace (txt, "\" "," "");
txt = replace (txt, "<", "<");
txt = replace (txt, "<", "<");
txt = replace (txt, ">", ">");
txt = replace (txt, ">", ">");
txt = replace (txt, "", "");
txt = replace (txt, "", "");
return txt;
}

String Unhtmlencode (String txt) {
txt = replace (TXT, "&", "&");
txt = replace (txt, "" "," \ ");
txt = replace (txt, "<", "<");
txt = replace (txt, ">", ">");
txt = replace (txt, "", "");
return txt;
}

File://ALERT Function
void Salert (String title,string msg,string Focus, jspwriter out) {
try {
Out.println ("<script language= ' JavaScript ' >");
OUT.PRINTLN ("alert" + title + "\" + \ "\\r\\n\" + \ "" + Replace (msg, "" "," ")," \ "", "\\\") + "\");
OUT.PRINTLN (focus);
Out.println ("</script>");
catch (Exception e) {System.out.println (e);}
}
File://CONFIRM Function
void Sconfirm (String title,string msg, string location,jspwriter out) {
try{
Out.println ("<script language= ' JavaScript ' >");
Out.println ("If" Confirm (\ "" + title + "\" + \ "\\r\\n\" + \ "" + Replace (msg, "\", "\\\") + "\")) {");
Out.println ("var d = (new Date ()). ToString ();");
OUT.PRINTLN (location + "+ D;");
Out.println ("}");
Out.println ("Parent.document.form1.text1.select ();");
Out.println ("</script>");
catch (Exception e) {}
}

file://Get string length
int Getstringlength (String s) {
int p = 0;
try {
int ilength = 0;
int ireallength = 0;
int j = 0;
p = s.getbytes ("Shift_JIS"). Length;
catch (Java.io.UnsupportedEncodingException e) {}
return p;
}

void formatstring (String s,int b,jspwriter out) {
b--;
String t = "";
try {
int leng = Getstringlength (s);
int j = 0;
if (Leng > B) {
for (int i = 1; I <= b; i++) {
t = s.substring (0,i);
if (T.getbytes ("Shift_JIS"). Length <= b) {
j + +;
else break;
}
file://j = j+1;
File://System.out.println ("b=" + B + "----" + "j=" + j);
for (int i = 0;i < J; i++) {
if (s.substring (i,i+1). Equals (""))
Out.print ("");
if (!s.substring (i,i+1). Equals (""))
Out.print (HtmlEncode (s.substring (i,i+1)));
}
if (s.substring (0,j). GetBytes ("Shift_JIS"). length = = b) {
Out.print ("|");
}else{
Out.print ("|");
}
} else {
for (int i = 0; i < B-leng; i++)
t = t + "";
for (int i = 0; i < s.length (); i++) {
if (s.substring (i,i+1). Equals (""))
Out.print ("");
if (!s.substring (i,i+1). Equals (""))
Out.print (HtmlEncode (s.substring (i,i+1)));
}
Out.print (HtmlEncode (t + "|"));
}
catch (Exception e) {System.out.println ("----" + e);}
}

FILE://judge whether it is a number
Boolean IsNumeric (String number)
{
Try
{
Integer.parseint (number);
return true;
}
catch (NumberFormatException Sqo)
{
return false;
}
}
FILE://judge whether it is a date
String Mk_date (String C_date,int c_mode)
{
String wk_buf= "";
String today = "";
Try
{
Today = Integer.tostring (Java.util.Calendar.getInstance (). Get (Java.util.Calendar.YEAR));
Switch (C_mode)
{
Case 0:
{
Wk_buf = c_date.substring (2, 4) + "/" + c_date.substring (4, 6);
if (c_date.length () = = 8)
{
Wk_buf = Wk_buf + "/" + c_date.substring (6, 8);
}
Break
}
Case 1:
{
for (int i=0; i<c_date.length (); i++)
{
if (c_date.substring (i, i+1) = = "")
{
Wk_buf = Wk_buf + "0";
}
Else
{
Wk_buf = Wk_buf + c_date.substring (i, i+1);
}
}
C_date = Wk_buf;
Wk_buf = "";
if (c_date.substring (0, 1)!= "0")
{
Wk_buf = "19";
}
Else
{
Wk_buf = "20";
}

Wk_buf = wk_buf + c_date.substring (0, 2) + c_date.substring (3, 5);

if (c_date.length () = = 8)
{
Wk_buf = Wk_buf + c_date.substring (6, 8);
}
Break
}
Case 2:
{
Wk_buf = c_date.substring (0, 4) + c_date.substring (5, 7) + c_date.substring (8, 10);
Break
}
Case 3:
{
for (int i=0; i<c_date.length (); i++)
{
if (c_date.substring (i, i+1) = = "")
{
Wk_buf = Wk_buf + "0";
}
Else
{
Wk_buf = Wk_buf + c_date.substring (i, i+1);
}
}
C_date = Wk_buf;
Wk_buf = "";
if (Integer.parseint (c_date.substring (0,2)) > Integer.parseint (today.substring (2, 4))
{
Wk_buf = integer.tostring (Integer.parseint (today.substring (0,2))-1);
}
Else
{
Wk_buf = today.substring (0,2);
}
Wk_buf = Wk_buf + c_date.substring (0,2) + c_date.substring (3,5);
if (c_date.length () = = 8)
{
Wk_buf = Wk_buf + c_date.substring (6,8);
}
Break
}
Case 4:
{
Wk_buf = c_date.substring (0,4) + "/" + c_date.substring (4,6) + "/" + c_date.substring (6, 8);
Break
}
Case 5:
{
Wk_buf = c_date.substring (0,4) + c_date.substring (5, 7);
Break
}
}
}
catch (Exception e)
{
System.out.println ("wrong:" + E);
}
return wk_buf;
}

int cInt (float stsid) {
int cInt = 0;
try{
int stsid1 = 0;
if (Stsid > 0) {
if ((Stsid-(int) stsid > 0) && (stsid-(int) Stsid!= 0.5))
STSID1 = (int) (STSID + 0.5);
if (Stsid-(int) Stsid = = 0.5) {
if (((int) (stsid-0.5))%2 = = 0)
STSID1 = (int) (stsid-0.5);
Else
STSID1 = (int) (STSID + 0.5);
}
if (Stsid-(int) Stsid ==0)
STSID1 = (int) stsid;
}else{
Stsid =-stsid;
if ((Stsid-(int) stsid > 0) && (stsid-(int) Stsid!= 0.5))
STSID1 =-(int) (STSID + 0.5);
if (Stsid-(int) Stsid = = 0.5) {
if (((int) (stsid-0.5))%2 = = 0)
STSID1 =-(int) (stsid-0.5);
Else
STSID1 =-(int) (STSID + 0.5);
}
if (Stsid-(int) Stsid ==0)
STSID1 =-(int) stsid;
}
CInt = STSID1;
}catch (Exception e) {}
return cInt;
}

Long CLng (float numb) {
int nu = 0;
Long cLng = 0;
try{
Nu = cInt (numb);
CLng = (long) nu;
}catch (Exception e) {}
return cLng;
}

Float Dealfloat (String s) {
S=dealnull (s);
if (S.equals (""))
return 0;
return float.parsefloat (s);
}

String[] Split (String source,string div) {
int arynum = 0,intidx=0,intidex=0,div_length = Div.length ();
if (Source.compareto ("")!=0) {
if (Source.indexof (div)!=-1) {
Intidx = Source.indexof (div);
for (int intcount =1;; intcount++) {
if (Source.indexof (div,intidx+div_length)!=-1) {
intidx= Source.indexof (div,intidx+div_length);
Arynum = intcount;
}
else {arynum+=2;break;}
}
}else arynum = 1;
}else arynum = 0;

intidx=0;
intidex=0;
string[] Returnstr = new String[arynum];

if (Source.compareto ("")!=0) {

if (Source.indexof (div)!=-1) {

INTIDX = (int) source.indexof (DIV);
returnstr[0]= (String) source.substring (0,INTIDX);

for (int intcount =1;; intcount++) {
if (Source.indexof (div,intidx+div_length)!=-1) {
intidex= (int) source.indexof (div,intidx+div_length);

Returnstr[intcount] = (String) source.substring (Intidx+div_length,intidex);

INTIDX = (int) source.indexof (div,intidx+div_length);
}
else {
Returnstr[intcount] = (String) source.substring (Intidx+div_length,source.length ());
Break
}
}
}
else {returnstr[0] = (String) source.substring (0,source.length ()); return returnstr;}
}
else {return returnstr;}
return returnstr;
}


public class Stc_rsyb_name
{
public int pos =-1;
Public String NAME = "";
Public String host = "";
Public String home1 = "";
Public String home2 = "";
Public String Home3 = "";
}
public class Stc_syb_inf
{
String reg_name = "";
String g_syb = "";
String r_syb = "";
}
file://Output Space
void Showblank (int num,jspwriter out) {
try{
for (int i=0;i<=num;i++)
Out.print ("");
}
catch (Exception e) {}
}


String G_syb_mk () {
String G_SYB_MK = null;
String nis_m_o = "1???????????????????????????????????????????????????????????????";
String local_m = "1??????????????????????????????????????????????????????????????";
String winnt_dc = "???? 1??????????????????????????????????????????????????????????? ";
String nis_local = "one??????????????????????????????????????????????????????????????";
int Ret = 0,reccount = 0,k = 0;
String Set_ret = "Ssy";
string[] Domein = null;
string[] Group_syb = null;
int wk_syb[];
try{
String sql = "SELECT COUNT (*)" from 寁 Lantau 婡 where 庬 暿 like ' "+ nis_m_o +" ' or words beast 庬 暿 like ' "+ local_m +" ' or words beast 庬 暿 like ' "+winnt _DC + "'";
rs = stmt.executequery (SQL);
if (Rs.next ()) {
RecCount = Rs.getint (1);
rs = null;
}
String SQL2 = "SELECT * from 寁 婡 where to talk beast 庬 暿 like '" + nis_m_o + "' or words beast 庬 暿 like '" + local_m + "' or words Beast 庬 ' 暿 like '" +winnt_dc + " '";
while (Rs.next ()) {
int i = 0;
Domein[i] = rs.getstring ("Neon also 輺?");
i++;
}
for (int j = 0; J < RecCount; J + +) {
String sql3 = "SELECT * from 僌 儖 乕 僾 庬 where neon 暿? = '" + 輺] + "'";
rs = Stmt.executequery (SQL3);
if (Rs.next ()) {
K = k + 1;
Group_syb[k] = rs.getstring ("Gao Cease kick 庬 暿");
}
}
for (int l = 0; l < l++) {
for (int m = 0; m < K; m++) {
if (group_syb[m].substring (l,l+1). Equals ("1")) {
if (l==1) {
Set_ret = "1";
}else{
Set_ret = Set_ret + "1";
}
Break
}else{
if (m==k) {
if (l==1) {
Set_ret = "0";
}else{
Set_ret = Set_ret + "0";
}
}
}
}
}
G_SYB_MK = Set_ret;
}catch (Exception e) {}
return G_SYB_MK;
}

int recount (String strtable,string Sel)
{
String myst= "";
int t = 0;
ResultSet Rsd=null;
Try
{
myst= "SELECT COUNT (*) ads from" + strtable + "" + Sel;
Rsd= conn.createstatement (). executequery (Myst);
if (Rsd.next ())
{
T=rsd.getint ("ads");
}
}
catch (SQLException sqe)
{
System.out.println ("ODBC wrong:" + sqe + "+" +myst);
}
RSD = null;
return t;

}

int DCount (String duan,string strtable,string Strsel)
{
int t=0;
ResultSet Rss=null;
Try
{
String mysql= "SELECT COUNT (*) ads from" + strtable + "where" + Strsel + "and" + Duan + "are not NULL";
Rss= conn.createstatement (). executequery (MYSQL);
if (Rss.next ())
{
T=rss.getint ("ads");
}
}
catch (Exception sqe)
{
System.out.println (SQE);
}
Rss=null;
return t;
}

String DLookup (String duan,string strtable,string Strsel)
{
String strreturn = null;
ResultSet RSS = NULL;
try{
String strSQL = "Select" +duan+ "from" +strtable+ "where" + Strsel;
rss = Conn.createstatement (). executequery (strSQL);
if (Rss.next ()) {

Strreturn = rss.getstring (Duan);
}

}catch (Java.sql.SQLException Sqle) {
System.out.println ("dlookup_exception:" +sqle);
}

rss = NULL;
return strreturn;
}
String RTrim (String str) {
String rstr = str;
if (str = null)
{
RSTR = "";
}
Else
{
if (str.length () = = 0) Rstr = "";
if (str.length () > 0)
{
for (int i = Str.length (); i > 0; i--)
{
if (str.substring (i-1,i). Equals (""))
RSTR = rstr.substring (0,i-1);
else break;
}
}

}
return RSTR;
}
void Sybinf_set (Stc_rsyb_name in_data, Stc_syb_inf out_data) throws java.sql.sqlexception{
String new_syb = "00000000000000000000000000000000";
Out_data.reg_name = In_data.name;
String sql = "";
String rdomein1 = "";
String rdomein2 = "";
String rdomein3 = "";
ResultSet RSF = null;
Statement STF = Conn.createstatement ();
try {
sql = "Select Gao Cease kick 庬 暿" from 僌 儖 乕 僾 庬 暿 Where neon also 輺? = ' "+ In_data.host +" ";
RSF = Conn.createstatement (). executequery (SQL);
if (Rsf.next ()) {
Out_data. G_syb = Dealnull (rsf.getstring ("Gao Cease kick 庬 暿"));
} else {
Out_data. G_syb = "";
}
catch (Java.sql.SQLException Sqle) {
System.out.println ("111111" + Sqle);
}
try {
sql = "Select 棙 梡 庬 暿 from 棙 梡 庬 暿 Where neon also 輺? = '" + in_data.home1 + "'";
RSF = stf.executequery (sql);
if (Rsf.next ())
Rdomein1 = Dealnull (rsf.getstring ("棙 梡 庬 暿"));
catch (Java.sql.SQLException Sqle) {
System.out.println ("22222" + Sqle);
}
try {
sql = "Select 棙 梡 庬 暿 from 棙 梡 庬 暿 Where neon also 輺? = '" + in_data.home2 + "'";
RSF = stf.executequery (sql);
if (Rsf.next ())
Rdomein2 = Dealnull (rsf.getstring ("棙 梡 庬 暿"));
catch (Java.sql.SQLException Sqle) {
System.out.println ("333333" + Sqle);
}
try {
sql = "Select 棙 梡 庬 暿 from 棙 梡 庬 暿 Where neon also 輺? = '" + In_data.home3 + "'";
RSF = stf.executequery (sql);
if (Rsf.next ())
rdomein3 = Dealnull (rsf.getstring ("棙 梡 庬 暿"));
catch (Java.sql.SQLException Sqle) {
System.out.println ("444444" + Sqle);
}
RSF = null;
STF = null;
String p = char_or (rdomein1, rdomein2);
if (P.equals ("")) p = new_syb;
String q = char_or (P, rdomein3);
if (Q.equals ("")) Q = new_syb;
Out_data. R_SYB = q;
}
String Char_or (String arg1, string arg2) {
int len1 =-1, len2 =-1, Max_len =-1, Min_len =-1;
String ret_sts = "";
int long_f =-1;
Len1 = Arg1.length ();
Len2 = Arg2.length ();
if (len1 = = 0 && len2 > 0)
return arg2;
else if (len1 > 0 && len2 = 0)
return arg1;
else if (len1 = 0 && len2 = 0)
Return "";
Ret_sts = "";
if (Len1 > Len2) {
Max_len = len1;
Min_len = Len2;
Long_f = 1;
else if (Len1 < len2) {
Max_len = Len2;
Min_len = len1;
Long_f = 2;
} else {
Max_len = len1;
Min_len = Len2;
Long_f = 0;
}
for (int i = 1; I <= Max_len; i++) {
if (i <= Min_len) {
if (arg1.substring (i-1, i). Equals ("1") | | | arg2.substring (I-1, i). Equals ("1"))
ret_sts = ret_sts + "1";
Else
ret_sts = ret_sts + "0";
} else {
if (Long_f = 1)
ret_sts = ret_sts + arg1.substring (i-1, i);
else if (Long_f = 2)
ret_sts = ret_sts + arg2.substring (i-1, i);
}
}
return ret_sts;
}
%>

<%!
void WriteFile (String mystring,string target_file) {
try {
Java.io.PrintWriter pw = new Java.io.PrintWriter (new Java.io.FileOutputStream (target_file));
Pw.println (myString);
Pw.close ();
Mystring= "";
catch (Java.io.IOException e) {
System.out.println (E.getmessage ());
}
}
%>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.