📄 studentdao.java
字号:
package cn.andy.dao;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import cn.andy.bean.Student;
import cn.andy.db.DbCon;
public class StudentDao {
private Connection conn;
private Statement sta;
private ResultSet rs;
/**
* 返回所有学员对象
*
* @return
*/
public ArrayList getAll() throws Exception {
conn = DbCon.getCon();
sta = conn.createStatement();
ArrayList list = new ArrayList();
String sql = "select * from student ";
rs = sta.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
Student temp = new Student(id, name, sex, age);
list.add(temp);
}
return list;
}
/**
* 添加学员信息
*
* @param id
* @param name
* @param sex
* @param age
* @param classId
* @throws SQLException
* @throws ClassNotFoundException
*/
public boolean addStu(int id, String name, String sex, int age) {
try {
conn = DbCon.getCon();
sta = conn.createStatement();
// String sql = "insert into student values (" + id + ",'" + name
// + "','" + sex + "'," + age + ",'" + ")";
String sql = "insert into student values(" + id + ",'" + name
+ "','" + sex + "'," + age + ")";
sta.executeUpdate(sql);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/***************************************************************************
* 删除学习信息
*
* @param id
* @return
*/
public boolean deleteStu(int id) {
try {
conn = DbCon.getCon();
sta = conn.createStatement();
String sql = "delete from student where STU_ID = " + id;
sta.executeUpdate(sql);
return true;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return false;
}
/***************************************************************************
* 根据ID查找学生信息
*
* @param id
* @return
*/
public ArrayList seleteStuForId(int idTemp) throws Exception {
ArrayList list = new ArrayList();
conn = DbCon.getCon();
sta = conn.createStatement();
String sql = "select * from student where STU_ID = " + idTemp;
ResultSet rs = sta.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
Student temp = new Student(id, name, sex, age);
list.add(temp);
}
return list;
}
/***************************************************************************
* 根据姓名查找
*
* @param strTemp
* @return
* @throws Exception
*/
public ArrayList seleteStuForName(String strTemp) throws Exception {
ArrayList list = new ArrayList();
conn = DbCon.getCon();
sta = conn.createStatement();
String sql = "select * from student where stu_name = '" + strTemp + "'";
ResultSet rs = sta.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
Student temp = new Student(id, name, sex, age);
list.add(temp);
}
return list;
}
/***************************************************************************
* 根据性别查找
*
* @param strTemp
* @return
* @throws Exception
*/
public ArrayList seleteStuForSex(String strTemp) throws Exception {
ArrayList list = new ArrayList();
conn = DbCon.getCon();
sta = conn.createStatement();
String sql = "select * from student where STU_SEX = '" + strTemp + "'";
ResultSet rs = sta.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
Student temp = new Student(id, name, sex, age);
list.add(temp);
}
return list;
}
/***************************************************************************
* 根据年龄查找
*
* @param strTemp
* @return
* @throws Exception
*/
public ArrayList seleteStuForAge(int strTemp) throws Exception {
ArrayList list = new ArrayList();
conn = DbCon.getCon();
sta = conn.createStatement();
String sql = "select * from student where STU_AGE = " + strTemp;
ResultSet rs = sta.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
Student temp = new Student(id, name, sex, age);
list.add(temp);
}
return list;
}
/***************************************************************************
* 修改学生信息
*
* @param id
* @param name
* @param sex
* @param age
* @param classId
* @return
*/
public boolean ReviseStu(int id, String name, String sex, int age,
String classId) {
try {
conn = DbCon.getCon();
sta = conn.createStatement();
String sqlName = "update student set name = '" + name
+ "'where id = " + id;
String sqlSex = "update student set sex = '" + sex + "'where id = "
+ id;
String sqlAge = "update student set age = '" + age + "'where id = "
+ id;
String sqlClassId = "update student set myclassid = '" + classId
+ "'where id = " + id;
sta.executeUpdate(sqlName);
sta.executeUpdate(sqlSex);
sta.executeUpdate(sqlAge);
sta.executeUpdate(sqlClassId);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -