C3P0数据库连接池SQL语句的简单总结

 UserDao.java

package com.dao;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.bean.User;
import com.utils.DButil;

public class UserDao {
	public User login(String user_name, String user_password, String user_kind) {
		try{
			QueryRunner runner = new QueryRunner(DButil.open());
			String sql="select * from user where user_name=? and user_password=? and user_kind=? ";
			Object params[] = {user_name,user_password,user_kind};
			System.out.println("查找数据成功!");
			return (User)runner.query(sql, params, new BeanHandler(User.class));
		}catch (Exception e) {
			System.out.println("查找数据失败!");
			throw new RuntimeException(e);
		}
	}
	
	//查找已经存在用户
		public User find(String user_name){
			
			try{
				QueryRunner runner = new QueryRunner(DButil.open());
				String sql = "select * from user where user_name= ?";
				System.out.println("查找成功!");
				return (User) runner.query(sql, user_name, new BeanHandler(User.class));
			}catch (Exception e) {
				System.out.println("查找失败!");
				throw new RuntimeException(e);
			}
		} 
		
		//用户修改密码
		public void update(User user){
			try{
				QueryRunner runner = new QueryRunner(DButil.open());
				String sql = "update user set user_password = ? ,user_status = ? where user_name =?";
				Object params[] = {user.getUser_password(),user.getUser_status(),user.getUser_name().toString()};
				System.out.println("修改密码成功!");
				runner.update(sql, params);
			}catch (Exception e) {
				System.out.println("修改密码失败!");
				throw new RuntimeException(e);
			}
		}
		
		//查找所有员工
		public List<User> findBykind(String user_kind){
			try{
				QueryRunner runner = new QueryRunner(DButil.open());
				String sql = "select * from user where user_kind = ?";
				System.out.println("查找员工成功!");
				return (List<User>) runner.query(sql, user_kind, new BeanListHandler(User.class));
			}catch (Exception e) {
				System.out.println("查找员工失败!");
				throw new RuntimeException(e);
			}
		}
}

ParameterDao.java

package com.dao;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.bean.Parameter;
import com.utils.DButil;

public class ParameterDao {
	public void AddParameter(Parameter parameter){
		try{
			QueryRunner runner = new QueryRunner(DButil.open());
			String sql = "insert into parameter(name,model,power,voltage,Serial_number,Riot_level,Pro_certificate,Insulation_Class,address,Date,describes) values(?,?,?,?,?,?,?,?,?,?,?)";
			Object params[] = {parameter.getName(),parameter.getModel(),parameter.getPower(),parameter.getVoltage(),parameter.getSerial_number(),
					parameter.getRiot_level(),parameter.getPro_certificate(),parameter.getInsulation_Class(),parameter.getAddress(),parameter.getDate(),parameter.getDescribes()};
			runner.update(sql, params);
			System.out.println("设备参数添加成功!");
		}catch (Exception e) {
			System.out.println("设备参数添加失败!");
			throw new RuntimeException(e);
		}
	}
	
	public List<Parameter> findAll(){
		try{
			QueryRunner runner = new QueryRunner(DButil.open());
			String sql = "select * from parameter";
			return (List<Parameter>) runner.query(sql, new BeanListHandler(Parameter.class));
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public Parameter findById(int id){
		try{
			QueryRunner runner = new QueryRunner(DButil.open());
			String sql = "select * from parameter where id=?";
			return (Parameter) runner.query(sql, id, new BeanHandler(Parameter.class));
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	//更新
	public void update(Parameter parameter){
		try{
			QueryRunner runner = new QueryRunner(DButil.open());
			String sql = "update parameter set name=?,model=?,power=?,voltage=?,Serial_number=?,Riot_level=?,Pro_certificate=?,Insulation_Class=?,address=?,Date=?,describes=? where id= ?";
			Object params[] = {parameter.getName(),parameter.getModel(),parameter.getPower(),parameter.getVoltage(),parameter.getSerial_number(),parameter.getRiot_level(),parameter.getPro_certificate(),
					parameter.getInsulation_Class(),parameter.getAddress(),parameter.getDate(),parameter.getDescribes(),parameter.getId()};
			runner.update(sql, params);
			System.out.println("更新数据成功!");
		}catch (Exception e) {
			System.out.println("更新数据失败!");
			throw new RuntimeException(e);
		}
	}
	
	//删除
	public void delete(int id){
		try{
			QueryRunner runner = new QueryRunner(DButil.open());
			String sql ="delete from parameter where id = ?";
			runner.update(sql, id);
			System.out.println("删除数据成功!");
		}catch (Exception e) {
			System.out.println("删除数据失败!");
			throw new RuntimeException(e);
		}
	}
}


  • 0
    点赞
  • 2
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:酷酷鲨 设计师:CSDN官方博客 返回首页
评论

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值