步骤 1 : dao包 步骤 2 : ConfigDAO 步骤 3 : 方法讲解 
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
				
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
				package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import entity.Config;
import util.DBUtil;
public class ConfigDAO {
	public int getTotal() {
		int total = 0;
		try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
			String sql = "select count(*) from config";
			ResultSet rs = s.executeQuery(sql);
			while (rs.next()) {
				total = rs.getInt(1);
			}
			System.out.println("total:" + total);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
	public void add(Config config) {
		String sql = "insert into config values(null,?,?)";
		try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setString(1, config.key);
			ps.setString(2, config.value);
			ps.execute();
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				int id = rs.getInt(1);
				config.id = id;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void update(Config config) {
		String sql = "update config set key_= ?, value=? where id = ?";
		try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setString(1, config.key);
			ps.setString(2, config.value);
			ps.setInt(3, config.id);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void delete(int id) {
		try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
			String sql = "delete from config where id = " + id;
			s.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public Config get(int id) {
		Config config = null;
		try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
			String sql = "select * from config where id = " + id;
			ResultSet rs = s.executeQuery(sql);
			if (rs.next()) {
				config = new Config();
				String key = rs.getString("key_");
				String value = rs.getString("value");
				config.key = key;
				config.value = value;
				config.id = id;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return config;
	}
	public List<Config> list() {
		return list(0, Short.MAX_VALUE);
	}
	public List<Config> list(int start, int count) {
		List<Config> configs = new ArrayList<Config>();
		String sql = "select * from config order by id desc limit ?,? ";
		try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setInt(1, start);
			ps.setInt(2, count);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Config config = new Config();
				int id = rs.getInt(1);
				String key = rs.getString("key_");
				String value = rs.getString("value");
				config.id = id;
				config.key = key;
				config.value = value;
				configs.add(config);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return configs;
	}
	public Config getByKey(String key) {
		Config config = null;
		String sql = "select * from config where key_ = ?" ;
		try (Connection c = DBUtil.getConnection(); 
				PreparedStatement ps = c.prepareStatement(sql);
			) {
			
			ps.setString(1, key);
			ResultSet rs =ps.executeQuery();
			if (rs.next()) {
				config = new Config();
				int id = rs.getInt("id");
				String value = rs.getString("value");
				config.key = key;
				config.value = value;
				config.id = id;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return config;
	}
}
 
								
										
									
								
							
				
					
						增值内容,请先登录
					
				
				 
		
					
		
		
					
					完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
					 
			增值内容,点击购买 
					使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
					 
					
				
				HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
			 
			 
			
			
			
			
			
		
		
		
		 	问答区域     
		 	
				
		  
	 
	  		
	  
	  	2022-01-04
	  		
	  				
	  					 
	  
					
						 PreparedStatement和Statement 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  
	  		
	  
	  	2020-12-24
	  		
	  				
	  					 
	  
					
						在进行分页查询的时候遇到了问题,报sql异常了,但是我真的没看出来啥问题。 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  
	  		
	  
	  	2020-12-16
	  		
	  				
	  					 
	  
					
						站长,请教个问题 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  
	  		
	  
	  	2020-11-09
	  		
	  				
	  					 
	  
					
						使用问题 
					
					
						
							
						
											
							
					
					
					
	   
	  
	  
	  	    
	    
	  提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 22 条以前的提问,请 点击查看 
			
			提问之前请登陆
			
		 
		提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢	
	 
 | 
	|||||||||||||