/*
 * @Author: Kane
 * @Date: 2023-02-04 11:38:32
 * @LastEditors: Kane
 * @LastEditTime: 2023-02-17 10:52:40
 * @FilePath: /后端-需求/src/main/java/com/cpic/xim/utils/db/RequirementDbOperation.java
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.utils.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import com.cpic.xim.data.RequirementStatus;

public final class RequirementDbOperation
{
    private static final String MYSQL_JDBC_CONNECT = "jdbc:mysql://10.39.0.85:3306";
    private static final String MYSQL_CLASS_DRIVER = "com.mysql.cj.jdbc.Driver";

    /*****************************************************
     * 查询需求状态。
     * @return Vector<RequirementStatus> 需求状态的集合
     *****************************************************/
    public static Vector<RequirementStatus> queryRequirementStatus()
            throws ClassNotFoundException, SQLException
    {
        Vector<RequirementStatus> vStatus = new Vector<RequirementStatus>();

        Class.forName( MYSQL_CLASS_DRIVER );

        Connection conn = null;
        Statement statement = null;
        ResultSet results = null;
        String querSQL = "select * from requirement.requirement_status";

        try
        {
            conn = DriverManager.getConnection( MYSQL_JDBC_CONNECT, "cpicxim", "Cpic#1234" );
            statement = conn.createStatement();
            results = statement.executeQuery( querSQL );

            while ( results.next())
            {
                RequirementStatus status = new RequirementStatus();

                status.setStatus_code( results.getInt( "status_code" ) );
                status.setStatus_name( results.getString( "status_name" ) );

                vStatus.add( status );
            }
        }
        finally
        {
            if ( results != null )
            {
                try
                {
                    results.close();
                }
                catch ( SQLException except )
                {
                    except.printStackTrace();
                }
            }

            if ( statement != null )
            {
                try
                {
                    statement.close();
                }
                catch ( SQLException except )
                {
                    except.printStackTrace();
                }
            }

            if ( conn != null )
            {
                try
                {
                    conn.close();
                }
                catch ( SQLException except )
                {
                    except.printStackTrace();
                }
            }
        }

        return vStatus;
    }
}
