?? matchselectbean.java
字號:
//匹配查詢的xml配置解析器
//數(shù)據(jù)庫bean管理
package find;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Properties;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.net.URL;
import java.net.URLDecoder;
import java.io.File;
public class MatchSelectBean extends DefaultHandler{
//private String currentSet;
//private String currentName;
private int step;//用于xml元素解析步驟
private StringBuffer currentValue = new StringBuffer();
//匹配值
private String matchString;//匹配查詢所需要的字符串
private String sql;//sql語句
private String title;//頁面標(biāo)題
private String matchColumn;//匹配列
private String hide;//隱藏列
private int iMatch;//匹配列的標(biāo)號
private int iHide;//隱藏列的標(biāo)號
private int iRSCol;//結(jié)果集的column數(shù)
private String label;//標(biāo)題名
private String[] labels;//
private int p_label;//指針
private int pX;
private int pY;
private String column;//列名
// private String[][] value;//
// private String[] hideValue;//隱藏值
private String back;//返回組件
private String xmlFile;//xml配置文件路徑
//jsp表現(xiàn)層用
private int iCol;//頁面上表格的column數(shù)
private int iLine;//頁面上表格的行數(shù)
private String hideValue;//隱藏列的值;
private String matchValue;//匹配列的值;
private boolean noResult = false;//沒有結(jié)果
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
private ResultSetMetaData rsmd;
public MatchSelectBean(){
this.clear();
}
//清空
public void clear(){
this.step = 1;
this.label = "";
this.column = "";
}
//get和set方法
public String getMatchString(){
return this.matchString;
}
public void setMatchString(String s){
this.matchString = s.replaceAll("'","_");
}
public String getSql(){
return this.sql;
}
public void setSql(String s){
this.sql = s;
}
public String getXmlFile(){
return this.xmlFile;
}
public void setXmlFile(String s){
this.xmlFile = s;
}
public String getTitle(){
return this.title;
}
public void setTitle(String s){
this.title = s;
}
public String getMatchColumn(){
return this.matchColumn;
}
public void setMatchColumn(String s){
this.matchColumn = s;
}
public String getHide(){
return this.hide;
}
public void setHide(String s){
this.hide = s;
}
public String[] getLabel(){
return this.label.split(";");
}
public void setLabel(String s){
this.label = this.label+s+";";
}
public String[] getColumn(){
return this.column.split(";");
}
public void setColumn(String s){
this.column = this.column+s+";";
}
public String getBack(){
return this.back;
}
public void setBack(String s){
this.back = s;
}
public int getICol(){
return this.iCol;
}
public void setICol(int i){
this.iCol=i;
}
//定義開始解析元素的方法. 這里是將<xxx>中的名稱xxx提取出來.
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
currentValue.delete(0, currentValue.length());
if(qName.equals("標(biāo)題名")) this.step = 5;
}
//這里是將<xxx></xxx>之間的值加入到currentValue
public void characters(char[] ch, int start, int length) throws SAXException {
currentValue.append(ch, start, length);
}
//在遇到</xxx>結(jié)束后,
public void endElement(String uri, String localName, String qName) throws SAXException {
switch(this.step){
case 1:
this.setSql(this.currentValue.toString().trim());
break;
case 2:
this.setTitle(this.currentValue.toString().trim());
break;
case 3:
this.setMatchColumn(this.currentValue.toString().trim());
break;
case 4:
this.setHide(this.currentValue.toString().trim());
break;
case 5:
this.setLabel(this.currentValue.toString().trim());
break;
case 6:
this.setColumn(this.currentValue.toString().trim());
break;
case 7:
break;
case 8:
this.setBack(this.currentValue.toString().trim());
break;
default:
;
}
this.step++;
//props.put(qName.toLowerCase(), currentValue.toString().trim());
}
public void parse(String filename) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
//factory.setNamespaceAware(false);
//factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
try{
parser.parse(new File(filename).toURL().toString(), this);
}finally{
factory=null;
parser=null;
}
}
public void work() throws Exception{
//清空元素
this.clear();
//解析xml文件
this.parse(this.getXmlFile());
//取連接
conn = new SqlConnection().getSQLConnection();
/*
*step 1:填充表頭陣列fillheadline
*step 2: 取行數(shù)
*step 3:取結(jié)果集
*/
//step 1
this.fillHeadline();
//step 2
this.iLine = this.dbgetLine();
if(this.iLine==0){
this.setMatchString("");
this.iLine = this.dbgetLine();
if(this.iLine==0)this.noResult=true;
}
//step 3:
this.dbgetRS();
//關(guān)閉;
this.conn = null;
}
//根據(jù)已有的sql生成符合模塊要求的sql
public String makeSql(String s){
//插入sql條件
String a,b;
String upper = s.toUpperCase();
if(sql.matches("(?i).* ORDER BY .*")){
a = s.substring(0,upper.lastIndexOf(" ORDER BY "));
b = s.substring(upper.lastIndexOf(" ORDER BY "),s.length());
}else{
a = s;
b = "";
}
if(a.matches("(?i).* WHERE .*")){
a = a + " and ";
}else{
a = a + " where ";
}
a = a + this.getMatchColumn()+" like '%"+this.getMatchString()+"%'";
s = a + b;
return s;
}
//下面是數(shù)據(jù)庫操作函數(shù)
//得到表的行數(shù)
int dbgetLine(){
try{
//初期設(shè)定
String sql = this.sql;
sql = "SELECT count(*)" + sql.substring(sql.toLowerCase().indexOf(" from "),sql.length());
if(sql.matches("(?i).* ORDER BY .*"))
sql = sql.substring(0,sql.toUpperCase().lastIndexOf(" ORDER BY "));
String key = sql.matches("(?i).* WHERE .*")?" and ":" where ";
sql = sql+key+this.getMatchColumn()+" like "+"'%"+this.getMatchString()+"%'";
//System.out.println(sql);
//準(zhǔn)備
stmt = conn.prepareStatement(sql);
//設(shè)定
//查詢
rs = stmt.executeQuery();
//取參數(shù)
if(rs.next()){
int i = rs.getInt(1);
//System.out.print("需要打印的行數(shù):");
//System.out.print(i);
return i;
}
else return -1;
/*
rs.last();
System.out.println(rs.getRow());
rs.beforeFirst();
while(rs.next()){
System.out.println(rs.getString(4));
}
*/
}catch(Exception e){
e.printStackTrace();
return -2;
}
}
//取結(jié)果及。
int dbgetRS(){
try{
//初期設(shè)定
//如果行數(shù)為0,則無正確匹配值,將匹配字串設(shè)成""
//最后設(shè)定sql
this.setSql(this.makeSql(this.getSql()));
System.out.println(this.sql);
String sql = this.sql;
this.pX = 1;
//準(zhǔn)備
stmt = conn.prepareStatement(sql);//ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE);
//設(shè)定
//查詢
rs = stmt.executeQuery();
//取參數(shù)
/*
*1。標(biāo)記隱藏列
*2。取真實結(jié)果集列數(shù)
*/
this.iMatch = rs.findColumn(this.getMatchColumn());
this.iHide = this.getHide().equals("")?-1:rs.findColumn(this.getHide());
this.iRSCol = rs.getMetaData().getColumnCount();
this.iCol = this.iHide==-1?this.iRSCol:this.iRSCol-1;
rs.next();
return 0;
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
//數(shù)據(jù)庫操作取match值
public String dbgetMValue() throws Exception{
this.matchValue = new String(rs.getString(this.iMatch).getBytes("ISO-8859-1"));
return this.matchValue;
}
//數(shù)據(jù)庫操作取hide值
public String dbgetHValue() throws Exception{
if(this.iHide<1){
this.hideValue = "";
}else this.hideValue = new String(rs.getString(this.hide).getBytes("ISO-8859-1"));
return this.hideValue;
}
public String dbgetTValue() throws Exception{
String s = "";
//隱藏值要跳過
if(this.pX==this.iHide) this.pX++;
if(this.pX!=this.iMatch){
s = new String(rs.getString(this.pX++).getBytes("ISO-8859-1"));
}
else{
this.pX++;
s = this.matchValue;
}
while(this.pX==this.iHide){
this.pX++;
}
//大于總列數(shù)要到下一行
if(this.pX>this.iRSCol){
this.pX = 1;
rs.next();
}
return s;
}
/*
*
*
*/
//填充表頭
void fillHeadline(){
this.labels = this.getLabel();
this.p_label = 0;
}
//下面是jsp表現(xiàn)層函數(shù)
//打印表頭
public String printHeadline(){
return this.labels[this.p_label++];
}
//取打印行數(shù)
public int getPrintLine(){
return this.iLine;
}
//取表中的匹配列值
public String getMatchValue() throws Exception{
return this.dbgetMValue();
}
//取表中的hide值
public String getHideValue() throws Exception{
return this.dbgetHValue();
}
//取表中的其它數(shù)據(jù)值
public String getTValue() throws Exception{
return this.dbgetTValue();
}
//讀title
public String showTitle(){
return this.getTitle();
}
//
public String hasResult(){
return this.noResult?"對不起,沒有結(jié)果":"";
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -