Connect oracle____oracle in Eclipse

Source: Internet
Author: User
Tags stmt

Create a new Java Project connectoracle, under the project to build a package, named Oracletest, under the package to build a class, named Oracletest, the following code into the class file.

Package oracletest;

Import java.sql.*;

public class Oracletest {public
	static void Main (string[] args) {
		//String serverName = ' localhost ';
		try {
			class.forname ("Oracle.jdbc.driver.OracleDriver");
			System.out.println ("1");
			String url = "Jdbc:oracle:thin: @localhost: 1521:icss";
			Connection conn = DriverManager
					. getconnection (URL, "system", "Manager1");
			System.out.println ("2");
			Statement stmt = Conn.createstatement ();
			ResultSet rs = stmt.executequery ("SELECT * from table1");
			while (Rs.next ()) {
				System.out.println (rs.getstring ("id"));
				System.out.println (rs.getstring ("name"));
			}
		catch (SQLException e) {
			//TODO automatically generates catch blocks
			e.printstacktrace ();
		} catch (ClassNotFoundException e) {
  //TODO automatically generates catch blocks
			e.printstacktrace ();}}

If the operation succeeds, the connection is successful.

Errors may be encountered at run time

Java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver at
	Java.net.urlclassloader$1.run (Unknown Source) at
	java.security.AccessController.doPrivileged (Native method)
	at Java.net.URLClassLoader.findClass (Unknown source) at
	Java.lang.ClassLoader.loadClass (Unknown source)
	at Sun.misc.launcher$appclassloader.loadclass (Unknown source) at
	Java.lang.ClassLoader.loadClass (Unknown source) At
	java.lang.ClassLoader.loadClassInternal (Unknown Source) in
	JAVA.LANG.CLASS.FORNAME0 (Native method) At
	java.lang.Class.forName (Unknown Source) at
	Oracletest. Oracletest.main (Oracletest.java:9)

This is because we did not introduce Oracle's JDBC Connection classes.

Right-click on the project name to select Properties

In the pop-up window, select Java build Path-> libraries-> Add External JARS ...

Then select the Ojdbc14.jar file under the D:\oracle\ora92\jdbc\lib directory (where D:\oracle is my installation directory) and select Open

We see that we have added success

Select OK. Running the program again is OK.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.