Java connects Oracle with JDBC

Source: Internet
Author: User

1  PackageOracle;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.Statement;7 8 //Connect to Oracle using JDBC9 Ten  Public classtestoracle { One  A      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub -  the         Try { -             //1. Load Driver -Class.forName ("Oracle.jdbc.driver.OracleDriver"); -             //2. Get Connected +Connection conn= drivermanager.getconnection ("jdbc:oracle:thin:@127.0.0.1:1521:oracle12c", "Sys as SYSDBA", " fairy6280 ");  -              +Statement SM =(Statement) conn.createstatement (); AString sql = "SELECT * FROM Student"; atString SQL1 = "SELECT * from" (Select A1.*,rownum rowIndex from (SELECT * from Student Order by salary) A1 where RowNum &lt ; 5) where RowIndex > 1 "; -ResultSet RSet =sm.executequery (SQL1); -              while(Rset.next ()) -             { -System.out.println ("Name:" + rset.getstring (2)); -             } in              -             //Join transaction Processing toConn.setautocommit (false); +String sql3= "Update student set salary=salary-100 where sname= ' Smith '"; -String SQL4 = "Update student Set salary = salary +100 where sname= ' Bill '"; the sm.executeupdate (SQL3); * sm.executeupdate (SQL4); $ conn.commit ();Panax Notoginseng              -             //Close Resource the rset.close (); + sm.close (); A conn.close (); the}Catch(Exception e) { +             //Todo:handle Exception - e.printstacktrace (); $         } $System.out.println ("----------"); -     } -  the}

Note: To add JDBC Driver-Ojdbc7.jar to your project

Java connects Oracle with JDBC

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.