1.学生类
package com.bean.Impl;
import com.bean.Interface.*;;
public class Student implements StudentItf{
private String stuName = null;
private String stuNum = null;
private String stuClass = null;
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getStuClass() {
return stuClass;
}
public void setStuClass(String stuClass) {
this.stuClass = stuClass;
}
}
2.操作系统连接类
package com.bean.sql; import java.sql.*; public class SqlCon { private Connection con = null; public Connection getCon(){ String url = "jdbc:mysql://localhost:3306/studentSystem"; try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("加载驱动成功"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("加载驱动失败"); } String username = "chenjunxian"; String password = "123456"; try { con = DriverManager.getConnection(url,username,password); System.out.println("数据库验证成功"); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("数据库验证失败"); } return con; } }
3.数据库操作类
package com.bean.Impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.bean.sql.SqlCon;
public class SqlOper implements com.bean.Interface.SqlOper {
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private Statement stmt = null;
public void close() {
// TODO Auto-generated method stub
try {
if(ps!=null)
{
ps.close();
ps = null;
}
if(rs!=null)
{
rs.close();
rs = null;
}
if(stmt!=null)
{
stmt.close();
stmt = null;
}
if(con!=null)
{
con.close();
con = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭失败");
}
}
public ArrayList<Student> nameSelect(Student stu) {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
String stuName = stu.getStuName();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuName = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuName);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
stuList.add(reStu);
}
System.out.println("使用名字查询成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("使用名字查询失败");
}
close();
return stuList;
}
public ArrayList<Student> classSelect(Student stu) {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
String stuClass = stu.getStuClass();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuClass = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuClass);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
stuList.add(reStu);
reStu = null;
}
System.out.println("使用班级查询成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("使用班级查询失败");
}
close();
return stuList;
}
public ArrayList<Student> numSelect(Student stu) {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
String stuNum = stu.getStuNum();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuNum = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuNum);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
System.out.println(rs.getString("stuName"));
stuList.add(reStu);
}
System.out.println("使用学号查询成功");
System.out.println("sqloper"+stuList.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("使用学号查询失败");
}
close();
return stuList;
}
public boolean stuInsert(Student stu) {
// TODO Auto-generated method stub
boolean isInsert = false;
this.con = new SqlCon().getCon();
String sql = "insert into student (stuName,stuNum,stuClass) values (?,?,?)";
try {
ps = this.con.prepareStatement(sql);
ps.setString(1, stu.getStuName());
ps.setString(2, stu.getStuNum());
ps.setString(3, stu.getStuClass());
ps.executeUpdate();
System.out.println("执行插入语句成功");
isInsert = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("执行插入语句失败");
}
close();
return isInsert;
}
public boolean stuDrop(Student stu) {
// TODO Auto-generated method stub
boolean isInsert = false;
this.con = new SqlCon().getCon();
String sql = "delete from student where stuNum = ?";
try {
ps = this.con.prepareStatement(sql);
ps.setString(1, stu.getStuNum());
ps.executeUpdate();
System.out.println("执行删除语句成功");
isInsert = true;
} catch (SQ