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