该demo实现的功能有:
- 用户注册:
注册时输入两次密码,若两次输入不一致,则注册失败,需要重新输入。若用户名被注册过,则提示用户重新输入用户名; - 用户登录:
需要验证数据库中是否有对应的用户名和密码,若密码输错三次,则终止用户的登录操作; - 文件上传:
从本地上传文件到文件数据库中 - 文件下载:
从数据库中下载文件到本地 - 文件更新:
根据id可更新数据库中的文件名 - 文件删除:
根据id删除数据库中某一个文件 - 看数据库所有文件;
- 查看文件(根据用户名);
- 查看文件(根据文件id);
代码分为如下四个部分:Entity,Service,Socket,Util
实体包Entity中主要存放用户实体和文件实体
User类:
`package Entity;
import java.io.Serializable;
/**
* 用户类
* @author Administrator
*
*/
public class User implements Serializable{
//属性
private int id;
private String username;
private String password;
//方法
//构造方法
public User(){
super();
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
//Getter,Setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
文件类:
`package Entity;
import java.io.Serializable;
import java.util.Arrays;
/**
* 文件实体类
* @author Administrator
*
*/
public class FileEntity implements Serializable {
//属性
private int fid;
private String username;// 用户名,方便查看某个用户上传的文件
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String fname;
private byte[] fcontent;
//方法
//构造方法
public FileEntity(){
super();
}
public FileEntity(String username,String fname, byte[] fcontent) {
super();
this.username=username;
this.fname = fname;
this.fcontent = fcontent;
}
//Getter,Setter方法
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public byte[] getFcontent() {
return fcontent;
}
public void setFcontent(byte[] fcontent) {
this.fcontent = fcontent;
}
@Override
public String toString() {
return "FileEntity [fid=" + fid + ", username=" + username + ", fname="
+ fname + "]";
}
}
Service包:包括用户服务类和文件服务类
UserSevice类包括了与用户相关的注册和登录功能相关的方法
package Service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import Util.DBUtil;
import Entity.User;
public class UserService {
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
/**
* 登陆
*/
public boolean login(User user){
String sql="select * from tb_user where username=? and password=?";
try{
conn=DBUtil.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
if(rs.next()){
return true;
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBUtil.CloseAll(rs, pstmt, conn);
}
return false;
}
/**
* 注册
*/
public void register(User user){
String sql="insert into tb_user(username,password)value(?,?)";
try {
conn=DBUtil.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.CloseAll(rs, pstmt, conn);
}
}
/**
* 查找tb_user,查看用户是否已经注册过
*/
public boolean queryByUsername(String username){
conn=DBUtil.getConnection();
String sql="select * from tb_user where username=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, username);
rs=pstmt.executeQuery();
if(rs.next()){
//说明已经被注册过,要求重新填写用户名
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
文件服务类:
package Service;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import Entity.FileEntity;
import Util.DBUtil;
public class FileService {
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
/**
* 保存文件到数据库,类似用户的注册功能
* @param fileEntity
*/
public void saveFile(FileEntity fileEntity){
String sql="insert into tb_file(username,fname,fcontent)value(?,?,?)";
try {
conn=DBUtil.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,fileEntity.getUsername());
pstmt.setString(2, fileEntity.getFname());
pstmt.setBytes(3, fileEntity.getFcontent());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.CloseAll(rs, pstmt, conn);
}
}
//查看数据库中的所有文件信息
public List<FileEntity> queryAll() throws SQLException{
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder(" select * from tb_file");
pstmt=conn.prepareStatement(sb.toString());
rs=pstmt.executeQuery();
List<FileEntity> fety=new ArrayList<FileEntity>();
while(rs.next()){
FileEntity file=new FileEntity();
file.setFid(rs.getInt("fid"));
file.setUsername(rs.getString("username"));
file.setFname(rs.getString("fname"));
file.setFcontent(rs.getBytes("fcontent"));
fety.add(file);
}
return fety;
}
//根据用户名查看用户上传的文件
public List<FileEntity> queryFile(String user_name) throws SQLException{
Connection conn = DBUtil.getConnection();
StringBuilder sb=new StringBuilder(" select * from tb_file");
sb.append(" where username = ? ");
pstmt=conn.prepareStatement(sb.toString());
pstmt.setString(1, user_name);
// System.out.println(sb.toString());
rs = pstmt.executeQuery();
List<FileEntity> fety =new ArrayList<FileEntity>();
while (rs.next()) {
FileEntity file=new FileEntity();
file.setFid(rs.getInt("fid"));
file.setUsername(rs.getString("username"));
file.setFname(rs.getString("fname"));
file.setFcontent(rs.getBytes("fcontent"));
fety.add(file);
}
return fety;
}
//根据id查找文件
public FileEntity queryFileByFid(int fid) throws SQLException {
conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder("");
sb.append(" select * from tb_file where fid=? ");
pstmt=conn.prepareStatement(sb.toString());
pstmt.setInt(1, fid);
// System.out.println(sb.toString());
rs=pstmt.executeQuery();
FileEntity file=null;
while(rs.next()){
file=new FileEntity();
file.setFid(rs.getInt("fid"));
file.setUsername(rs.getString("username"));
file.setFname(rs.getString("fname"));
file.setFcontent(rs.getBytes("fcontent"));
}
return file;
}
//删除文件
public void deleteFile(int fid) throws SQLException {
conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder("");
sb.append(" delete from tb_file where fid=? ");
pstmt=conn.prepareStatement(sb.toString());
pstmt.setInt(1,fid);
pstmt.execute();
}
//更新文件
public void updateFile(FileEntity file) throws SQLException{
conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder("");
sb.append(" update tb_file set fname=? where fid=? ");
pstmt=conn.prepareStatement(sb.toString());
pstmt.setString(1,file.getFname());
pstmt.setInt(2,file.getFid());
pstmt.execute();
}
}