Dynamic Classloading Example in Java   #


package com.livrona.snippets.dao; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * This snippet shows how to connect to an Oracle database using JDBC. Open the connection, execute the query and close * the connection. */ public class OracleDao { private static String driverClass = "oracle.jdbc.driver.OracleDriver"; private Connection con; private String url; private String userName; private String password; /** * Initialize the Dao * * @param propertiesFile * @throws IOException * @throws ClassNotFoundException */ public void init(String propertiesFile) throws IOException, ClassNotFoundException { // load the properties Properties props = new Properties(); props.load(new FileInputStream(propertiesFile)); // get the properties url = props.getProperty("db.url"); userName = props.getProperty("db.user"); password = props.getProperty("db.password"); // load the class Class.forName(driverClass); } /** * Open the Dao Connection * * @param fs * @throws SQLException * @throws IOException */ public void open() throws SQLException, IOException { // get the connection to the database con = DriverManager.getConnection(url, userName, password); } /** * Close the connection * * @throws SQLException */ public void close() throws SQLException { if (con != null) { // close the connection con.close(); } } public static void main(String[] args) throws Exception { // store args String propertiesFile = args[0]; // execute the test functions OracleDao dao = new OracleDao(); dao.init(propertiesFile); dao.open(); dao.close(); } }

Loading Please Wait...