代码:
Connection conn = null;
Properties connInfo = new Properties();
// System.out.println(System.getProperty( "user.dir ")+ "\\ConnInfo.Properties ");
try {
connInfo.load(new FileInputStream(System.getProperty( "user.dir ")+ "\\ConnInfo.Properties "));
Class.forName( "oracle.jdbc.driver.OracleDriver ").newInstance();
String url = connInfo.getProperty( "url ");
String user = connInfo.getProperty( "user ");
String pass = connInfo.getProperty( "pass ");
conn = DriverManager.getConnection(url, user, pass);
} catch (InstantiationException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackT …
百思不得其解:按说打包后Jar和ConnInfo.Properties在同一目录下应该可以读到吧,总是 "No Driver "…,
------解决方案--------------------
不一定
要看具体配置
你尝试着更换一下文件名前面的相对目录
一个个文件夹加上去
文件放在与.class相同的目录下
------解决方案--------------------
/**
* @(#)JDBCTool.java 1.0 2007-07-30
*
*
* */
package com.sqxy.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.net.URLDecoder;
import java.util.Properties;
public class JDBCTool{
private static Connection connection;
public static Connection getConnection(){
Properties properties = new Properties();
try {
String path = JDBCTool.class
.getProtectionDomain().getCodeSource()
.getLocation().getPath();
path = URLDecoder.decode(path, "utf-8 ");
properties.load
(new FileReader(path.substring(0,path.lastIndexOf( '/ ')+1) +
"jdbc-config.properties "));
String driver = properties.getProperty( "jdbc.driver ");
String url = properties.getProperty( "jdbc.url ") ;
String user = properties.getProperty( "jdbc.user ");
String password = properties.getProperty( "jdbc.password ");
Class.forName(driver);
connection = DriverManager.getConnection(url,user,password);
}
catch (FileNotFoundException ex){
System.out.println( "------[警告:找不到配置文件jdbc-config.properties]------\n " +
"请将jdbc-config.properties与本工具包放在同一目录下\n " +
"若配置文件已丢失,您可以重新编写。配置文件格式为:\n " +
"jdbc.driver=[数据库驱动类名]\n " +
"jdbc.url=[数据库URL]\n " +
"jdbc.user=[数据库登录用户名]\n " +
"jdbc.password=[数据库登录密码] ");
}
catch (IOException ex){
ex.printStackTrace();
}
catch (ClassNotFoundException ex){
System.out.println( "------[找不到JDBC驱动类]------ ");