package com.homewoek3_4.dao;

 import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties; /**
* 数据库连接通用类
* @author Administrator
*/
public abstract class BaseDao {
private static final String path = "database.properties";
private static String DB_DRIVER;
private static String DB_URL;
private static String DB_USER;
private static String DB_PWD; protected Connection conn = null;
protected PreparedStatement ps = null;
protected ResultSet rs = null; static {
Properties pro = new Properties();
InputStream io = BaseDao.class.getClassLoader().getResourceAsStream(path);
try {
//读取配置文件
pro.load(io);
} catch (IOException e) {
e.printStackTrace();
}
DB_DRIVER = pro.getProperty("DB_DRIVER");
DB_URL = pro.getProperty("DB_URL");
DB_USER = pro.getProperty("DB_USER");
DB_PWD = pro.getProperty("DB_PWD");
try {
//加载驱动类
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 打开数据库连接
*/
protected void openConn() {
try {
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭数据库连接
*/
protected void closeConn() {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 增删改
* @param sql
* @param obj
* @return
*/
protected int executeUpdate(String sql, Object...obj) {
int result = -1;
this.openConn();
try {
ps = conn.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
ps.setObject(i+1, obj[i]);
}
}
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 查询
* @param sql
* @param obj
* @return
*/
protected void executeQuery(String sql, Object...obj) {
this.openConn();
try {
ps = conn.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
ps.setObject(i+1, obj[i]);
}
}
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

创建database.properties文件,注意:后缀名一定是properties。此格式的文件在MyEclipse中图标会变,这点很容易写错,要注意。

 DB_DRIVER=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/Pet?useUnicode=true&characterEncoding=utf8&useSSL=true
DB_USER=root
DB_PWD=111111

Java_JDBC连接数据库_使用读取配置文件的方式的更多相关文章

  1. C#连接数据库_使用读取配置文件的方式

    using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlCli ...

  2. python读取配置文件的方式

    python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...

  3. Java读取配置文件的方式

    Java读取配置文件的方式-笔记 1       取当前启动文件夹下的配置文件   一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...

  4. python中读取配置文件的方式

    方式1:argparse argparse,是Python标准库中推荐使用的编写命令行程序的工具.也可以用于读取配置文件. 字典样式的配置文件*.conf 配置文件test1.conf { " ...

  5. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  6. Spring中@Value注解使用——一种新的直接读取配置文件的方式

    1.@Value注解作用 该注解的作用是将我们配置文件的属性读出来,有@Value(“${}”)和@Value(“#{}”)两种方式. 2.@Value注解作用的两种方式 场景 假如有以下属性文件de ...

  7. SpringBoot两种读取配置文件的方式

    方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...

  8. Spring读取配置文件的方式总结

    一.基于XML配置的方式 1.使用 PropertyPlaceholderConfigurer - 在 applicationContext.xml 中配置: <context:property ...

  9. scala读取jar包外配置文件的方式

    在scala的开发过程中,经常会修改程序的参数,将这些参数放到配置文件中避免了重复编译,打包的过程 这里给出读取配置文件的三种方式 方式一: 这是最常见的读取配置文件方式 val postgprop ...

随机推荐

  1. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  2. linux下weblogic11g成功安装后,启动报错Getting boot identity from user

    <2015-7-1 下午05时46分33秒 CST> <Info> <Management> <BEA-141107> <Version: Web ...

  3. java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

    java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException 遇到这样的问题 ...

  4. Centos 6.4 实际工作环境搭建(LNMP)

    基本配置 服务器IP设置.编辑网卡配置文件,命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 注:ifcfg-eth0参数  TYPE=Ethernet ...

  5. ASO--简单了解

    ASO是“应用商店优化”的简称.ASO(App Search Optimization)就是提升你APP在各类APP应用商店/市场排行榜和搜索结果排名的过程. 类似普通网站针对搜索引擎的优化,即SEO ...

  6. 02-Swift学习笔记-元组类型

    02-Swift学习笔记-元组类型 元组类型由N个任意类型的数据组成(N>=0) 元组类型的数据称为"元素" eg var size = (x:100 , y:100) si ...

  7. PyTorch 60 分钟入门教程

    PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程 http://pytorchchina.com/2018/06/25/what-is-pytorch/ PyTorch 6 ...

  8. 51NOD 1821 最优集合 栈

    1821 最优集合   一个集合S的优美值定义为:最大的x,满足对于任意i∈[1,x],都存在一个S的子集S',使得S'中元素之和为i. 给定n个集合,对于每一次询问,指定一个集合S1和一个集合S2, ...

  9. HBase在大搜车金融业务中的应用实践

    摘要: 2017云栖大会HBase专场,大搜车高级数据架构师申玉宝带来HBase在大搜车金融业务中的应用实践.本文主要从数据大屏开始谈起,进而分享了GPS风控实践,包括架构.聚集分析等,最后还分享了流 ...

  10. python itertools

    1 product 1.1 一个generator函数 因此它的返回值是一个iterator,可以用for遍历. 1.2 计算product的参数分类 1.2.1 dict和list 只用了dict的 ...