JDBC

public static Connection getConnection() throws Exception {
        // 1. ׼��������ݿ�� 4 ���ַ�.
        // 1). ���� Properties ����
        Properties properties = new Properties();

        // 2). ��ȡ jdbc.properties ��Ӧ��������
        InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
                "jdbc.properties");

        // 3). ���� 2�� ��Ӧ��������
        properties.load(in);

        // 4). ������� user, password ��4 ���ַ�.
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driver = properties.getProperty("driver");

        // 2. ������ݿ������(��Ӧ�� Driver ʵ��������ע����ľ�̬�����.)
        Class.forName(driver);

        // 3. ͨ�� DriverManager �� getConnection() ������ȡ��ݿ�����.
        return DriverManager.getConnection(jdbcUrl, user, password);
    }

转载于:https://www.cnblogs.com/taz372436/p/5593213.html

你可能感兴趣的:(JDBC)