学生管理系统实验报告

实 验 报 告

课程名称:数据库实验      实验项目:  综合应用         

学生管理系统实验报告

一 需求分析

在如今的高校日常管理当中,学生管理系统就是其中非常重要的一环,特别是当前学生规模大,课程门类多,校区分散等实际情况,学生管理更具有非常大的实际应用意义。但是,我们看到,其实我们学校的学生管理在很多方面做的并不是很完善。管理成为学校教学管理中十分重要又相当复杂的管理工作之一,单纯的采用传统的手工处理已经不符合教育和管理的要求,而计算机具有运算速度快,处理能力强等特点,很自然地进入到这一应用领域中。因此为了保证学校的信息流畅,工作高效,有必要设计一个学生成绩管理系统。这不但能使教务人员从复杂的成绩管理中解脱出来,而且对于推动教学的发展也起到非常重要的作用。 

二 ER图

学生管理系统实验报告

三 创建数据库的步骤

使用SQL创建数据库,如下命令:

Create table student(id char(20),name char(20),gender char(2),address char(50),phone char(20),major text(30));

四 用JAVA开发系统前端的步骤过程

1、学生管理系统主界面(StudentManagement

/核心代码/

public class EX12_5_StudentManagement extends JFrame implements ActionListener{

  JMenuBar bar=null;

  JMenu menu1,menu2,menu3,menu4,menu5;

  JMenuItem item1,item2,item3,item4,item5;

  EX12_6_StudentAdd zengjia;

  EX12_7_StudentQuery chaxun;

  EX12_8_StudentUpdate gengxin;

  EX12_9_StudentDelete shanchu;

……

  public void actionPerformed(ActionEvent e){

   if(e.getSource()==item1){

     this.getContentPane().removeAll();

       this.getContentPane().add(zengjia,"Center");

       this.getContentPane().repaint();

    this.getContentPane().validate();

   }

   if(e.getSource()==item2){

       this.getContentPane().removeAll();

       this.getContentPane().add(chaxun,"Center");

       this.getContentPane().repaint();   

    this.getContentPane().validate();

   }

   if(e.getSource()==item3){

       this.getContentPane().removeAll();

       this.getContentPane().add(gengxin,"Center");

       this.getContentPane().repaint();   

    this.getContentPane().validate();

   }

   if(e.getSource()==item4){

    this.getContentPane().removeAll();

       this.getContentPane().add(shanchu,"Center");

       this.getContentPane().repaint();   

    this.getContentPane().validate();

   }

   if(e.getSource()==item5){

       

     System.exit(0);  

   }

  }

  public static void main(String[] args)

  {

        

   EX12_5_StudentManagement stuM=new EX12_5_StudentManagement();

   stuM.setVisible(true);

  

   stuM.addWindowListener(new WindowAdapter(){

    public void windowClosing(WindowEvent e){

     System.exit(0);

    }

   });

  }

}

2、学生信息录入界面(StudentAdd

/核心代码/

EX12_6_StudentAdd(){

   try{

    Class.forName("com.mysql.jdbc.Driver");

    }

   catch(ClassNotFoundException e){}

    try{

     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc","root","123456");

     sql=con.createStatement();

    

    }

    catch(SQLException ee){}

   

   ……

  }

  public void actionPerformed(ActionEvent e){

   if(e.getSource()==b1){

    try{ insert();}

    catch(SQLException ee){}

    JOptionPane.showMessageDialog(this,"数据已入库!","提示对话框",JOptionPane.INFORMATION_MESSAGE);

   }

   else if(e.getSource()==b2){

       tf1.setText(" ");

       tf2.setText(" ");

       tf3.setText(" ");

       tf4.setText(" ");

       tf5.setText(" ");

       tf6.setText(" ");

   }

  }

  public void insert() throws SQLException{

   String s1="'"+tf1.getText().trim()+"'";

   String s2="'"+tf2.getText().trim()+"'";

   String s3="'"+tf3.getText().trim()+"'";

   String s4="'"+tf4.getText().trim()+"'";

   String s5="'"+tf5.getText().trim()+"'";

 String s6="'"+tf6.getText().trim()+"'";

  

    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc","root","123456");

  

    String temp="INSERT INTO student VALUES ("+s1+","+s2+","+s3+","+s4+","+s5+","+s6+")";

    sql.executeUpdate(temp);

   con.close();

  }

}

3、学生信息查询界面(StudentQuery

/核心代码/

EX12_7_StudentQuery(){

   try{

    Class.forName("com.mysql.jdbc.Driver");

    }

   catch(ClassNotFoundException e){}

    try{

     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc","root","123456");

     sql=con.createStatement();

    }

    catch(SQLException ee){}

   

    ……

  }

  public void actionPerformed(ActionEvent e){

   flag=0;

   try{query();}

   catch(SQLException ee){}

  }

  public void query() throws SQLException{

   String num,name,gender,address,phone,major;

   con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc","root","123456");

   num=t1.getText().trim();

   ResultSet rs=sql.executeQuery("SELECT * FROM student where id='"+num+"'");

  

   if(rs.next()){

    //num=rs.getString("ID");

    name=rs.getString("name");

    gender=rs.getString("gender");

    address=rs.getString("address");

    phone=rs.getString("phone");

    major=rs.getString("major");

    //if(num.equals(t1.getText().trim())){

     t2.setText(name);

     t3.setText(gender);

     t4.setText(address);

     t5.setText(phone);

     t6.setText(major);

     flag=1;

     //break;

    //}

   }else

   {

           JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);

   }

   con.close();

   if(flag==0){t1.setText("没有该学生");}

  }

}

