⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentinfo.java

📁 一个简单的学生信息查询系统
💻 JAVA
字号:
//StudentInfo.java 


import java.sql.*; 

public class StudentInfo { 
    String ID, Name, Sex, Classname, Homeaddr, Mobile, Diploma; 

    String Birthdate, Intime, Outtime; 

    public StudentInfo() { 
        try { 
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
        } catch (ClassNotFoundException e) { 
            System.out.println("连接数据库错误" + e); 
        } 
    } 

    // 根据学号查询信息 
    public void SearchByID(String ID) { 
        String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
        Connection con = null; 
        Statement stmt = null; 
        ResultSet rs = null; 
        String IDsearchStr = new String( 
        "select * from StudentInfo where Personalid='" + ID + "'"); 
        try { 
            con = DriverManager.getConnection(url); 
            stmt = con.createStatement(); 
            // 查询所输入学号对应的信息 
            rs = stmt.executeQuery(IDsearchStr); 
            if (rs.next()) { 
                this.ID = ID; 
                Name = rs.getString(2); 
                Sex = rs.getString(3); 
                Birthdate = rs.getString(4); 
                Classname = rs.getString(5); 
                Homeaddr = rs.getString(6); 
                Mobile = rs.getString(7); 
                Intime = rs.getString(8); 
                Outtime = rs.getString(9); 
                Diploma = rs.getString(10); 
            } else { 
                this.ID = "无此学号信息"; 
            } 
        } catch (SQLException e) { 
            this.ID = "查询出错"; 
            } finally { 
                if (rs != null) { 
                    try { 
                        rs.close(); 
                    } catch (SQLException ex) {   
                    } 
                } 
                if (stmt != null) { 
                    try { 
                        stmt.close(); 
                    } catch (SQLException ex) { 
                    } 
                } 
                if (con != null) { 
                    try { 
                        con.close(); 
                    } catch (SQLException ex) { 
                    } 
                } 
            } 
        } 

        // 查询特定ID是否存在 
        public boolean isExist(String ID) { 
        boolean isExist = false; 
        String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
        Connection con = null; 
        Statement stmt = null; 
        ResultSet rs = null; 
        String isExistStr = new String( "select * from StudentInfo where Personalid='" + ID + "'"); 
        try { 
            con = DriverManager.getConnection(url); 
            stmt = con.createStatement(); 
            // 查询所输入ID是否存在 
            rs = stmt.executeQuery(isExistStr); 
            if (rs.next()) { 
            isExist = true; 
        } 
        } catch (SQLException e) { 
            System.out.println("查询ID错误" + e); 
        } finally { 
            if (rs != null) { 
                try { 
                    rs.close(); 
                } catch (SQLException ex) { 
                } 
            } 
            if (stmt != null) { 
                try { 
                    stmt.close(); 
                } catch (SQLException ex) { 
                } 
            } 
            if (con != null) { 
                try { 
                    con.close(); 
                } catch (SQLException ex) { 
                } 
            } 
        } 
        return isExist; 
    } 

    // 更新信息 
    public boolean UpdateInfo(String ID, String Name, String Sex, 
        String Birthdate, String Classname, String Homeaddr, String Mobile, 
        String Intime, String Outtime, String Diploma) { 
        boolean update = false; 
        String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
        Connection con = null; 
        Statement stmt = null; 
        String UpdateStr = new String("update StudentInfo set Name='" + Name + "',Sex='" + Sex + "',Birthdate='" + Birthdate 
        + "',Classname='" + Classname + "',Homeaddr='" + Homeaddr + "',Mobile='" + Mobile + "',Intime='" + Intime + "',Outtime='" 
        + Outtime + "',Diploma='" + Diploma + "' where Personalid='" + ID + "'"); 
        try { 
            con = DriverManager.getConnection(url); 
            stmt = con.createStatement(); 
            // 更新信息 
            stmt.executeUpdate(UpdateStr); 
            update = true; 

        } catch (SQLException e) { 
            System.out.println("更新信息" + e); 
        } finally { 
            if (stmt != null) { 
                try { 
                    stmt.close(); 
                } catch (SQLException ex) { 
                } 
            } 
            if (con != null) { 
                try { 
                    con.close(); 
                } catch (SQLException ex) { 
                } 
            } 
        } 
        return update; 
    } 

    // 创建信息 
    public boolean CreateInfo(String ID, String Name, String Sex, 
    String Birthdate, String Classname, String Homeaddr, String Mobile, 
    String Intime, String Outtime, String Diploma) { 
        boolean create = false; 
        String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
        Connection con = null; 
        Statement stmt = null; 
        String CreateStr = new String("insert into StudentInfo values('" + ID + "','" + Name + "','" + Sex + "','" + Birthdate + "','" 
        + Classname + "','" + Homeaddr + "','" + Mobile + "','" + Intime + "','" + Outtime + "','" + Diploma + "')"); 
        try { 
            con = DriverManager.getConnection(url);   
            stmt = con.createStatement(); 
            // 创建信息 
            stmt.executeUpdate(CreateStr); 
            create = true; 

        } catch (SQLException e) { 
            System.out.println("创建信息" + e); 
        } finally { 
            if (stmt != null) { 
                try { 
                    stmt.close(); 
                } catch (SQLException ex) { 
                } 
            } 
            if (con != null) { 
                try { 
                    con.close(); 
                } catch (SQLException ex) { 
                } 
            } 
        } 
        return create; 
    } 

    // 删除信息 
    public boolean DeleteInfo(String ID) { 
        boolean delete = false; 
        String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
        Connection con = null; 
        Statement stmt = null; 
        String DeleteStr = new String( "delete from StudentInfo where Personalid='" + ID + "'"); 
        try { 
            con = DriverManager.getConnection(url); 
            stmt = con.createStatement(); 
            // 删除信息 
            stmt.executeUpdate(DeleteStr); 
            delete = true; 

        } catch (SQLException e) { 
            System.out.println("删除信息" + e); 
        } finally { 
            if (stmt != null) { 
                try { 
                    stmt.close(); 
                } catch (SQLException ex) { 
                } 
            } 
            if (con != null) { 
                try { 
                    con.close(); 
                } catch (SQLException ex) { 
                } 
            } 
        } 
        return delete; 
    } 

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -