JDBC通过配置文件(properites)读取数据库配置信息
扫盲:
Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中。与普通程序不同的是。Java程序(class文件)并不是本地的可执行程序。当运行Java程序时,首先运行JVM(Java虚拟机),然后再把Java class加载到JVM里头运行,负责加载Java class的这部分就叫做Class Loader。
JVM本身包含了一个ClassLoader称为Bootstrap ClassLoader,和JVM一样,BootstrapClassLoader是用本地代码实现的,它负责加载核心JavaClass(即所有java.*开头的类)。另外JVM还会提供两个ClassLoader,它们都是用Java语言编写的,由BootstrapClassLoader加载;其中Extension ClassLoader负责加载扩展的Javaclass(例如所有javax.*开头的类和存放在JRE的ext目录下的类),ApplicationClassLoader负责加载应用程序自身的类。
当运行一个程序的时候,JVM启动,运行bootstrapclassloader,该ClassLoader加载java核心API(ExtClassLoader和AppClassLoader也在此时被加载),然后调用ExtClassLoader加载扩展API,最后AppClassLoader加载CLASSPATH目录下定义的Class,这就是一个程序最基本的加载流程。
一:工具类
package Jdbc_Test; import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties; public class jdbcutils {
private jdbcutils(){};
private static Connection con;
public static void main(String[] args)throws IOException{ }
public static Connection getCon(){
/*
通过读取properites配置文件来获取数据库连接信息。
通过类加载器,读取当前父目录的配置文件。通过class.getClassLoader方法getResourceAstream获取配置文件的
内容,然后通过IO流处理properites文件获取对应的key值。
配置文件便于灵活配置数据库信息。
*/
InputStream inp=jdbcutils.class.getClassLoader().getResourceAsStream("database.properties");
Properties pro=new Properties();
try {
pro.load(inp);
}catch (Exception ex){
throw new RuntimeException(ex+"读取配置文件失败!");
}
try {
String Drverclass = pro.getProperty("Driver.class");
String url = pro.getProperty("Url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
Class.forName(Drverclass);
con = DriverManager.getConnection(url, username, password);
}catch (Exception ex){
throw new RuntimeException(ex+"数据库连接失败!");
}
return con;
}
public static void cls_re(Connection con, Statement sta, ResultSet re){
if(con!=null){
try{
con.close();
}catch (Exception ex){}
}
if(sta!=null){
try{
sta.close();
}catch (Exception ex){}
}
if(re!=null){
try{
re.close();
}catch (Exception ex){}
} }
public static void cls_re(Connection con, Statement sta){
if(con!=null){
try{
con.close();
}catch (Exception ex){}
}
if(sta!=null){
try{
sta.close();
}catch (Exception ex){}
} }
}
二:测试代码:
package Jdbc_Test; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; public class jdbc_test {
public static void main(String... args)throws Exception{
Connection con =jdbcutils.getCon();
PreparedStatement pre=con.prepareStatement("select * from system_user");
ResultSet re=pre.executeQuery();
List <Sys_User> lis =new ArrayList<Sys_User>();
while (re.next()){
Sys_User sys=new Sys_User(re.getString("username"),re.getString("password"));
lis.add(sys);
}
System.out.print(lis);
}
}
class Sys_User{
private String username;
private String password;
public Sys_User(String username ,String password){
this.username=username;
this.password=password;
} @Override
public String toString() {
String info=this.username+" "+this.password;
return info;
}
}
properites配置文件:
Driver.class=com.mysql.jdbc.Driver
Url=jdbc:mysql://192.168.147.146:3306/homework_day13
username=test
password=
需要注意配置文件中不能有双引号 !
JDBC通过配置文件(properites)读取数据库配置信息的更多相关文章
- 读取数据库配置信息的两种方式(以后开发项目用java链接数据库)-------java基础知识
第一步:先建立jdbc.properties user=root password url/yanlong driver=com.mysql.jdbc.Driver 第一种方式:直接文件读取 pack ...
- PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)
http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)
- golang 读取 ini配置信息
package main //BY: 29295842@qq.com//这个有一定问题 如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost ...
- 泛微ecology OA系统某接口存在数据库配置信息泄露漏洞
2漏洞详情 攻击者可通过该漏洞页面直接获取到数据库配置信息,攻击者可通过访问存在漏洞的页面并解密从而获取数据库配置信息,如攻击者可直接访问数据库,则可直接获取用户数据,由于泛微e-cology默认数据 ...
- 泛微e-cology OA系统某接口存在数据库配置信息泄露漏洞复现
1.简介(开场废话) 攻击者可通过存在漏洞的页面直接获取到数据库配置信息.如果攻击者可直接访问数据库,则可直接获取用户数据,甚至可以直接控制数据库服务器. 2.影响范围 漏洞涉及范围包括不限于8.0. ...
- 泛微ecology OA系统在数据库配置信息泄露
漏洞描述 攻击者可通过该漏洞页面直接获取到数据库配置信息,攻击者可通过访问存在漏洞的页面并解密从而获取数据库配置信息,如攻击者可直接访问数据库,则可直接获取用户数据,由于泛微e-cology默认数据库 ...
- spring读取加密配置信息
描述&背景Spring框架配置数据库等连接等属性时,都是交由 PopertyPlaceholderConfigurer进行读取.properties文件的,但如果项目不允许在配置文件中明文保存 ...
- spring boot mybatis XML文件读取properties配置信息
配置文件application.properties中相关配置信息可以在部署以后修改,引用配置信息可以在代码和mybatis的映射文件中 1.JAVA代码 可以通过变量去读取 application. ...
- java 读取数据库中文信息,为何在jsp页面中出现乱码
有如下几种解决办法:1.保证项目的字符编码和每一个jsp页面的字符编码一致,如果不一致可能导致中文乱码问题<%@ page language="java" contentTy ...
随机推荐
- 关于vue跨域名对接微信授权认证和APP授权认证
这种情况一般也只会出现在前后端分离,跨域名授权的时候吧.耗费了一个前端+一个后台+一个网关,熬夜通宵了两天才整出来一套方法(你们见过凌晨6点的杭州吗,对,我下班的时候天黑了,到家天亮了....),和开 ...
- js-权威指南学习笔记15
第十五章 脚本化文档 1.文档对象模型DOM是表示和操作HTML和XML文档内容的基础API. 2.Document.Element.Text是Node的子类. 3.查询文档的一个或多个元素有如下方法 ...
- 读《锋利的jQuery》中first-child时的一个细节
今天在看<锋利的jQuery>这书时,看到过滤选择器那一节.有个知识点引起了我的注意. (我不用书里一模一样的代码做例子)举个简单的例子-代码: <ul> <li> ...
- pom.xml配置文件内容记录
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- LeetCode 544----Output Contest Matches
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...
- 通过代码动态创建IIS站点
对WebApi进行单元测试时,一般需要一个IIS站点,一般的做法,是通过写一个批处理的bat脚本来实现,其实通过编码,也能实现该功能. 主要有关注三点:应用程序池.Web站点.绑定(协议类型:http ...
- idea appliction context not configured for this file
File --> Project Structure
- Redis redis-trib集群配置
redis文档:http://doc.redisfans.com/ 参考:https://www.cnblogs.com/wuxl360/p/5920330.html http://www.cnblo ...
- Week8——hashcode()和equals()方法
equals()方法 Object类中的equals方法和“==”是一样的,没有区别,即俩个对象的比较是比较他们的栈内存中存储的内存地址.而String类,Integer类等等一些类,是重写了equ ...
- 【转】grep -v grep
1.grep 是查找含有指定文本行的意思,比如grep test 就是查找含有test的文本的行 2.grep -v 是反向查找的意思,比如 grep -v grep 就是查找不含有 grep 字段的 ...