4、学生信息更新界面(StudentUpdate

/核心代码/

EX12_8_StudentUpdate(){

   try{

    Class.forName("com.mysql.jdbc.Driver");

    }

   catch(ClassNotFoundException e){}

    try{

     con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc","root","123456");

     sql=con.createStatement();

    }

    catch(SQLException ee){}

   ……

  }

  public void actionPerformed(ActionEvent e){

   if(e.getSource()==b1){

       try{

        String num,name,gender,address,phone,major;

           con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc","root","123456");

           num=t1.getText().trim();

           ResultSet rs=sql.executeQuery("SELECT * FROM student where id='"+num+"'");

           if(rs.next()){

            //num=rs.getString("id");

            name=rs.getString("name");

            gender=rs.getString("gender");

            address=rs.getString("address");

            phone=rs.getString("phone");

            major=rs.getString("major");

            //if(num.equals(t1.getText().trim())){

             t2.setText(name);

             t3.setText(gender);

             t4.setText(address);

             t5.setText(phone);

             t6.setText(major);

             //break;

            //}

        }else{

            JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);           

        }

          

           con.close();

        }

        catch(SQLException ee){}

   }

   if(e.getSource()==b2){

    try{update();}

    catch(SQLException ee){}

   }

   if(e.getSource()==b3){

    t2.setText(" ");

    t3.setText(" ");

    t4.setText(" ");

    t5.setText(" ");

    t6.setText(" ");

   }

  }

  public void update() throws SQLException{

   String s1="'"+t1.getText().trim()+"'";

   String s2="'"+t2.getText().trim()+"'";

   String s3="'"+t3.getText().trim()+"'";

   String s4="'"+t4.getText().trim()+"'";

   String s5="'"+t5.getText().trim()+"'";

   String s6="'"+t6.getText().trim()+"'";

   String temp="UPDATE student SET name ="+s2+", gender="+s3+", address="+s4+", phone="+s5+", major="+s6+" WHERE id="+s1;

   con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc", "root", "123456");

   sql.executeUpdate(temp);

   JOptionPane.showMessageDialog(this,"修改成功!","提示对话框",JOptionPane.INFORMATION_MESSAGE);              

   con.close();

  }

}

5、学生信息删除界面(StudentDelete

/核心代码/

       EX12_9_StudentDelete() {

              try {

                     Class.forName("com.mysql.jdbc.Driver");

              } catch (ClassNotFoundException e) {

              }

              try {

                     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc", "root", "123456");

                     sql = con.createStatement();

              } catch (SQLException ee) {

              }

              ……

       }

       public void actionPerformed(ActionEvent e) {

              if (e.getSource() == t1) {

                     try {

                            delete();

                     } catch (SQLException ee) {

                     }

              } else if (e.getSource() == b) {

                    

                     try {

                            delete();

                     } catch (SQLException ee) {

                     }

                    

                     int n = JOptionPane.showConfirmDialog(this, "确定要删除该学生的全部信息吗?",

                                   "确定", JOptionPane.YES_NO_OPTION);

                     if (n == JOptionPane.YES_OPTION) {

                            try {

                                   String s1 = "'" + t1.getText().trim() + "'";

                                   String temp = "DELETE FROM student WHERE id=" + s1;

                                   System.out.println(temp);

                                   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc", "root", "123456");

                                   sql.executeUpdate(temp);

                                   JOptionPane.showMessageDialog(this, "删除成功!", "提示对话框",

                                                 JOptionPane.INFORMATION_MESSAGE);

                                   con.close();

                            } catch (SQLException ee) {

                            }

                     } else if (n == JOptionPane.NO_OPTION) {

                     }

              }

       }

       public void delete() throws SQLException {

              String num, name, gender, address, phone, major;

              con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xskc", "root", "123456");

              num = t1.getText().trim();

              ResultSet rs = sql.executeQuery("select  * FROM student where id='"

                            + num + "'");

              if (rs.next()) {

                     // num=rs.getString("id");

                     name = rs.getString("name");

                     gender = rs.getString("gender");

                     address = rs.getString("address");

                     phone = rs.getString("phone");

                     major = rs.getString("major");

                     // if(num.equals(t1.getText().trim())){

                     t2.setText(name);

                     t3.setText(gender);

                     t4.setText(address);

                     t5.setText(phone);

                     t6.setText(major);

                     // break;

                     // }

              } else {

                     JOptionPane.showMessageDialog(this, "没有该学生!", "提示对话框",

                                   JOptionPane.INFORMATION_MESSAGE);

              }

              con.close();

       }

}

