编写数据库连接配置文件
开发环境:eclipse+tomcat
以往我连接数据库的参数都写在tomcat的conf/context.xml中。
我现在想把数据库的连接参数写在项目自己的配置文件中,我应该如何写?代码中应该如何调用?
[解决办法]
建个properties文件,在里面配置如“default.connection.username=root”,在类似jdbc中调用
[解决办法]
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.connection.url"> jdbc:mysql://localhost/hibernate_fi </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">long</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.show_sql">true</property> <mapping resource="com/hibernate/User.hbm.xml" /> </session-factory></hibernate-configuration>
[解决办法]
getProperties()之类的东西是可以的
[解决办法]
jdbc.properties文件:jdbc.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcljdbc.username=oraclejdbc.password=oraclejdbc.driverClassName=oracle.jdbc.driver.OracleDriver读取: /*************************************************************************** * Loads the given properties file from disk. The file is assumed to reside * in the path pointed to by the system property <CODE>idtrust.home</CODE>. * * @param propertiesFile * the name of the properties file * * @return The loaded <CODE>Properties</CODE> structure */ public static Properties loadProperties(String propertiesFile) throws Exception { Properties props = null; // propertiesFile = getPropertiesFile(propertiesFile); // ---------------------------- // ...and if so, then load the values from that external file InputStream in = null; try { in = new FileInputStream(propertiesFile); props = new Properties(); props.load(in); } catch (Exception e) { props = null; } finally { if (in != null) in.close(); } return props; }