1 package com.pmjava.web.taglib;
2 import com.pmjava.jdbc.util.ConnectionFactory;
3 import com.pmjava.jdbc.util.DBclose;
4 import java.io.PrintStream;
5 import java.sql.*;
6 import javax.servlet.jsp.*;
7 import javax.servlet.jsp.tagext.TagSupport;
8 public class DbSelectByDbWhere extends TagSupport
9 {
10 private static final long serialVersionUID = 1L;
11 private String table_name;
12 private String id_col_name;
13 private String select_id;
14 private String name;
15 private String name_col_name;
16 private String condition;
17 private String pleaseselect;
18 private String sql_where;
19 private String id;
20 PreparedStatement ps;
21 ResultSet rs;
22 Connection con;
23 public DbSelectByDbWhere()
24 {
25 ps = null;
26 rs = null;
27 con = null;
28 try
29 {
30 jbInit();
31 }
32 catch (Exception ex)
33 {
34 ex.printStackTrace();
35 }
36 }
37 public int doStartTag()
38 throws JspException
39 {
40 if (sql_where == null || "null".equals(sql_where))
41 sql_where = "";
42 if (condition == null || "null".equals(condition))
43 condition = "";
44 try
45 {
46 con = ConnectionFactory.getConnection();
47 pageContext.getOut().write("");
48 if ("1".equals(pleaseselect))
49 pageContext.getOut().write("选择城区");
50 String Selected = null;
51 String Sql = "select * from " + table_name + " where 1=1 ";
52 if (!"".equals(sql_where) || sql_where != null)
53 Sql = Sql + " and city_id=" + sql_where + " ";
54 ps = con.prepareStatement(Sql);
55 for (rs = ps.executeQuery(); rs.next(); pageContext.getOut().write("" + rs.getString(name_col_name) + ""))
56 if (rs.getString(name_col_name).equals(select_id))
57 Selected = "selected";
58 else
59 Selected = "";
60 pageContext.getOut().write("");
61 }
62 catch (Exception e)
63 {
64 e.printStackTrace();
65 }
66 finally
67 {
68 DBclose.close(rs, ps, con);
69 }
70 return 0;
71 }
72 public void setTable_name(String table_name)
73 {
74 this.table_name = table_name;
75 }
76 public void setId_col_name(String id_col_name)
77 {
78 this.id_col_name = id_col_name;
79 }
80 public void setSelect_id(String select_id)
81 {
82 this.select_id = select_id;
83 }
84 public void setName(String name)
85 {
86 this.name = name;
87 }
88 public void setName_col_name(String name_col_name)
89 {
90 this.name_col_name = name_col_name;
91 }
92 public void setCondition(String condition)
93 {
94 this.condition = condition;
95 }
96 public void setPleaseselect(String pleaseselect)
97 {
98 this.pleaseselect = pleaseselect;
99 }
100 public void setSql_where(String sql_where)
101 {
102 this.sql_where = sql_where;
103 }
104 public String getTable_name()
105 {
106 return table_name;
107 }
108 public String getId_col_name()
109 {
110 return id_col_name;
111 }
112 public String getSelect_id()
113 {
114 return select_id;
115 }
116 public String getName()
117 {
118 return name;
119 }
120 public String getName_col_name()
121 {
122 return name_col_name;
123 }
124 public String getCondition()
125 {
126 return condition;
127 }
128 public String getPleaseselect()
129 {
130 return pleaseselect;
131 }
132 public String getSql_where()
133 {
134 return sql_where;
135 }
136 private void jbInit()
137 throws Exception
138 {
139 }
140 public static void main(String args1[])
141 {
142 }
143 public String getId()
144 {
145 return id;
146 }
147 public void setId(String id)
148 {
149 this.id = id;
150 }
151 }
文章来源:http://www.pmjava.com/Article/ShowInfo.asp?ID=62944