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

📄 userdao.java

📁 j2se开发的即时聊天系统
💻 JAVA
字号:
package com.wczy.chatroom.server.database;

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

public class UserDAO {

	private Connection getConnection(){
		return DBOperator.getConnection();
	}
	private void closeConn(Connection conn){
		DBOperator.close(conn);
	}
	public int insert(User user){
		int ret=0;
		Connection conn=getConnection();
		try{
			PreparedStatement pstmt=conn.prepareStatement("insert into UserTable values(?,?)");
			try{
				pstmt.setString(1, user.getUserName());
				pstmt.setString(2, user.getPassword());
				ret=pstmt.executeUpdate();
			}finally{
				pstmt.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			closeConn(conn);
		}
		return ret;
		
	}
	public User findByUserName(String userName){
		User user=null;
		Connection conn=getConnection();
		try{
			PreparedStatement pstmt=conn.prepareStatement("select * from UserTable where UserName=?");
			try{
				pstmt.setString(1, userName);
				ResultSet rs=pstmt.executeQuery();
				if(rs.next()){
					user=new User();
					user.setUserName(userName);
					user.setPassword(rs.getString("password"));
				}
				rs.close();
			}finally{
				pstmt.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			closeConn(conn);
		}
		return user;
	}
	public int update(User user){
		int ret=0;
		Connection conn=getConnection();
		try{
			PreparedStatement pstmt=conn.prepareStatement("update UserTable set Password=? where UserName=?");
			try{
				pstmt.setString(1, user.getPassword());
				pstmt.setString(2, user.getUserName());
				ret=pstmt.executeUpdate();
			}finally{
				pstmt.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			closeConn(conn);
		}
		return ret;
	}
}

⌨️ 快捷键说明

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