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

📄 userdao.java

📁 jsp+javabean+servlet案例
💻 JAVA
字号:
package dao;

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


import Beans.userDto;

import db.DBConnection;



public class userDao {
	
	public boolean login(userDto dto)
	{
		boolean flag=false;
	
		Connection conn = null;
		conn = DBConnection.getConnecton();
		Statement st=null;
		ResultSet rs=null;
		String sql="select * from t_user where name='"+dto.getName()+"' and  pass='"+dto.getPass()+"'";
		System.out.println(sql);
		try
		{
			st=conn.createStatement();
			rs=st.executeQuery(sql);
			if(rs.next())
			{
					flag=true;
				
			}
		}
		catch(SQLException e)
		{
			System.out.print(e.getMessage());
		}
		finally
		{
			try
			{
				rs.close();
				st.close();
				conn.close();
			}
			catch(SQLException ex)
			{
				ex.printStackTrace();
			}
		}
		return flag;
		
			
		}
	public ArrayList select()
	{
		ArrayList al = new ArrayList();
		Statement st=null;
		ResultSet rs = null;
		String strSQL=null;
		Connection conn=DBConnection.getConnecton();
		strSQL="select * from t_user ";
		try {
			st=conn.createStatement();
			
			rs=st.executeQuery(strSQL);
			while(rs.next())
			{
				userDto dto=new userDto();
				dto.setId(rs.getString("id"));	
				dto.setName(rs.getString("name"));
				dto.setPass(rs.getString("pass"));
				al.add(dto);
			}
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			return null;
		} 
		finally
		{
			try
			{
				rs.close();
				st.close();
				conn.close();
			}
			catch(SQLException ex)
			{
				ex.printStackTrace();
			}
		}
		return al;

	}
	
	public userDto sel(String id)
	{
		ArrayList al = new ArrayList();
		Statement st=null;
		ResultSet rs = null;
		String strSQL=null;
		Connection conn=DBConnection.getConnecton();
		userDto dto=new userDto();
		strSQL="select * from  t_user where id="+id;
		try {
			st=conn.createStatement();			
			rs=st.executeQuery(strSQL);
			while(rs.next())
			{
				
				dto.setId(rs.getString("id"));	
				dto.setName(rs.getString("name"));
				dto.setPass(rs.getString("pass"));
			}
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			return null;
		} 
		finally
		{
    	 try {
    		 	rs.close();
    	        st.close();
    	        conn.close();
    	    } catch (SQLException ex1) {
    	        ex1.printStackTrace();
    	    }    	
		        }	
		
		return dto;

	}
	
	
	
	public int upbook(userDto dto)
	{
		
		Statement st=null;		
		String strSQL=null;
		Connection conn=DBConnection.getConnecton();	
		strSQL="update t_user set name='"+dto.getName()+"',pass='"+dto.getPass()+"'  where id='"+dto.getId()+"'";
		System.out.println(strSQL);
		int returnSts = 0;
		try {
					
			st=conn.createStatement();
			returnSts=st.executeUpdate(strSQL);
			returnSts=1;			
	   }
	catch(Exception e)
	{
		System.out.println(e.getMessage());
	}
	finally
	{
	 try {
		 
	        st.close();
	        conn.close();
	    } catch (SQLException ex1) {
	        ex1.printStackTrace();
	    }    	
	        }	
	return returnSts;
}
	
	public int delbook(String id)
	{
		
		Statement st=null;
		
		String strSQL=null;
		Connection conn=DBConnection.getConnecton();
	
		strSQL="delete from t_user where id="+id;
		System.out.println(strSQL);
		int returnSts = 0;
		try {
					
			st=conn.createStatement();
			returnSts=st.executeUpdate(strSQL);
			returnSts=1;
			
			
	}
	catch(Exception e)
	{
		System.out.println(e.getMessage());
	}
	finally
	{
	 try {
		 
	        st.close();
	        conn.close();
	    } catch (SQLException ex1) {
	        ex1.printStackTrace();
	    }    	
	        }	
	
	return returnSts;
    }
	
	public int insert(userDto dto) {
		
	   int i=0;   
	   Statement st=null;
	   Connection conn = DBConnection.getConnecton();
	   String sql="insert into t_user values('"+dto.getName()+"','"+dto.getPass()+"')";
	   try {
	      st=conn.createStatement();
	      i=st.executeUpdate(sql);
	      
	      } catch (SQLException ex) {
	          ex.printStackTrace();
	      } finally {          
	      	 try {
	      		 
	      		 st.close();
	               conn.close();
	           } catch (SQLException ex1) { }                              
	      }
	      return i;
			}


}

⌨️ 快捷键说明

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