package com.emr.util;


import com.emr.entity.Zd_Assort;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class JdbcUtil {
    private static String className;
    private static String url;
    private static String user;
    private static String password;

    static
    {
        try {
            InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("config/config.properties");
            Properties props = new Properties();
            props.load(in);
            className = props.getProperty("sqlServerClassName");
            url = props.getProperty("sqlServerUrl");
            user = props.getProperty("sqlServerUserName");
            password = props.getProperty("sqlServerPassword");

            //System.out.println(className);
            //System.out.println(url);

            //注册驱动
            Class.forName(className);

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws Exception
    {
        return DriverManager.getConnection(url, user, password);

    }

    public static void close(ResultSet rs, Statement stmt,Connection con)
    {

        if(rs!=null)
        {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            rs= null;
        }

        if(stmt!=null)
        {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            stmt= null;
        }

        if(con!=null)
        {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            con= null;
        }
    }

    //查询
    public List<Zd_Assort> select(Statement stmt) throws SQLException {
        List<Zd_Assort> list=new ArrayList<>();
        ResultSet rs = stmt.executeQuery("select * from [zd_assort] ");
        Zd_Assort zd_assort=null;
        while(rs.next()){
            zd_assort=new Zd_Assort();
            //如果对象中有数据，就会循环打印出来
            //System.out.println("类型ID="+rs.getString("assort_id"));
            zd_assort.setAssortId(rs.getString("assort_id"));
            zd_assort.setAssortName(rs.getString("assort_name"));
            list.add(zd_assort);
            //System.out.println(rs.getInt("教师编号")+","+rs.getString("姓名")+","+rs.getInt("专业"));
        }
        return list;
    }

    //测试用例
    public static void main(String[] args) throws Exception{
        JdbcUtil d = new JdbcUtil();
        //3.通过数据库的连接操作数据库，实现增删改查
        Connection conn=d.getConnection();
        Statement stmt = conn.createStatement();
        //ResultSet executeQuery(String sqlString)：执行查询数据库的SQL语句 ，返回一个结果集（ResultSet）对象。
        //d.insert(stmt);
        //d.update(stmt);
        List<Zd_Assort> list=d.select(stmt);
        System.out.println(list.get(0).getAssortName()+"==================");

    }
}