五 连接数据库步骤

各步骤简单描述如下:

(1)加载驱动程序。为数据库管理系统加载一个JDBC驱动程序。通过   Class.forName()方法调用来完成这一操作,需要给出驱动类的名称。

(2)打开数据库连接。使用驱动程序打开特定数据库的连接。

(3)获取Statement对象。

(4)执行SQL语句。通过Statement提出SQL请求。

(5)处理结果集。如果返回结果集则对结果集进行处理。

(1)加载驱动程序

通过类装载器来加载相应的数据库驱动程序,格式为:

Class.forName(driverName)

import java.sql.*;

public class EX12_1_StudentSQL {

       public static void main(String[] args) {

         Connection con=null;

                Statement stmt=null;

                String strTemp = "";

              try{

                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                         }

              catch(ClassNotFoundException e){}

       }

(2)建立连接

通过DriverManager:

Connection con=DriverManager.getConnection(url,user,password);

(3)获取statement

statement用来发送要执行的sql语句。有三种Statement对象。

①Statement

执行不带参数的sql语句。创建方法:

connection.createStatement()

②PreParedStatement

执行带参数或不带参数的预编译的SQL语句,下次执行的时候就不要编译和优化了。

创建方法:

只有SELECT语句才会有结果集返回。代码片断如下:

while(rs.next()){//rs是一个游标,初始时在第一条记录的上面一行。

//每next一次,向下一行。

rs.getString(1);

rs.getInt(2);

}

可以使用位置标识,也可以使用列名来标识(当结果集字段比较多的时候用列名标识法,可以增强程序的可读性)。

最终代码

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/**

 * 1首先要安装有JDK。

 * 2然后安装MySQL。

 * 3配置好这两个环境后,下载JDBC驱动mysql-connector-java-5.0.5.zip。

 * 然后将其解压缩到任一目录。然后将其目录下的mysql-connector-java-5.0.5-bin.jar加到classpath里,

 * 4环境配置好了。现在,先配置Java连接MySQL,设其用户名为“root”,密码为“123456”。

 * 在命令行或用一个SQL的前端软件创建Database。

 * @author hj

 *

 */

public class JDBCTest {

       public static Connection getConnection(){ 

        String driver="com.mysql.jdbc.Driver";   //获取mysql数据库的驱动类 

        String url="jdbc:mysql://localhost:3306/xskc"; //连接数据库(test是数据库名)  

        String name="root";//连接mysql的用户名 

        String pwd="123456";//连接mysql的密码 

        try{ 

            Class.forName(driver);  //1. 加载驱动程序

            Connection conn=DriverManager.getConnection(url,name,pwd);//2. 打开数据库连接

            return conn; 

        }catch(ClassNotFoundException e){ 

            e.printStackTrace(); 

            return null; 

        }catch(SQLException e){ 

            e.printStackTrace(); 

            return null; 

        } 

    } 

     

    public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){ 

        try{ 

            if(rs!=null){ 

                rs.close(); 

            } 

        }catch(SQLException e){ 

            e.printStackTrace(); 

        } 

        try{ 

            if(ps!=null){ 

                ps.close(); 

            } 

        }catch(SQLException e){ 

            e.printStackTrace(); 

        } 

        try{ 

            if(conn!=null){ 

                conn.close(); 

            } 

        }catch(SQLException e){ 

            e.printStackTrace();     

        } 

    } 

     

    public static void main(String[] args) throws SQLException 

    { 

         

        Connection cc=JDBCTest.getConnection(); 

         

        if(!cc.isClosed()) 

        System.out.println("Succeeded connecting to the Database!"); 

        Statement statement = cc.createStatement();  //3. 获取statement对象

        String sql = "select * from student"; 

        ResultSet rs = statement.executeQuery(sql);  //4. 执行SQL语句

        while(rs.next()) {                           //5. 处理结果集

            System.out.println(rs.getString("Sno")+""); 

        } 

    } 

}

六 小结

数据库的时候出现了错误:

1  Connection refused(DESCRIPTION=(TMP=)(VSNNUM=153093120)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))

dbc连接数据库的时候,需要使用数据库的sid_name,而不是数据库的services_name
而使用plsql连接数据库的时候,只需要数据库的services_name即可,所以修改连接字符串中的services_name 为sid_name

2  JDBC操作ORACLE数据库时出现‘java.sql.SQLException:IO异常,不在流模式下’

   解决方法setAutoCommit失败,该连接并重新建立一个连接

3  在学生信息系统录入,查询,更新的代码中,最后的sql.executeQuerty(temp);改为sql.executeUqdate(temp);此时就能正常执行程序功能了。

相关推荐