不使用框架与MySQL连接以及加载配置文件的几种方式

不使用框架与MySQL连接以及加载配置文件的几种方式

package com.service;

import com.pojo.Book;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.*;
import java.util.*;


public class BookService {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    public BookService() {
        try {
            Properties prop = new Properties();
            prop.load(this.getClass().getResourceAsStream("db.properties"));
        //  new PropertyResourceBundle(new FileInputStream(new File("hahaa")));
            driver = prop.getProperty("driver");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
        } catch (Exception e) {
            System.err.println("Can't find db.properties");
            e.printStackTrace();
        }
    }

    public List getBookList() {
        Connection conn = null;
        List listOfBooks = new ArrayList();
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.err.println("Can't find the driver");
            e.printStackTrace();
        }
        try {
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.err.println("Can't not connect to the database");
            e.printStackTrace();
        }
        try {
            Statement prest = (Statement) conn.createStatement();
            ResultSet resultSet = prest.executeQuery("select * from tbl_books");
            while (resultSet.next()) {
                String isbn = resultSet.getString("isbn");
                String title = resultSet.getString("title");
                String copyright = resultSet.getString("copyright");
                int editionNumber = resultSet.getInt("editionNumber");
                double price = resultSet.getDouble("price");
                Book book = new Book(isbn, title, copyright, editionNumber, price);
                listOfBooks.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return listOfBooks;
    }

    /**
     * 加载配置文件的几种方式
     */
    public void getproperties(String[] args) throws IOException {
        //类加载器进行加载相关文件
        ClassLoader loader = this.getClass().getClassLoader();
        InputStream inputStream = loader.getResourceAsStream("hahah.properties");
        byte[] bytes = new byte[inputStream.available()];
        int read = inputStream.read(bytes);
        System.out.println(new String(bytes, 0, read));
        //resourcebound进行加载相关配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("hahhaha.properties");
        String bundleString = bundle.getString("esese");
        System.out.println(bundleString);
        //Properties的方式进行加载相关文件
        Properties properties = new Properties();
        properties.load(this.getClass().getResourceAsStream("hahahhha"));
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        System.out.println(username);
        System.out.println(password);
        //File流的方式加载文件
        File file = new File("ahahhah");
        if (file.exists()) {
            PropertyResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream(file));
            if (resourceBundle.containsKey("key")) {
                resourceBundle.getString("key");
                resourceBundle.getKeys();
                resourceBundle.getBaseBundleName();
            }
        }
        //jdk中比较重要的包:Java.util   Java.lang   Java.net  Java.util.concurrent ,没事可以多看看这几个包,有助于理解JDK 以及涉及到JVM的相关问题解答等等。

        //设计模式对于Java开发者确实需要好好研究,假如不用框架(没了封装)这时候设计模式的优美则体现出来了
        /**比如:单例模式 工厂模式  抽象工厂模式  适配器模式  策略模式  装饰者模式  代理模式 等等 */




    }
}

你可能感兴趣的:(不使用框架与MySQL连接以及加载配置文件的几种方式)