zoukankan      html  css  js  c++  java
  • java对数据库的操作

    package com.DateSystem;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.LinkedList;
    import java.util.List;
    
    import javax.swing.JOptionPane;
    
    import com.gui.*;
    public class Good_data {
    	//根据商品名查询
    	public List<Goods> findGoods(String sql){
    		List<Goods> list=new LinkedList<Goods>();
    		Statement stat=null;
    		ResultSet rs=null;
    		
    		//获取和数据库的连接
    		Connection conn=ConnectionDriver.getConnection();
    		try {
    			//生成Statement对象,封装SQL语句,执行查询
    			stat=conn.createStatement();
    			rs=stat.executeQuery(sql);
    			//处理结果集中数据,读取每一条记录
    			while(rs.next()){
    				//int  id=rs.getInt(1);
    				String  id=rs.getString(1);
    				
    				String name=rs.getString(2);
    				String address=rs.getString(3);
    				
    				double  in_price=rs.getDouble(4);
    				double  out_price=rs.getDouble(5);
    				
    				int  jinhuo=rs.getInt(6);				
    				int  chuhuo=rs.getInt(7);
    				int  kucun=rs.getInt(8);
    				
    				
    				
    				Goods d=new Goods();
    				
    				d.setName(name);
    				d.setId(id);
    				d.setAddress(address);
    				d.setIn_price(in_price);
    				d.setOut_price(out_price);
    				d.setKucun(kucun);
    				d.setJinhuo(jinhuo);
    				d.setChuhuo(chuhuo);
    				list.add(d);			
    				
    			}
    		} catch (SQLException e) {			
    			e.printStackTrace();
    		}finally{
    			JDBcloss.close(rs, stat, conn);
    		}
    		
    		return list;
    	}
    	//依靠商品号查询
    	public Goods findStudentById(String id){
    		
    		System.out.print("goods");
    		//创建Data对象
    		Goods data=new Goods();
    		Statement stat=null;
    		ResultSet rs=null;
    		Connection conn=ConnectionDriver.getConnection();
    
    		String sql="select  * from result where id='"+id+"'";
    		try {
    			stat=conn.createStatement();
    			//执行查询
    			rs=stat.executeQuery(sql);
    			//处理结果集
    			if(rs.next()){
    				//int  id_id=rs.getInt(1);
    				String  id_id=rs.getString(1);
    				
    				String name=rs.getString(2);
    				String address=rs.getString(3);
    				
    				double  in_price=rs.getDouble(4);
    				double  out_price=rs.getDouble(5);
    				
    				int  jinhuo=rs.getInt(6);				
    				int  chuhuo=rs.getInt(7);
    				int  kucun=rs.getInt(8);
    				
    				
    				data.setId(id_id);
    				data.setName(name);
    				data.setAddress(address);
    				data.setIn_price(in_price);
    				data.setOut_price(out_price);
    				data.setKucun(kucun);
    				data.setJinhuo(jinhuo);
    				data.setChuhuo(chuhuo);
    				
    								
    			}
    		} catch (SQLException e) {			
    			e.printStackTrace();
    		}		
    		return data;
    	}
    	
    	
    	
    	
       //对商品信息进行增加、修改、删除
    	public boolean updateStudent(String sql){
    		boolean flag=false;
    		Statement stat=null;
    		Connection conn=ConnectionDriver.getConnection();
    		try {
    			//生成Statement对象,向数据库发送sql指令
    			stat=conn.createStatement();
    			int i=stat.executeUpdate(sql);
    			if(i>0){
    				flag=true;
    			}
    		} catch (SQLException e) {			
    			e.printStackTrace();
    		}finally{
    			JDBcloss.close(stat, conn);
    		}
    		
    		return flag;
    	}
    }
    


  • 相关阅读:
    Windows安装Docker Toolbox 和docker-machine 常用操作
    VirtualBox6.1下载及安装 创建虚拟机
    Linux CentOS7 Docker-machine的安装
    CentOS Docker 安装
    gcc、g++
    找不到所需要的ndbm.h头文件
    最长公共字串(LCS)最长连续公共字串(LCCS)
    C# 爬虫批量下载文件
    '"VCVARS32.BAT"' 不是内部或外部命令,也不是可运行的程序
    jsoncpp 生成 json 字符串
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3188537.html
Copyright © 2011-2022 走看看