不使用框架与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开发者确实需要好好研究,假如不用框架(没了封装)这时候设计模式的优美则体现出来了
/**比如:单例模式 工厂模式 抽象工厂模式 适配器模式 策略模式 装饰者模式 代理模式 等等 */
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/80409.html