文件位置: 建议src下
文件名称:扩展名为properties
文件内容:一行一组数据,格式“key=value”
key 命名自定义,如果是多单词,习惯使用点分割,例如jdbc.driver
value 值不支持中文,如果有需要使用非英文字符,将进行Unicode转化
----------Oracle
jdbc.driver_class=oracle.jdbc.driver.OracleDriver //数据库驱动
jdbc.connection.url=jdbc:oracle:thin:@localhost:1521:orcl //数据库地址
jdbc.connection.username=c##wuyong//数据库名称
jdbc.connection.password=//数据库密码
-----------mysql
jdbc.driver_class=com.mysql.jdbc.Driver
jdbc.connection.url=jdbc:mysql://127.0.0.1:3306/MySQL
jdbc.connection.username=root
jdbc.connection.password=Admin
-----------sqlserver
jdbc.user=sa
jdbc.password=1
jdbc.className=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://192.168.24.137:1433;databaseName=xxsb
加载配置文件:properties对象
开发中会使用Properties对象进行, 我们可以采用加载properties 文件获得流,然后使用Properties对象进行处理
1. 加载properties文件获取inputStream
1.1 方式1.使用类加载ClassLoader加载src的资源(固定写法) 获得ClassLoader固定写法:当前类.class.getClassLoader();
InputStream is= jdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
// InputStream is=本类名.class.getClassLoader().getSourceAsStream("properties配置文件名称");
1.2 方式2 加载当前类同包下的资源,如果需要从src开始必须填写 ‘’/‘’
InputStream is2=jdbcUtil.class.getResourceAsStream("jdbc2.properties");
加载src下的资源
InputStream is3=jdbcUtil.class.getResourceAsStream("/jdbc.properties");
加载完成后:使用Properties处理流
Properties props=new Properties();
使用load() 方法加载指定的流
Properties props=new Properties();
props.load(is);
//使用getProperty(key),获取需要的值
className=props.getProperty("jdbc.className");
url=props.getProperty("jdbc.url");
user=props.getProperty("jdbc.user");
password=props.getProperty("jdbc.password");
jdbc加载项目
package com.jdec.util_v3;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class jdbcUtil {
private static String user;
public static String password;
public static String className;
public static String url;
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
static{
//1.加载properties文件获取inputStream
/*1.1 方式1.使用类加载ClassLoader加载src的资源(固定写法)
* 获得ClassLoader固定写法:当前类.class.getClassLoader();
*/
InputStream is= jdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//加载当前类同包下的资源,如果需要从src开始必须填写/
InputStream is2=jdbcUtil.class.getResourceAsStream("jdbc2.properties");
//加载src下的资源
InputStream is3=jdbcUtil.class.getResourceAsStream("/jdbc.properties");
//使用Properties处理流
// 使用load() 方法加载指定的流
Properties props=new Properties();
try {
props.load(is);
//使用getProperty(key),获取需要的值
className=props.getProperty("jdbc.className");
url=props.getProperty("jdbc.url");
user=props.getProperty("jdbc.user");
password=props.getProperty("jdbc.password");
} catch (IOException e) {
e.printStackTrace();
}
}
public jdbcUtil() {
try{
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
try {
con=(Connection) DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
con=null;
e.printStackTrace();
}
return con;
}
public ResultSet excuteQuery(String sql, Object[] obj) {
if (sql!=null ){
con=getConnection();
if(con!=null){
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
if (obj!=null) {
for(int i=0;i<obj.length;i++){
pstm.setObject(i+1,obj[i]);
}
}
rs=pstm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return rs;
}
public int excuteUpdate(String sql, Object[] obj) {
// TODO Auto-generated method stub
int flag=-1;
if(sql!=null && obj!=null){
con=getConnection();
if (con!=null) {
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
pstm.setObject(i+1, obj[i]);
}
flag=pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return flag;
}
public ResultSet queryAll(String sql) {
con=getConnection();
if(con!=null){
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
rs=pstm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
public void closeAll() {
// TODO Auto-generated method stub
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstm!=null) {
try {
pstm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试类
package com.jdbc.util;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.jdbc.dao.JdbcUtils;
import com.jdec.util_v3.jdbcUtil;
public static void main(String[] args) throws SQLException{
jdbcUtil jd=new jdbcUtil();
String sql="select * from book";
Object[] obj=null;
ResultSet rs=jd.excuteQuery(sql, obj);
while(rs.next()){
System.out.println(rs.getObject("bookId")+" "+rs.getObject("bookName")+" "+rs.getObject("bookAuthor"));
}
}
}
jdbc.properties
jdbc.user=sa
jdbc.password=1
jdbc.className=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://192.168.24.137:1433;databaseName=xxsb
参考原文档: