⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db_type.java

📁 精通Jsp-Web开发技术与典型应用 源码
💻 JAVA
字号:
package ch16.JavaBean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import ch16.JavaBean.DB_Conn;
import ch16.JavaBean.Type;

public class DB_type {
	DB_Conn db_conn = new DB_Conn();

	// 根据商品的类别编号查询出此商品所属的类别名称
	public String getTypename(int typeid) {
		db_conn.ConnectDB();
		ResultSet rs = null;
		try {
			String sql = "select * from type where typeid=" + typeid;
			sql = new String(sql.getBytes("ISO8859-1"), "GB2312");
			rs = db_conn.sm.executeQuery(sql);
			Type type = null;
			String typename = null;
			while (rs.next()) {
				type = new Type();
				type.setTypeid(rs.getInt("typeid"));
				type.setTypename(rs.getString("typename"));
				if (type.getTypeid() == typeid) {
					typename = type.getTypename();
				}
			}
			return typename;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return null;
		} catch (Exception E) {
			E.printStackTrace();
			return null;
		} finally {
			// 关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}

	public Vector list_type() {
		db_conn.ConnectDB();
		ResultSet rs = null;
		try {
			String sql = "select * from type";
			sql = new String(sql.getBytes("ISO8859-1"), "GB2312");
			rs = db_conn.sm.executeQuery(sql);
			Type type = null;
			Vector vector = new Vector();
			// ResultSet指针最初位于第一行之前,调用next方法使下一行成为当前行
			while (rs.next()) {
				type = new Type();
				type.setTypeid(rs.getInt("typeid"));
				type.setTypename(rs.getString("typename"));
				type.setTypedesc(rs.getString("typedesc"));
				// 向Vector矢量中添加对象goods
				vector.addElement(type);
			}
			return vector;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return null;
		} catch (Exception E) {
			E.printStackTrace();
			return null;
		} finally {
			// 关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}

	public Vector list_type(String key) {
		db_conn.ConnectDB();
		ResultSet rs = null;
		try {
			String sql = "select * from type where typename like" + "'" + "%"
					+ key + "%" + "'";
			rs = db_conn.sm.executeQuery(sql);
			Type type = null;
			Vector vector = new Vector();
			// ResultSet指针最初位于第一行之前,调用next方法使下一行成为当前行
			while (rs.next()) {
				type = new Type();
				type.setTypeid(rs.getInt("typeid"));
				type.setTypename(rs.getString("typename"));
				type.setTypedesc(rs.getString("typedesc"));
				// 向Vector矢量中添加对象goods
				vector.addElement(type);
			}
			return vector;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return null;
		} catch (Exception E) {
			E.printStackTrace();
			return null;
		} finally {
			// 关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}

	public Type list_type(int typeid) {
		db_conn.ConnectDB();
		ResultSet rs = null;
		try {
			Type type = new Type();
			String sql = "select * from type where typeid=" + typeid;
			rs = db_conn.sm.executeQuery(sql);
			if (rs.next()) {
				type.setTypeid(rs.getInt("typeid"));
				type.setTypename(rs.getString("typename"));
				type.setTypedesc(rs.getString("typedesc"));
			}
			return type;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return null;
		} catch (Exception E) {
			E.printStackTrace();
			return null;
		} finally {
			// 关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}

	public int delete_type(int typeid) {
		db_conn.ConnectDB();
		try {
			String sql = "delete from type where typeid=" + typeid;
			int i = db_conn.sm.executeUpdate(sql);
			return i;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return -1;
		} catch (Exception E) {
			E.printStackTrace();
			return -2;
		} finally {
			// 关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}

	public int inserttype(Type type) {
		db_conn.ConnectDB();
		try {
			String sql = "insert into type(typename,typedesc) values(" + "'"
					+ type.getTypename() + "'" + "," + "'" + type.getTypedesc()
					+ "')";
			sql = new String(sql.getBytes("ISO8859-1"), "GB2312");
			int i = db_conn.sm.executeUpdate(sql);
			return i;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return -1;
		} catch (Exception E) {
			E.printStackTrace();
			return -2;
		} finally {
			// 关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}

	public int updatetype(Type type) {
		db_conn.ConnectDB();
		try {
			String sql = "update type set typeid=" + type.getTypeid()
					+ ",typename='" + type.getTypename() + "',typedesc='"
					+ type.getTypedesc() + "'" + " where typeid="
					+ type.getTypeid();
			sql = new String(sql.getBytes("ISO8859-1"), "GB2312");
			int i = db_conn.sm.executeUpdate(sql);
			return i;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return -1;
		} catch (Exception E) {
			E.printStackTrace();
			return -2;
		} finally {
			//关闭连接,释放数据库资源:
			db_conn.CloseDB();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -