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

📄 chatdao.java

📁 用Struts做的聊天系统
💻 JAVA
字号:
package com.chat.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.chat.ImplementDAO.Iuser;
import com.chat.utildata.ConnFactory;
import com.chat.vo.ChatUser;

public class Chatdao implements Iuser{
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	//这是向数据库里面插入一条用户数据库的方法
	public boolean insertuser(ChatUser user){
		boolean flag=false;
		long chat_userid=user.getChat_userid();
		String chat_username=user.getChat_username();
		String chat_userpassword=user.getChat_userpassword();
		String chat_usermail=user.getChat_usermail();
		String chat_usersex=user.getChat_usersex();
		String chat_useraddr=user.getChat_useraddr();
		String chat_userphone=user.getChat_userphone();
		
		String chat_userquestion=user.getChat_userquestion();
		String chat_useranswer=user.getChat_useranswer();
		
		try{
			conn=new ConnFactory().getFactory().getConn();
			pstmt=conn.prepareStatement("insert into chat_user values(?,?,?,?,?,?,?,?,?,?)");
			pstmt.setLong(1, chat_userid);
			pstmt.setString(2, chat_username);
			pstmt.setString(3, chat_userpassword);
			pstmt.setString(4, chat_usermail);
			pstmt.setString(5, chat_usersex);
			pstmt.setString(6, chat_useraddr);
			pstmt.setString(7, chat_userphone);
			pstmt.setInt(8, 0);
			pstmt.setString(9, chat_userquestion);
			pstmt.setString(10, chat_useranswer);
			int i=pstmt.executeUpdate();
			if(i!=0){
				flag=true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
				if(pstmt!=null){
					pstmt.close();
					pstmt=null;
				}
				if(conn!=null){
					conn.close();
					conn=null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	public boolean validateUser(ChatUser user) {
		boolean flag=false;
		String chat_username=user.getChat_username();
		String chat_userpassword=user.getChat_userpassword();
		String password;
		try{
			conn=new ConnFactory().getFactory().getConn();
			pstmt=conn.prepareStatement("select chat_userpassword from chat_user where chat_username='"+chat_username+"'");
			rs=pstmt.executeQuery();
			while(rs.next()){
				password=rs.getString(1);
				if(password!=null&&password.equals(chat_userpassword)){
					flag=true;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
				if(pstmt!=null){
					pstmt.close();
					pstmt=null;
				}
				if(conn!=null){
					conn.close();
					conn=null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	public boolean updatePassword(String chat_username,String chat_userpassword,String chat_userquestion,String chat_useranswer) {
		boolean flag=false;
		try{
			conn=new ConnFactory().getFactory().getConn();
			pstmt=conn.prepareStatement("update chat_user set chat_userpassword='"+chat_userpassword+"',chat_userquestion='"+chat_userquestion+"',chat_useranswer='"+chat_useranswer+"'where chat_username='"+chat_username+"'");
			int i=pstmt.executeUpdate();
			if(i!=0){
				flag=true;
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
				if(pstmt!=null){
					pstmt.close();
					pstmt=null;
				}
				if(conn!=null){
					conn.close();
					conn=null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	public boolean validateFindPassword(ChatUser user) {
		boolean flag=false;
		String chat_username=user.getChat_username();
		String chat_userquestion=user.getChat_userquestion();
		String chat_useranswer=user.getChat_useranswer();
		String question;
		String answer;
		try{
			conn=new ConnFactory().getFactory().getConn();
			pstmt=conn.prepareStatement("select chat_userquestion,chat_useranswer from chat_user where chat_username='"+chat_username+"'");
			rs=pstmt.executeQuery();
			while(rs.next()){
				question=rs.getString(1);
				answer=rs.getString(2);
				if(question.equals(chat_userquestion)&&answer.equals(chat_useranswer)){
					flag=true;
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
				if(pstmt!=null){
					pstmt.close();
					pstmt=null;
				}
				if(conn!=null){
					conn.close();
					conn=null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
}

⌨️ 快捷键说明

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