qq_MEGALOVANIA_0
2018-10-23 13:30:12浏览 2060
package model;
import java.util.Date;
public class Student {
private String id;
private String name;
private Date birthday;
private int age;
private float score;
public Student(){}
public Student(String id, String name, Date birthday, int age, float score) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.age = age;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "id:" + id + " 姓名:" + name + " 生日:" + birthday + " 年领:" + age + " 成绩:" + score;
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package db;
import java.sql.*;
public class DBUtil {
private static final String url = "jdbc:mysql://localhost:3306/student?useSSL=false";
private static final String name = "root";
private static final String password = "123";
private static Connection conn =null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,name,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;
import db.DBUtil;
import model.Student;
public class StudentDao {
public void addStudent(Student s) throws SQLException{
Connection conn = DBUtil.getConnection();
String sql = "insert into student"+
"(id,name,age,score,birthday)"+
"value(?,?,?,?,?)";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1,s.getId());
ptmt.setString(2, s.getName());
ptmt.setInt(3, s.getAge());
ptmt.setFloat(4, s.getScore());
ptmt.setDate(5, new Date(s.getBirthday().getTime()));
ptmt.execute();
ptmt.close();
conn.close();
}
public void delStudent(String id) throws SQLException{
Connection conn = DBUtil.getConnection();
String sql = "delete from student where id=?";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1,id);
ptmt.execute();
ptmt.close();
conn.close();
}
public void updateStudent(String id,Student s) throws SQLException{
Connection conn = DBUtil.getConnection();
String sql = "update student set id=?,name=?,birthday=?,age=?,score=?"+
"where id=?";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1, s.getId());
ptmt.setString(2, s.getName());
ptmt.setDate(3,new Date(s.getBirthday().getTime()));
ptmt.setInt(4, s.getAge());
ptmt.setFloat(5, s.getScore());
ptmt.setString(6,id);
ptmt.execute();
ptmt.close();
conn.close();
}
public Student queryStudent(String id) throws SQLException{
Connection conn = DBUtil.getConnection();
String sql = "select * from student where id=?";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1,id);
ResultSet rs = ptmt.executeQuery();
Student s = null;
while(rs.next()){
s = new Student();
s.setId(rs.getString("id"));
s.setName(rs.getString("name"));
s.setAge(rs.getInt("age"));
s.setBirthday(rs.getDate("birthday"));
s.setScore(rs.getFloat("score"));
}
rs.close();
ptmt.close();
conn.close();
return s;
}
public List<Student> queryAllStudents() throws SQLException{
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from student";
ResultSet rs = stmt.executeQuery(sql);
List<Student> students =new ArrayList<Student>();
Student stu = null;
while(rs.next()){
stu = new Student();
stu.setId(rs.getString("id"));
stu.setName(rs.getString("name"));
stu.setBirthday(rs.getDate("birthday"));
stu.setAge(rs.getInt("age"));
stu.setScore(rs.getFloat("score"));
students.add(stu);
}
rs.close();
stmt.close();
conn.close();
return students;
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package Action;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import dao.StudentDao;
import model.Student;
public class StudentAction {
StudentDao sd = new StudentDao();
Scanner sc = new Scanner(System.in);
public void add() throws SQLException{
String id = "";
String name = "";
Date birthday = null;
int age = 0;
float score = 0;
System.out.print("输入学生id:");
if(sc.hasNext()){
id = sc.next();
}
System.out.print("输入学生姓名:");
if(sc.hasNext()){
name = sc.next();
}
System.out.print("输入学生生日:");
if(sc.hasNext()){
String dateString = sc.next();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
birthday = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
}
System.out.print("输入学生年龄:");
if(sc.hasNextInt()){
age = sc.nextInt();
}
System.out.print("输入学生成绩:");
if(sc.hasNextFloat()){
score = sc.nextFloat();
}
Student s = new Student(id,name,birthday,age,score);
sd.addStudent(s);
System.out.println("学生信息添加成功!");
}
public void del() throws SQLException{
String id = "";
System.out.print("输入要删除的学生的id:");
if(sc.hasNext()){
id = sc.next();
}
sd.delStudent(id);
System.out.println("删除成功!!!");
}
public void update() throws SQLException{
String id = "";
String name = "";
Date birthday = null;
int age = 0;
float score = 0;
String birthdaystr = null;
System.out.print("输入要修改的学生的id:");
if(sc.hasNext()){
id = sc.next();
}
System.out.print("输入修改的学生的姓名:");
if(sc.hasNext()){
name = sc.next();
}
System.out.print("输入要修改的学生的生日:");
if(sc.hasNext()){
birthdaystr = sc.next();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
birthday = sdf.parse(birthdaystr);
} catch (ParseException e) {
e.printStackTrace();
}
}
System.out.print("输入要修改的学生的年龄:");
if(sc.hasNext()){
age = sc.nextInt();
}
System.out.print("输入要修改的学生的成绩:");
if(sc.hasNextFloat()){
score = sc.nextFloat();
}
Student s = new Student(id,name,birthday,age,score);
sd.updateStudent(id, s);
System.out.print("修改完成!!");
}
public void query() throws SQLException{
String id = "";
System.out.print("输入要查询的学生的id:");
if(sc.hasNext()){
id = sc.next();
}
Student s = sd.queryStudent(id);
System.out.println("id:"+s.getId());
System.out.println("姓名:"+s.getName());
System.out.println("年龄:"+s.getAge());
System.out.println("生日:"+s.getBirthday());
System.out.println("成绩:"+s.getScore());
}
public void queryAll() throws SQLException{
List<Student> sl = new ArrayList<Student>();
sl = sd.queryAllStudents();
for(Student s:sl){
System.out.println(s.toString());
}
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package main;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import Action.StudentAction;
import dao.StudentDao;
import model.Student;
public class Test {
public static void main(String[] args) throws SQLException {
Scanner sc = new Scanner(System.in);
StudentAction sa = new StudentAction();
System.out.println(">>>>>>>>>>>>>>学生管理系统<<<<<<<<<<<<<<<");
System.out.println("1.新增学生信息
2.删除学生信息
3.修改学生信息
4.查找学生信息
5.显示全部学生信息
"
+ "6.输入EXIT/E退出");
System.out.print("输入查询的指令编号:");
do{
String order = sc.next();
if("EXIT".equals(order.toUpperCase())
||"EXIT".substring(0, 1).equals(order.toUpperCase())){
System.out.println(">>>>>>>>>>>>>>>>>系统已退出<<<<<<<<<<<<<<<<<<<");
break;
}
switch(order){
case "1":
sa.add();
break;
case "2":
sa.del();
break;
case "3":
sa.update();
break;
case "4":
sa.query();
break;
case "5":
sa.queryAll();
break;
default:
System.out.println("输入指令有误!");
}
}while(sc.hasNext());
}
}