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

📄 goodlistbean.java

📁 一个购物网站
💻 JAVA
字号:
package goodsList;
import java.sql.*;
import java.util.*;
public class GoodListBean {
	private Connection getConnection(){
		Connection con=null;
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}catch(ClassNotFoundException ce){
			System.out.println("ClassNotFoundException:"+ce.getMessage());
		}
		try{
			con=DriverManager.getConnection("jdbc:odbc:gamezone");
		}catch(SQLException e){
			System.out.println("GoodListBean SQLException:"+e.getMessage());		
		}
		return con;
	}
	public ArrayList getGoods(){
		ArrayList list=new ArrayList();
		Connection con=null;
		ResultSet rs=null;
		Statement stmt=null;
		ResultSetMetaData md=null;
		int colCount=0;
		try{
			
			con=getConnection();
			stmt=con.createStatement();
			rs=stmt.executeQuery("select goodId,goodName,goodPrice,goodNum,goodDetail from goodsInfo");
		
			md=rs.getMetaData();
			colCount=md.getColumnCount();
			
			while(rs.next()){
					
				Hashtable rec=new Hashtable();
				for(int i=1;i<=colCount;i++){
					String name=md.getColumnLabel(i);
					rec.put(name,rs.getObject(name));
					System.out.println("i="+i+"  name="+name);
				}
				list.add(rec);
			}
			rs.close();
			rs=null;
			stmt.close();
			stmt=null;			
		}catch(SQLException e){
			System.out.println("SQLException:"+e.getMessage());
		}finally{
			if(con!=null){
				try{
					con.close();
				}catch(SQLException e){
					System.out.println("SQLException:"+e.getMessage());		
				}
			}
		}
		return list;
	}
	
	public boolean updateGood(String sql){
		Connection con=null;
		Statement stmt=null;
		try{
			con=getConnection();
			stmt=con.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
			stmt=null;			
		}catch(SQLException e){
			System.out.println("SQLException="+e.getMessage());
		}finally{
			if(con!=null){
				try{
					con.close();
				}catch(SQLException e){
					System.out.println("SQLException:"+e.getMessage());		
				}
			}
		}
		return true;
	}
	
	public boolean delGood(String id){
		Connection con=null;
		Statement stmt=null;
		try{
			con=getConnection();
			stmt=con.createStatement();
			stmt.executeUpdate("delete from goodsInfo where goodId='"+id+"'");
			stmt.close();
			stmt=null;			
		}catch(SQLException e){
			System.out.println("SQLException="+e.getMessage());
		}finally{
			if(con!=null){
				try{
					con.close();
				}catch(SQLException e){
					System.out.println("SQLException:"+e.getMessage());		
				}
			}
		}
		return true;
	}

}

⌨️ 快捷键说明

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