Class newInstance Method

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Class newInstance Method

This Java example program introduces Class newInstance method.

[edit] Java Source Code

  • Package: example.sql.connection
  • File: TestClassForNameNewInstanceApp.java
package example.sql.connection;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class TestClassForNameNewInstanceApp {
 
    public static void main(String args[]) {
 
	try {
	    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
	} catch (ClassNotFoundException e) {
	    System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver");
	    System.exit(1);
	} catch (IllegalAccessException e) {
	    System.out.println("Uh Oh! You can't load oracle.jdbc.driver.OracleDriver");
	    System.exit(2);
	} catch (InstantiationException e) {
	    System.out.println("Geez! Can't instantiate oracle.jdbc.driver.OracleDriver");
	    System.exit(3);
	}
 
	Connection conn = null;
	Statement stmt = null;
	ResultSet rset = null;
	try {
	    conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
 
	    stmt = conn.createStatement();
	    rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
	    while (rset.next())
		System.out.println(rset.getString(1));
	    rset.close();
	    rset = null;
	    stmt.close();
	    stmt = null;
	    conn.close();
	    conn = null;
	} catch (SQLException e) {
	    System.out.println("Darn! A SQL error: " + e.getMessage());
	} finally {
	    if (rset != null)
		try {
		    rset.close();
		} catch (SQLException ignore) {
		}
	    if (stmt != null)
		try {
		    stmt.close();
		} catch (SQLException ignore) {
		}
	    if (conn != null)
		try {
		    conn.close();
		} catch (SQLException ignore) {
		}
	}
    }
}

[edit] What Result You Can Get

Coming soon...

[edit] Required External Libraries and/or Files for this Java Example

In order to run this Java example, one of the following libraries may be required: In order to run this example program, one of the following libraries may be required:


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools