1:student、course类
package JDBCU; public class Student {
private String no;
private String name; public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
package JDBCU; public class Course {
private String no;
private String cname;
private float grades; public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public float getGrades() {
return grades;
}
public void setGrades(float grades) {
this.grades = grades;
} }
2 公共连接类
package JDBCU; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DatabaseConnection {
private static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/stuma";
public static final String DBNAME="root";
public static final String DBPWD="root";
private Connection conn;
public DatabaseConnection(){
try {
Class.forName(DBDRIVER);
this.conn=DriverManager.getConnection(DBURL,DBNAME,DBPWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
}
public Connection getconnection(){
return this.conn;
}
public void close(){
if(this.conn!=null){
try {
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
3、接口
package JDBCU; import java.util.List; public interface IScoreDao {
public Student findstu(String no);
public boolean updatestu(String no,String name);
public Course findc(String no,String cname);
public List<Course> findallc(String keyword);
public boolean addc(Course course);
public boolean delc(String no,String cname);
public boolean updatec(String no, String cname, float grades);
}
4、接口的实现
package JDBCU; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class ISocreServiceImpl implements IScoreDao {
private Connection conn=null;
private PreparedStatement pstmt=null; public ISocreServiceImpl(Connection conn) {
this.conn = conn;
} @Override
public Student findstu(String no) {
Student student=null;
String sql="select * from student where no=?";
try {
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,no);
ResultSet rs= this.pstmt.executeQuery();
if(rs.next()){
student=new Student ();
student.setNo(rs.getString(1));
student.setName(rs.getString(2));
}
this.pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
} @Override
public boolean updatestu(String no, String name) {
boolean flag=false;
String sql="update student set no=?,name=?";
try {
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,no);
this.pstmt.setString(2, name);
if(this.pstmt.executeUpdate()>0)
flag=true; this.pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
} @Override
public Course findc(String no, String cname) {
Course course=null;
String sql="select * from course where no=? and cname=?";
try {
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,no);
this.pstmt.setString(2,cname);
ResultSet rs= this.pstmt.executeQuery();
if(rs.next()){
course=new Course ();
course.setNo(rs.getString(1));
course.setCname(rs.getString(2));
course.setGrades(rs.getFloat(3));
}
this.pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return course;
} @Override
public List<Course> findallc(String keyword) {
List<Course>all=new ArrayList<Course>();
String sql="select * from course";
try {
this.pstmt=this.conn.prepareStatement(sql);
ResultSet rs= this.pstmt.executeQuery();
Course course=null;
while(rs.next()){
course=new Course ();
course.setNo(rs.getString(1));
course.setCname(rs.getString(2));
course.setGrades(rs.getFloat(3));
all.add(course);
}
this.pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return all;
} @Override
public boolean addc(Course course) {
boolean flag=false;
String sql="Insert into course(no,cname,grades)values(?,?,?)";
try{
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,course.getNo());
this.pstmt.setString(2, course.getCname());
this.pstmt.setFloat(3, course.getGrades());
if(this.pstmt.executeUpdate()>0)
flag=true;
this.pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return flag;
} @Override
public boolean delc(String no, String cname) {
boolean flag=false;
String sql="delete from course where no=? and cname=?";
try{
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,no);
this.pstmt.setString(2,cname);
if(this.pstmt.executeUpdate()>0)
flag=true;
this.pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return flag;
} @Override
public boolean updatec(String no, String cname, float grades) {
boolean flag=false; try {
String sql2="update course set no=?,cname=?,grades=? where no=? and cname=?";
this.pstmt=this.conn.prepareStatement(sql2);
this.pstmt.setString(1,no);
this.pstmt.setString(2, cname);
this.pstmt.setFloat(3, grades);
this.pstmt.setString(4,no);
this.pstmt.setString(5, cname);
if(this.pstmt.executeUpdate()>0)
flag=true; this.pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
}
5、回调
package JDBCU; import java.util.List; public class ISocreDaoImpl implements IScoreDao { private DatabaseConnection dbc=null;
private ISocreServiceImpl dao=null; public ISocreDaoImpl() {
this.dbc = new DatabaseConnection() ;
this.dao = new ISocreServiceImpl(this.dbc.getconnection());
} @Override
public Student findstu(String no) {
Student student=null;
try {
student=this.dao.findstu(no);
} catch (Exception e) { }finally{
dbc.close();
}
return student;
} @Override
public boolean updatestu(String no, String name) {
boolean flag=false;
try {
flag=this.dao.updatestu(no,name);
} catch (Exception e) { }finally{
dbc.close();
}
return flag;
} @Override
public Course findc(String no, String cname) {
Course course=null;
try {
course=this.dao.findc(no,cname);
} catch (Exception e) { }finally{
dbc.close();
}
return course;
} @Override
public List<Course> findallc(String keyword) {
List<Course> all=null;
try {
all=this.dao.findallc(keyword);
} catch (Exception e) { }finally{
dbc.close();
}
return all;
} @Override
public boolean addc(Course course) {
boolean flag=false;
try {
if(this.dao.findc(course.getNo(),course.getCname())==null)
flag=this.dao.addc(course);
} catch (Exception e) { }finally{
dbc.close();
}
return flag;
} @Override
public boolean delc(String no, String cname) {
boolean flag=false;
try {
flag=this.dao.delc(no,cname);
} catch (Exception e) { }finally{
dbc.close();
}
return flag;
} @Override
public boolean updatec(String no, String cname, float grades) {
boolean flag=false;
try {
flag=this.dao.updatec(no,cname,grades);
} catch (Exception e) { }finally{
dbc.close();
}
return flag;
} }
6、测试类以及菜单
package JDBCU; import java.util.Iterator;
import java.util.List;
import java.util.Scanner; public class ISocreTest {
public static void main(String[] args) {
menu();
} public static void menu() {
System.out.println("******************学生个人成绩管理系统***********");
System.out.printf("\n", null);
System.out.println(" 1、成绩管理");
System.out.println(" 2、学生管理");
System.out.println(" 3、退出系统");
System.out.printf("\n", null);
System.out.println("************************************************");
System.out.println("请输入功能编号(1-3):");
Scanner r0 = new Scanner(System.in);
int i = r0.nextInt();
switch(i){
case 1: grademenu(); break;
case 2: stumenu(); break;
case 3:
System.out.println("再见!");
break;
default:
System.out.println("请输入数字1到3");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
menu();
break;
}
r0.close();
}
private static void grademenu() {
System.out.println("******************学生个人成绩系统***************");
System.out.printf("\n", null);
System.out.println(" 1、增加成绩");
System.out.println(" 2、删除成绩");
System.out.println(" 3、修改成绩");
System.out.println(" 4、显示成绩");
System.out.println(" 5、汇总成绩");
System.out.println(" 6、返回上一系统");
System.out.printf("\n", null);
System.out.println("************************************************");
System.out.println("请输入功能编号(1-6):");
Course course=new Course();
Scanner r1 = new Scanner(System.in);
int s = r1.nextInt();
switch(s){
case 1:
try {
System.out.println("请输入信息,输入格式为学号,课程名。如:201220201032 math");
Scanner r2 = new Scanner(System.in);
String s1 =r2.nextLine();
String[] additem=s1.split(" ");
if(additem.length==2){
course.setNo(additem[0]);
course.setCname(additem[1]);
}
System.out.println("请输入分数");
float s2=r1.nextFloat();
course.setGrades(s2);
new ISocreDaoImpl().addc(course);
} catch (Exception e1) {
e1.printStackTrace();
}
main(null);
break;
case 2:
try {
System.out.println("请输入信息,输入格式为学号,课程名。如:201220201032 math");
Scanner r3 = new Scanner(System.in);
String s3 =r3.nextLine();
String[] additem1=s3.split(" ");
if(additem1.length==2){
new ISocreDaoImpl().delc(additem1[0],additem1[1]);
}
} catch (Exception e1) {
e1.printStackTrace();
}
main(null);
break;
case 3:
try {
System.out.println("请输入信息,输入格式为学号,课程名。如:201220201032 math");
Scanner r4 = new Scanner(System.in);
String s4 =r4.nextLine();
System.out.println("请输入新的分数");
Scanner r5 = new Scanner(System.in);
float s5=r5.nextFloat();
String[] additem2=s4.split(" ");
if(additem2.length==2){
if( new ISocreDaoImpl().findc(additem2[0],additem2[1])!=null)
new ISocreDaoImpl().updatec(additem2[0],additem2[1],s5);
}
} catch (Exception e1) {
e1.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
main(null); break;
case 4:
System.out.println("请输入信息,输入格式为学号,课程名。如:201220201032 math");
Scanner r6 = new Scanner(System.in);
String s6 =r6.nextLine();
String[] additem3=s6.split(" ");
if(additem3.length==2){
Course r= new ISocreDaoImpl().findc(additem3[0],additem3[1]);
if(r!=null){
System.out.println(r.getNo()+" "+r.getCname()+" "+r.getGrades());
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
main(null);
break;
case 5:
List<Course> all=new ISocreDaoImpl().findallc(" ");
Iterator<Course> iter=all.iterator();
while(iter.hasNext()){
course=iter.next();
System.out.println(course.getNo()+" "+course.getCname()+" "+course.getGrades());
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
main(null);
break;
case 6: menu(); break;
default:
System.out.println("请输入数字1到6");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stumenu();
break;
}
r1.close();
}
private static void stumenu() {
System.out.println("******************学生个人成绩管理系统***********");
System.out.printf("\n", null);
System.out.println(" 1、显示学生信息");
System.out.println(" 2、修改学生信息");
System.out.println(" 3、返回上一系统");
System.out.printf("\n", null);
System.out.println("************************************************");
System.out.println("请输入功能编号(1-3):");
Scanner r1 = new Scanner(System.in);
int t = r1.nextInt();
switch(t){
case 1:
System.out.println("请输入学号.如:201220201032");
Scanner r2 = new Scanner(System.in);
String s2=r2.nextLine();
Student r= new ISocreDaoImpl().findstu(s2);
if(r!=null){
System.out.println(r.getNo()+" "+r.getName());
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
main(null);
break;
case 2:
try {
System.out.println("请输入学号.如:201220201032");
Scanner r3 = new Scanner(System.in);
String s3=r3.nextLine();
if( new ISocreDaoImpl().findstu(s3)!=null)
{
System.out.println("请输入信息,输入格式为学号,姓名。如:201220201032 zhang");
Scanner r4 = new Scanner(System.in);
String s4 =r4.nextLine();
String[] additem=s4.split(" ");
if(additem.length==2){
new ISocreDaoImpl().updatestu(additem[0],additem[1]);
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
//延时
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
main(null);
break;
case 3:
menu();
break;
default:
System.out.println("请输入数字1到3");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stumenu();
break;
}
r1.close();
} }