package dao_sql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
public class WebBean
{
protected Connection conn = null;
protected void free(Statement stmt)
{
try {
if (stmt != null)
{
stmt.close();
}
}
catch (SQLException e)
{
println(e);
}
freeConn();
}
/**
* 此处插入方法说明。 创建日期:(2004-3-16 17:01:55)
*/
protected void freeConn()
{
try {
if (conn != null)
{
conn.close();
}
}
catch (SQLException e)
{
println(e);
}
}
private String gb2u(String s)
{
try {
String sUnicode = new String(s.getBytes("8859_1"), "gb2312");
s = "";
for (int i = 0; i < sUnicode.length(); i++)
{
if (sUnicode.charAt(i) < 255)
{
s = s + sUnicode.charAt(i);
}
else
{
String sHex = Integer.toHexString(sUnicode.charAt(i))
.toUpperCase();
if (sHex.length() < 4)
sHex = "0000".substring(0, 4 - sHex.length()) + sHex;
s += "&#x" + sHex + ";";
}
}
} catch (Exception exception) {
}
return s;
}
//连接数据库 这个很重要 这个方里 它是重要地方之一 这里需要你传递JNDI的名字
protected boolean getConnection() throws Exception
{
return getConnection("bbs");
}
protected HashMap getInfo(ResultSet rs) throws SQLException
{
return getInfo(rs, null);
}
protected HashMap getInfo(ResultSet rs, HashMap hm) throws SQLException
{
ResultSetMetaData rsmd = rs.getMetaData();
if (rs.next())
{
if (hm == null)
{
hm = new HashMap();
}
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{
String fieldname = rsmd.getColumnName(i);
String value = rs.getString(fieldname);
fieldname = fieldname.toLowerCase();
hm.put(fieldname, encoding(fieldname, value));
}
}
rs.close();
return hm;
}
//得到结果集 重要之二
protected HashMap getRS(ResultSet rs) throws SQLException
{
HashMap hm = new HashMap();
int count = 0;
String[] fieldsname = null ; //新加的 表示是数据库的字段名字
ResultSetMetaData rsmd = rs.getMetaData();
fieldsname = new String[rsmd.getColumnCount()];
while (rs.next())
{
//声明一个HashMap 对象
HashMap rec = new HashMap();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String fieldname = rsmd.getColumnName(i);
String value = rs.getString(fieldname);
fieldname = fieldname.toLowerCase();
rec.put(fieldname, encoding(fieldname, value));
}
//把HashMap 又放在了一个HashMap 里 这里的建值0,1,2。。。。存放了rec
hm.put("" + count, rec);
count++;
}
//把数据库的字段名字放到HashMap表里
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{
fieldsname[i-1] = rsmd.getColumnName(i);
}
hm.put("fields",fieldsname);
rs.close();
//这里的count是记录一共有多少个HashMap 用固定的count这个建值来表示
hm.put("count", new Integer(count));
return hm;
}
//重要之三 根据结果集得到 数据库里有多少条记录
protected int getCount(ResultSet rs) throws SQLException
{
int count = 0 ;
while (rs.next())
{
count = rs.getInt(1) ;
}
rs.close() ;
return count ;
}
/**
* 此处插入方法说明。 创建日期:(2004-3-15 15:28:32)
*
* @param obj
* java.lang.Object
*/
protected void println(Exception ex)
{
ex.printStackTrace(System.out);
System.out.println(ex);
}
private String encoding(String fieldname, String value)
{
if ((fieldname.indexOf("name") != -1 || fieldname.indexOf("note") != -1 || fieldname.indexOf("title") != -1)&& !fieldname.equals("servicename")
&& !fieldname.equals("businame")
&& !fieldname.equals("filename")
&& !fieldname.equals("mobname"))
{
return value;
}
return value;
}
protected boolean getConnection(String dbname) throws Exception
{
if (conn == null || conn.isClosed())
{
conn = Dao_Sql.getConnection(dbname);
}
if (conn == null || conn.isClosed())
{
return false;
}
return true;
}
}
posted on 2006-03-29 15:39
xiaozhi 阅读(337)
评论(0) 编辑 收藏