java+sqlserver實現學生信息管理系統

前提:

1.建立瞭與sqlserver數據庫的連接
(JTDS連接sqlserver數據庫的包jtds-1.2.7.jar)
2. 瞭解JDBC執行SQL的語法

一.實現效果

在這裡插入圖片描述

在這裡插入圖片描述

二.實現代碼

1.DBUtil.java

說明:直接復制必然出錯。

因為要連接自己的數據庫,其中部分數據說明:

            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
//本機V8 ip   192.168.223.1  
//在數據庫中建立的一個登錄名 admin 
//登錄名admin 的密碼 123123
//要連接的數據庫 物流寄存 (因為是臨時作業就先隨便找個數據庫放瞭)

DBUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DBUtil
{
    //連接數據庫
    private static Connection getSQLConnection(String ip, String user, String pwd, String db)
    {
        Connection con = null;
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            //con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db + ";charset=utf8", user, pwd);
            //jdbc:jtds:sqlserver://localhost:1433/dbname
            //解決輸出中文亂碼
            con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db , user, pwd);

        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return con;
    }

    //查詢
    public static String QuerySQL()
    {
        String result = "";
        try
        {  //10.0.2.2 android ip
            //本機V8 ip   192.168.223.1 
            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
            String sql = "select * from 學生信息表";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            //System.out.println(rs);
            while (rs.next())
            {//學號、姓名、班級、性別、專業、學院
                String s1 = rs.getString("學號").trim();
                String s2 = rs.getString("姓名").trim();
                String s3 = rs.getString("班級").trim();
                String s4 = rs.getString("性別").trim();
                String s5 = rs.getString("專業").trim();
                String s6 = rs.getString("學院").trim();
                result += s1 + "  " + s2 + "  " + s3 + "  " + s4+ "  " + s5+" "+s6+"\n";
               // System.out.println(s1 + "  -  " + s2);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
            result += "查詢數據異常!" + e.getMessage();
        }
        return result;
    }

    //插入學生信息
    public static String insert_student(String sno,String name,String banji,String sex,String shuanye,String xueyuan)
    {//學號、姓名、班級、性別、專業、學院
        String result = "";
        try
        {  //10.0.2.2 android ip
            //本機V8 ip   192.168.223.1
            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
            String sql = "insert into  學生信息表 values ('"+sno+"','"+name+"','"+banji+"','"+sex+"','"+shuanye+"','"+xueyuan+"');";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);


            stmt.close();
            conn.close();
            result+="插入成功";
        } catch (SQLException e)
        {
            e.printStackTrace();
            result += "查詢數據異常!" + e.getMessage();
        }
        return result;
    }

    //刪除學生信息
    public static String delete_student(String sno)
    {//學號、姓名、班級、性別、專業、學院
        String result = "";
        try
        {  //10.0.2.2 android ip
            //本機V8 ip   192.168.223.1
            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
            String sql = "delete 學生信息表 where 學號 = " + sno;
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);


            stmt.close();
            conn.close();
            result+="刪除成功";
        } catch (SQLException e)
        {
            e.printStackTrace();
            result += "查詢數據異常!" + e.getMessage();
        }
        return result;
    }

    //按學號查詢
    public static String QuerySQL_sno(String sno)
    {
        String result = "";
        try
        {  //10.0.2.2 android ip
            //本機V8 ip   192.168.223.1  171.120.157.130
            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
            String sql = "select * from 學生信息表 where 學號 = '"+ sno+"';";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next())
            {//學號、姓名、班級、性別、專業、學院
                String s1 = rs.getString("學號").trim();
                String s2 = rs.getString("姓名").trim();
                String s3 = rs.getString("班級").trim();
                String s4 = rs.getString("性別").trim();
                String s5 = rs.getString("專業").trim();
                String s6 = rs.getString("學院").trim();
                result += s1 + "  " + s2 + "  " + s3 + "  " + s4+ "  " + s5+" "+s6+"\n";
                // System.out.println(s1 + "  -  " + s2);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
            result += "查詢數據異常!" + e.getMessage();
        }
        return result;
    }

    //按性別查詢
    public static String QuerySQL_sex(String sex)
    {
        String result = "";
        try
        {  //10.0.2.2 android ip
            //本機V8 ip   192.168.223.1  171.120.157.130
            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
            String sql = "select * from 學生信息表 where 性別 = '"+sex+"';";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next())
            {//學號、姓名、班級、性別、專業、學院
                String s1 = rs.getString("學號").trim();
                String s2 = rs.getString("姓名").trim();
                String s3 = rs.getString("班級").trim();
                String s4 = rs.getString("性別").trim();
                String s5 = rs.getString("專業").trim();
                String s6 = rs.getString("學院").trim();
                result += s1 + "  " + s2 + "  " + s3 + "  " + s4+ "  " + s5+" "+s6+"\n";
                // System.out.println(s1 + "  -  " + s2);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
            result += "查詢數據異常!" + e.getMessage();
        }
        return result;
    }
    //按學院查詢
    public static String QuerySQL_xueyuan(String xueyuan)
    {
        String result = "";
        try
        {  //10.0.2.2 android ip
            //本機V8 ip   192.168.223.1  171.120.157.130
            Connection conn = getSQLConnection("192.168.223.1", "admin", "123123", "物流寄存");
            String sql = "select * from 學生信息表 where 學院 = '"+ xueyuan +"';";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            //System.out.println(rs);
            while (rs.next())
            {//學號、姓名、班級、性別、專業、學院
                String s1 = rs.getString("學號").trim();
                String s2 = rs.getString("姓名").trim();
                String s3 = rs.getString("班級").trim();
                String s4 = rs.getString("性別").trim();
                String s5 = rs.getString("專業").trim();
                String s6 = rs.getString("學院").trim();
                result += s1 + "  " + s2 + "  " + s3 + "  " + s4+ "  " + s5+" "+s6+"\n";
                // System.out.println(s1 + "  -  " + s2);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e)
        {
            e.printStackTrace();
            result += "查詢數據異常!" + e.getMessage();
        }
        return result;
    }
    public static void main(String[] args)
    {
        QuerySQL();
    }

}

2.操作程序test.java

說明:隻要DBUtil.java無錯誤,並且可以利用DBUtil.java操作數據庫,則這個test.java可以直接復制

import java.util.*;
public class test {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int flag = 0;
        String sno = "", name = " ", banji = " ", sex = " ", shuanye = " ", xueyuan = " ";
        System.out.println("  學生信息管理程序  ");
        System.out.println(" 0.查看控制面板  ");
        System.out.println(" 1.查詢全體學生信息   ");
        System.out.println(" 2.插入學生信息   ");
        System.out.println(" 3.刪除學生   ");
        System.out.println(" 4.修改學生信息   ");
        System.out.println(" 5.查詢相關信息   ");
        System.out.println(" 6.退出   ");
        while(true) {
            System.out.println(" 輸入要繼續執行的操作:");
            flag = in.nextInt();
            in.nextLine();
            if(flag == 6) break;
            else {
                switch (flag) {
                    case 0:
                        System.out.println(" 0.查看控制面板  ");
                        System.out.println(" 1.查詢全體學生信息   ");
                        System.out.println(" 2.插入學生信息   ");
                        System.out.println(" 3.刪除學生   ");
                        System.out.println(" 4.修改學生信息   ");
                        System.out.println(" 5.查詢相關信息   ");
                        System.out.println(" 6.退出   ");
                        break;
                    case 1://查詢全部
                        System.out.println("查詢全體學生信息:");
                        System.out.print(DBUtil.QuerySQL());
                        break;
                    case 2://插入信息
                        System.out.println("請輸入要插入的學生的信息(以空格隔開):");
                        String str = in.nextLine();
                        String[] S = str.split(" ");
                        sno = S[0];
                        name = S[1];
                        banji = S[2];
                        sex = S[3];
                        shuanye = S[4];
                        xueyuan = S[5];
                        System.out.print(DBUtil.insert_student(sno, name, banji, sex, shuanye, xueyuan));
                        break;
                    case 3://刪除學生信息
                        System.out.println("請輸入要刪除的學生的學號:");
                        sno = in.nextLine();
                        System.out.print(DBUtil.delete_student(sno));
                        break;
                    case 4://修改學生信息
                        System.out.println("請輸入要修改的學生的學號:");
                        sno = in.nextLine();
                        DBUtil.delete_student(sno);
                        System.out.println("請輸入要修改的學生的信息以空格隔開(學號不可修改):");
                        String str2 = in.nextLine();
                        String[] S2 = str2.split(" ");
                        name = S2[0];
                        banji = S2[1];
                        sex = S2[2];
                        shuanye = S2[3];
                        xueyuan = S2[4];
                        DBUtil.insert_student(sno, name, banji, sex, shuanye, xueyuan);
                        System.out.println("修改之後的數據:");
                        break;
                    case 5://查詢相關信息
                        System.out.println(" 1.按學院查詢   ");
                        System.out.println(" 2.按學號查詢  ");
                        System.out.println(" 3.按性別查詢  ");

                        int FLG = Integer.parseInt(in.nextLine());
                        //in.nextInt();
                        switch(FLG){
                            case 1 ://按學院查詢
                                System.out.println("要查詢的學院:");
                                String temp_xueyuan = in.nextLine();
                                System.out.print(DBUtil.QuerySQL_xueyuan(temp_xueyuan));
                                break;
                            case 2 ://按學號查詢
                                System.out.println("要查詢學生的學號:");
                                String temp_sno = in.nextLine();
                                System.out.print(DBUtil.QuerySQL_sno(temp_sno));
                                break;
                            case 3://按性別查詢
                                System.out.println("要查詢的性別:");
                                String temp_sex = in.nextLine();
                                System.out.print(DBUtil.QuerySQL_sex(temp_sex));
                                break;
                        }

                        break;
                }//switch
            }//else
        }
    }

}

推薦閱讀: