程序two.java正在编译,但没有生成输出,没有异常发生。
(在cmd中执行)
// db.properties
driverclass = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@loacalhost:1521:xe
user = system
password = kapil
// ConnectionProvider.java
class ConnectionProvider
{
static Properties prop;
static
{
prop = new Properties();
String path = File.separator + "db.properties";
InputStream in = prop.getClass().getResourceAsStream(path);
try
{
prop.load(in);
}
catch(Exception e)
{
}
}
public static Connection getConnection() throws Exception
{
Class.forName(prop.getProperty("driverclass"));
Connection con = DriverManager.getConnection(
prop.getProperty("url"),
prop.getProperty("user"),
prop.getProperty("password"));
return con;
}
}
// two.java
class Two
{
public static void main(String args[])
{
try
{
Connection con = ConnectionProvider.getConnection();
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery("Select * from Emp ");
while(rset.next())
{
System.out.println(rset.getInt(1) + "\t"
+ rset.getString(2) + "\t"
+ rset.getString(3) + "\t"
+ rset.getInt(4));
}
con.close();
}
catch(Exception e){}
}
}
答案 0 :(得分:1)
首先不要通过这样做来消耗异常catch(Exception e){}
这不是一个好习惯,总是像stacktrace
一样打印catch(Exception e){ e.printStacktrace();}
现在,你的代码中的问题是url
将其更改为 -
url = jdbc:oracle:thin:@localhost:1521:xe
网址localhost
中有拼写错误。
编辑:当您通过cmd
执行课程时,我希望课程和db.properties
在同一文件夹中尝试类似这样的内容
try {
prop.load(new FileInputStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
所以完整的ConnectionProvider类看起来像这样
class ConnectionProvider
{
static Properties prop;
static
{
prop = new Properties();
try {
prop.load(new FileInputStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception
{
Class.forName(prop.getProperty("driverclass"));
Connection con = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password"));
return con;
}
}
现在,在执行课程时,不要忘记在课程路径中包含ojdbc6.jar
。您可以从here获取。
答案 1 :(得分:0)
没有发生异常,因为您正在捕获它而不是打印它。换句话说,可能会发生异常,但您不打印它。
将其添加到catch
区块e.printStackTrace();
此代码:
String path = File.separator + "db.properties";
左看似乎没有问题。
首先打印出路径以确保指向正确的文件。
您可以使用.getCanonicalPath()
获取绝对路径。
做这样的事情:
String filePath = new File("./yourfile.properties").getCanonicalPath();
FileInputStream fis = new FileInputStream(filePath);
props.load(fis);
答案 2 :(得分:0)
您可以使用BalusC的精彩dao layer tutorial。有一个属性文件加载器,可以满足您的需求。代码摘要如下。
这就是文件dao.properties的样子(更改Oracle Db的jdbc.url和jdbc.driver):
javabase.jdbc.url = jdbc:mysql://localhost:3306/javabase
javabase.jdbc.driver = com.mysql.jdbc.Driver
javabase.jdbc.username = java
javabase.jdbc.password = d$7hF_r!9Y
属性文件加载器(如上所述,您可以根据需要更改它,注意:这取决于您认为此文件在您的环境中发生变化的频率,如果它每年只更改一次,那么它确实是不值得每次从磁盘加载它,但如果它每天都在变化,那么可能值得添加一个静态方法,它重新加载属性文件并通过某些(预定的)后台作业执行它。)
package com.example.dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* This class immediately loads the DAO properties file 'dao.properties' once in memory and provides
* a constructor which takes the specific key which is to be used as property key prefix of the DAO
* properties file. There is a property getter which only returns the property prefixed with
* 'specificKey.' and provides the option to indicate whether the property is mandatory or not.
*
* @author BalusC
* @link http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
*/
public class DAOProperties {
// Constants ----------------------------------------------------------------------------------
private static final String PROPERTIES_FILE = "dao.properties";
private static final Properties PROPERTIES = new Properties();
static {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);
if (propertiesFile == null) {
throw new DAOConfigurationException(
"Properties file '" + PROPERTIES_FILE + "' is missing in classpath.");
}
try {
PROPERTIES.load(propertiesFile);
} catch (IOException e) {
throw new DAOConfigurationException(
"Cannot load properties file '" + PROPERTIES_FILE + "'.", e);
}
}
// Vars ---------------------------------------------------------------------------------------
private String specificKey;
// Constructors -------------------------------------------------------------------------------
/**
* Construct a DAOProperties instance for the given specific key which is to be used as property
* key prefix of the DAO properties file.
* @param specificKey The specific key which is to be used as property key prefix.
* @throws DAOConfigurationException During class initialization if the DAO properties file is
* missing in the classpath or cannot be loaded.
*/
public DAOProperties(String specificKey) throws DAOConfigurationException {
this.specificKey = specificKey;
}
// Actions ------------------------------------------------------------------------------------
/**
* Returns the DAOProperties instance specific property value associated with the given key with
* the option to indicate whether the property is mandatory or not.
* @param key The key to be associated with a DAOProperties instance specific value.
* @param mandatory Sets whether the returned property value should not be null nor empty.
* @return The DAOProperties instance specific property value associated with the given key.
* @throws DAOConfigurationException If the returned property value is null or empty while
* it is mandatory.
*/
public String getProperty(String key, boolean mandatory) throws DAOConfigurationException {
String fullKey = specificKey + "." + key;
String property = PROPERTIES.getProperty(fullKey);
if (property == null || property.trim().length() == 0) {
if (mandatory) {
throw new DAOConfigurationException("Required property '" + fullKey + "'"
+ " is missing in properties file '" + PROPERTIES_FILE + "'.");
} else {
// Make empty value null. Empty Strings are evil.
property = null;
}
}
return property;
}
}