不使用框架与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<Book> getBookList() {
        Connection conn = null;
        List<Book> listOfBooks = new ArrayList<Book>();
        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开发者确实需要好好研究,假如不用框架(没了封装)这时候设计模式的优美则体现出来了
        /**比如:单例模式 工厂模式  抽象工厂模式  适配器模式  策略模式  装饰者模式  代理模式 等等 */




    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值