Java用于对数据库进行操作的dao

17 篇文章 1 订阅

本类用于对登录、注册、签到、查看学生信息和部分修改学生信息 等的数据库操作;登录,即将登录信息与数据库中存储的信息进行比较;注册,即将信息插入到数据库中,若数据库中已经存在信息则注册失败;

package com.dao;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import net.mah.util.Attendance;
import net.mah.util.DBUtil;
import net.mah.util.User;

public class UserDao {
    public static void main(String[] args) {
		ArrayList<User> user=UserDao.selectAll();
		for(User it:user) {
			System.out.println(it.getId()+" "+it.getName()+" "+it.getSex()+" "+it.getCla());
		}
}



public static int insert(String UserId,String Name,String Password,String sex,int clas) {
	int n=0;
	try {
		Connection con=(Connection) DBUtil.getConnection();
		String sql="insert into User (UserId,Name,Password,sex,class) values(?,?,?,?,?)";
		PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
		
		pst.setString(1,UserId);
		pst.setString(2,Name);
		pst.setString(3,Password);
		pst.setString(4,sex);
		pst.setInt(5,clas);
		
		n=pst.executeUpdate();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return n;
}

public static int insert1(String username,String name,String at) {
	int n=0;
	try {
		Connection con=(Connection) DBUtil.getConnection();
		String sql="insert into attendance (ID,Name,Attendance) values(?,?,?)";
		PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
		
		pst.setString(1,username);
		pst.setString(2,name);
		pst.setString(3,at);
		
		n=pst.executeUpdate();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return n;
}

public static int modify(String username,String newUsername) {
	int n=0;
	try {
		Connection con=(Connection) DBUtil.getConnection();
		String sql = "update User set UserId=? where UserId=?";
		PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
		pst.setString(1, newUsername);
		pst.setString(2, username);
        n = pst.executeUpdate();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return n;
}

public static ArrayList<User> selectAll() {
	ArrayList<User> user=new ArrayList<User>();
	try {
		Connection con=(Connection) DBUtil.getConnection();
		String sql="select * from User";
		PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
		ResultSet rst=pst.executeQuery();
		while(rst.next()) {
			User it=new User(rst.getString(1),rst.getString(2),rst.getString(3),
					rst.getString(4),rst.getString(5));
			user.add(it);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return user;
}

public static ArrayList<Attendance> selectAll1() {
	ArrayList<Attendance> user=new ArrayList<Attendance>();
	try {
		Connection con=(Connection) DBUtil.getConnection();
		String sql="select * from Attendance";
		PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
		ResultSet rst=pst.executeQuery();
		while(rst.next()) {
			Attendance it=new Attendance(rst.getString(1),rst.getString(2),rst.getString(3),
					rst.getString(4),rst.getString(5));
			user.add(it);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return user;
}

public static String select(String UserId) {
	String password=null;
	try {
		Connection con=(Connection) DBUtil.getConnection();
		String sql="select * from User where UserId=?";
		PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);
		pst.setString(1, UserId);
		ResultSet rst=pst.executeQuery();
		while(rst.next())
			password= rst.getString("Password");
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return password;
}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值