package aug.db;
import java.sql.*;
public class Jdb {
public String dbdriver = "sun.jdbc.odbc.JdbcOdbcDriver";
public String dbconn;
public long count;
String dbuser;
String dbpass;
Connection conn;
ResultSet rst;
private Statement stmt;
public Jdb() {
try {
Class.forName(dbdriver);
} catch (java.lang.ClassNotFoundException e) {
System.err.println("Jdb():" + e.getMessage());
}
}
public void setDbDriver(String dbdriverParam) {
dbdriver = dbdriverParam;
}
public void setConnStr(String dbconnParam, String dbuserParam, String dbpassParam) {
dbconn = dbconnParam;
dbuser = dbuserParam;
dbpass = dbpassParam;
}
public ResultSet execute(String sql) {
rst = null;
try {
conn = DriverManager.getConnection(dbconn, dbuser, dbpass);
stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rst = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println("Jdb.execute():" + ex.getMessage());
}
return rst;
}
public long update(String sql) {
long sqlResult = 0;
try {
conn = DriverManager.getConnection(dbconn, dbuser, dbpass);
Statement stmt = conn.createStatement();
sqlResult = stmt.executeUpdate(sql);
} catch (SQLException ey) {
System.err.println("Jdb.update():" + ey.getMessage());
}
return sqlResult;
}
public void closeAll() {
try {
if (rst != null) rst.close();
if (stmt != null ) stmt.close();
if (conn != null && !conn.isClosed()) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (conn != null && !conn.isClosed())
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public String pageStr(int page, int pageCount, String url, String showStr) {
//String str="Page:("+page+"/"+pageCount+") ";
String str = "";
String fstr;
String pstr;
String nstr;
String lstr;
//int page=currPage;
//int pageCount=pageCt;
if (showStr == "cn") {
fstr = "第一页";
pstr = "上一页";
nstr = "下一页";
lstr = "最后页";
} else if (showStr == "en") {
fstr = "First";
pstr = "Previous";
nstr = "Next";
lstr = "Last";
} else {
String[] temp_array = split(showStr, ",");
if (temp_array == null) {
str = "Please input String like: 'First, Previous, Next, Last'";
return str;
}
fstr = temp_array[0];
pstr = temp_array[1];
nstr = temp_array[2];
lstr = temp_array[3];
}
/*
int npage;
npgae=page+1;
int ppage;
ppage=page-1;
if(npage>pageCount)
{npae=pageCount;}
if(ppgae<1)
{ppage=1;}
*/
if (page == 1) {
str = str + "<a class='fpage' href=" + url + "page=" + (page + 1) + ">" + nstr + "</a> ";
str = str + "<a class='fpage' href=" + url + "page=" + pageCount + ">" + lstr + "</a> ";
}
if (page == pageCount) {
str = str + "<a class='fpage' href=" + url + "page=1>" + fstr + "</a> ";
str = str + "<a class='fpage' href=" + url + "page=" + (page - 1) + ">" + pstr + "</a> ";
}
if (page > 1 && page < pageCount) {
str = str + "<a class='fpage' href=" + url + "page=1>" + fstr + "</a> ";
str = str + "<a class='fpage' href=" + url + "page=" + (page - 1) + ">" + pstr + "</a> ";
str = str + "<a class='fpage' href=" + url + "page=" + (page + 1) + ">" + nstr + "</a> ";
str = str + "<a class='fpage' href=" + url + "page=" + pageCount + ">" + lstr + "</a> ";
}
int i=0;
str = str + " <select name=page_displaypagenum onChange=\"var jmpURL=this.options[this.selectedIndex].value; if(jmpURL!=''){window.location=jmpURL;} else {this.selectedIndex=0 ;}\" >";
while (i < pageCount) {
i++;
if(i == page){
str = str + "<option value="+url+"page="+i+" selected>"+i+"</option>";
}else{
str = str + "<option value="+url+"page="+i+">"+i+"</option>";
}
}
str = str + " </select> ";
return str;
}
public String[] split(String str, String strIn) {
char[] temp_array;
temp_array = str.toCharArray();
int strLength = str.length();
int strInLength = strIn.length();
int strInTimes = 0;
int strIndex[] = new int[strLength];
int i = 0;
int ii = 0;
while (i <= strLength - strInLength) {
String temp_str = "";
for (int j = i; j < i + strInLength; j++) {
temp_str = temp_str + temp_array[j];
}
if (temp_str.equals(strIn)) {
strInTimes++;
strIndex[ii] = i;
i = i + strInLength;
ii++;
} else {
i++;
}
}
if (strInTimes < 1) {
String[] back_str = null;
return back_str;
} else {
String back_str[] = new String[strInTimes + 1];
back_str[0] = str.substring(0, strIndex[0]);
for (int k = 1; k < strInTimes; k++) {
back_str[k] = str.substring(strIndex[k - 1] + strInLength, strIndex[k]);
}
back_str[strInTimes] = str.substring(strIndex[strInTimes - 1] + strInLength, str.length());
return back_str;
}
}
}