JDBC (connection to Oracle) ____oracle

Source: Internet
Author: User
Tags oracle database installation stmt

JDBC Programming Steps:
----Java-Oracle Data connection (under Lomboz_eclipse environment)
1. Locate the JDBC Folder →lib folder in the Oracle Database installation folder →classesl2.jar
Import this jar package in 2.lomboz_eclipse
Import Method:
Create a project, right-click on the project name to select Build Path→add External archives→
Select Classesl2.jar for Import

3. Create a new program to write code to connect with Oracle
The steps are as follows:
1. Instance Driver class
Class.forName ("Oracle.jdbc.driver.OracleDriver");
2. Establish a connection to the database
Connection conn = Drivermanager.getconnection ("Jdbc:oracle:thin:
@192.168.8.1:1521:yuewei "," Scott "," Tiger ");
3. Send data to the database
Statement STM = conn. Creatstatement ();
4. Execute statement (SELECT statement)
ResultSet rs = stm.executequery (SELECT * from dept);
5. Display statements
Rs.getstring ("Deptno");

The

Complete code is as follows:
Import java.sql.*; public class Testjdbc {public static void main (string[] args) {ResultSet rs = null; Statement stmt = null; Connection conn = null; try {class.forname ("Oracle.jdbc.driver.OracleDriver");//new Oracle.jdbc.driver.OracleDriver (); conn = Drivermanager.getconnection ("Jdbc:oracle:thin:@192.168.0.1:1521:yuewei", "Scott", "Tiger"); stmt = Conn.createstatement (); rs = Stmt.executequery ("SELECT * from dept"); while (Rs.next ()) {System.out.println (rs.getstring ("Deptno"));//system.out.println (Rs.getint ("Deptno"));} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} finally {try { if (Rs!= null) {Rs.close (); rs = null;} if (stmt!= null) {stmt.close (); stmt = null;} if (conn!= null) {conn.close (); conn = null; The catch (SQLException e) {e.printstacktrace ();}} } }

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.