Code for Java Eclipse to access the Oracle database

Source: Internet
Author: User

Package Com.hanqi.test;import java.sql.connection;import java.sql.drivermanager;import java.sql.ResultSet; Import Java.sql.sqlexception;import java.sql.Statement; Public classTest { Public Static voidMain (string[] args) {//Accessing the database//1. Load driver: Load the database corresponding package name Oracle.jdbc.driver//1. (Load the database corresponding to the driver Class) Oracledriver.class        Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); //This is a fixed notation.//2. Get the database connection: Drive Manager via Java//url-database address, different database notation different 127.0.0.1 and localhost represent native//url-Database Address: User-username: password-Password connection for connection DriverManager drive managerConnection conn=Drivermanager.getconnection ("JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL",                "test0816","123456"); System. out. println ("Connection Successful"); //operation Database-Adding and deleting changes//3. Get Operational Database declarationStatement st=conn.createstatement ();//Statement declaration createstatement Creation Statement//4.DML Add Data//Perform an update operation//The return value represents the number of data record bars affected by the operation//int i=st.executeupdate ("INSERT into student (sno,sname,ssex)")//+ "VALUES (' 120 ', ' Harry ', ' Men ')");                intI=st.executeupdate ("Update student set ssex= ' man ' where ssex= ' 1 '"); System. out. println ("Add Data success return value ="+i);//return value//5. Querying Data//resultset data Result setResultSet Rs=st.executequery ("SELECT * FROM Student"); //traversing the result set traversal must be a loop//Next () determines whether the next record exists, moves the pointer to the next record if it exists         while(Rs.next ()) {//reading DataString sno=rs.getstring ("Sno"); String sname=rs.getstring ("sname"); String Ssex=rs.getstring ("Ssex"); System. out. println ("sno="+sno+"sname="+sname+"ssex="+ssex); }        //Freeing Resources//Release Resources Purpose: system resources are reduced for each program running in Windows. //Some programs will consume a lot of system resources, even if the program is closed, in memory or some useless DLL files are running, which makes the system running speed down. Rs.close ();                St.close (); //Close the databaseConn.close (); } Catch(ClassNotFoundException |SQLException e) {            //TODO Auto-generated catch blockE.printstacktrace (); }    }}

Code for Java Eclipse to access the Oracle database

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.