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