package com.yinhe.operate;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.yinhe.model.ConnDB;
import com.yinhe.model.StudentInfo;
import com.yinhe.model.UserInfo;
//操作数据库类
public class OperateDB extends ConnDB {
Connection conn;
Statement st;
PreparedStatement ps;
ResultSet rs;
//根据 学号查询学生信息 方法
public StudentInfo selectStudById(String id){
conn = this.getConn();
StudentInfo stInfo = null;
try {
st = conn.createStatement();
rs = st.executeQuery("select * from 学生信息表 where 学号='" + id + "'");
//操作结果集 得到学生信息
while(rs.next()){
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
String mz = rs.getString(5);
Date birthday = rs.getDate(6);
String zzmm = rs.getString(7);
String tel = rs.getString(8);
String address = rs.getString(9);
stInfo = new StudentInfo(id,name,sex,age,mz,birthday,zzmm,tel,address);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally{
this.close();
}
return stInfo;
}
// 定义方法 查询学生信息表中的所有信息
public ArrayList<StudentInfo> getAllStudentInfo() {
ArrayList<StudentInfo> al = new ArrayList<StudentInfo>();
StudentInfo stInfo = null;
// 连接数据库
conn = this.getConn();
// 得到Statement对象的实例
try {
st = conn.createStatement();
// 执行SQL语句 并返回查询结果给ResultSet
// executeQuery()方法:执行查询语句
rs = st.executeQuery("select * from 学生信息表");
// 操作结果集
while (rs.next()) {
String id = rs.getString(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
String mz = rs.getString(5);
Date birthday = rs.getDate(6);
String zzmm = rs.getString(7);
String tel = rs.getString(8);
String address = rs.getString(9);
stInfo = new StudentInfo(id,name,sex,age,mz,birthday,zzmm,tel,address);
//将对象放入集合中
al.add(stInfo);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
this.close();
}
return al;
}
// 定义方法 查询学生信息表中的所有女性
public void getAllWomenInfo() {
// 连接数据库
conn = this.getConn();
try {
st = conn.createStatement();
rs = st.executeQuery("select * from 学生信息表 where 性别 = '女'");
while (rs.next()) {
System.out.println("学号:" + rs.getInt(1) + " 姓名:"
+ rs.getString(2) + " 性别:" + rs.getString(3) + " 年龄:"
+ rs.getInt(4) + " 民族:" + rs.getString(5));
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
this.close();
}
//增加一条数据
public int addStudentInfo(){
int count = 0;
//连接数据库
conn = this.getConn();
try {
//得到Statement 的实例
st = conn.createStatement();
//增加数据
count = st.executeUpdate("insert into 学生信息表(学号,姓名,性别) values(10022,'学友','男')");
System.out.println(count);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
this.close();
return count;
}
//删除一条数据(根据学号)
public int deleteStudentInfo(int id){
int count = 0;
//连接数据库
conn = this.getConn();
try {
st = conn.createStatement();
count = st.executeUpdate("delete * from 学生信息表 where 学号 = " + id);
System.out.println(count);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
this.close();
return count;
}
//删除政治面貌为党员的数据
public int deleteDYInfo(String dy){
int count = 0;
conn = this.getConn();
try {
st = conn.createStatement();
count = st.executeUpdate("delete * from 学生信息表 where 政治面貌 = '" + dy + "'");
System.out.println(count);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
this.close();
return count;
}
//修改数据
public int updateInfo(int id){
int count = 0;
conn = this.getConn();
try {
st = conn.createStatement();
count = st.executeUpdate("update 学生信息表 set 年龄 = 50 where 学号 = " + id);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
this.close();
}
return count;
}
//修改数据(将政治面貌为团员的改为党员)
public int updateInfo(String dy){
int count = 0;
conn = this.getConn();
try {
st = conn.createStatement();
count = st.executeUpdate("update 学生信息表 set 政治面貌 = '" + dy + "' where 政治面貌 = '团员'");
//System.out.println("update 学生信息表 set 政治面貌 = '" + dy + "' where 政治面貌 = '团员'");
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
this.close();
}
return count;
}
//定义 验证登录方法
public boolean selectLogin(String name,String pwd){
conn = this.getConn();
try {
st = conn.createStatement();
//根据用户输入的用户名查询密码
rs = st.executeQuery("select 密码 from loginRegTB where 姓名='" + name + "'");
while(rs.next()){
if(rs.getString("密码").equals(pwd)){//如果用户输入的密码和数据库中的密码相同 返回 true
return true;
}
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return false;
}
//定义 增加用户信息 方法
public int insertUserInfo(UserInfo info){
int x = 0;
conn = this.getConn();
try {
st = conn.createStatement();
x = st.executeUpdate("insert into loginRegTB values('" + info.getName()
+ "','" + info.getPwd() + "',"+ info.getAge() + ",'"
+ info.getEmail() + "','" + info.getInfo() + "')");
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally{
this.close();
}
return x;
}
//用PreparedStatement 添加学生信息,,
public int addStInfo(StudentInfo stInfo){
int x = 0;
conn = this.getConn();
try {
ps = conn.prepareStatement("insert into 学生信息表 values(?,?,?,?,?,?,?,?,?)");
ps.setString(1, stInfo.getId());
ps.setString(2, stInfo.getName());
ps.setString(3, stInfo.getSex());
ps.setInt(4, stInfo.getAge());
ps.setString(5, stInfo.getMz());
ps.setDate(6, (java.sql.Date) stInfo.getBirthday());
ps.setString(7, stInfo.getZzmm());
ps.setString(8, stInfo.getTel());
ps.setString(9, stInfo.getAddress());
x = ps.executeUpdate();
if (x != 0) {
System.out.println("信息添加成功");
} else {
System.out.println("信息添加失败");
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
this.close();
}
return x;
}
//用PreparedStatement 添加学生信息,,
public int updateStInfo(StudentInfo stInfo){
int x = 0;
conn = this.getConn();
try {
ps = conn.prepareStatement("");
ps.setString(1, stInfo.getId());
ps.setString(2, stInfo.getName());
ps.setString(3, stInfo.getSex());
ps.setInt(4, stInfo.getAge());
ps.setString(5, stInfo.getMz());
ps.setDate(6, (java.sql.Date) stInfo.getBirthday());
ps.setString(7, stInfo.getZzmm());
ps.setString(8, stInfo.getTel());
ps.setString(9, stInfo.getAddress());
x = ps.executeUpdate();
if (x != 0) {
System.out.println("信息添加成功");
} else {
System.out.println("信息添加失败");
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
this.close();
}
return x;
}
// 定义方法,关闭连接
public void close() {
// 关闭连接
try {
if (!conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {