JAVA-JDBC Connecting the Oracle Database

Source: Internet
Author: User

 Packagecom.zse.oracle;ImportOracle.jdbc.*;ImportJava.sql.*;Importjavax.swing.text.html.HTMLDocument.HTMLReader.PreAction; Public classTest { Public Static voidMain (string[] args) {//Load Driver        Try{class.forname ("Oracle.jdbc.OracleDriver"); String URL= "Jdbc:oracle:thin:@127.0.0.1:1521:xe"; String User= "Root"; String Pass= "111"; //Obtaining an Oracle database connection through the drive managerConnection conn=drivermanager.getconnection (URL, user, pass); //Add a recordStatement st=conn.createstatement (); //Add Statement//String sql= "insert into student values (3, ' CCC ', ' 444 ')";//Design Execution Statements//Modifying statements//String sql= "update student set pass= ' 999 ' where sid=1"; //DELETE statement//String sql= "Delete from student where sid=1"; //st.execute (SQL);//EXECUTE Statement//Read all records//String sql= "select * from student where name= ' aaa ' and pass= ' 111 ' ORDER by Sid"; //returns a result from a query//Fuzzy Query//String sql= "SELECT COUNT (*) as num from student where name is like '%a% '"; //ResultSet rs = st.executequery (SQL); //Traverse result output record//While (Rs.next ())//determine if the next record exists, and if so, the cursor moves backward one cell//            {//                //System.out.println (rs.getstring ("Sid") + ";" +rs.getstring ("name") + ";" +rs.getstring ("pass"));//System.out.println (rs.getstring ("num"));//            }                        //precompilation: Record insertion//String sql= "insert into student values (?,?,?)";//preparedstatement ps=conn.preparestatement (SQL);//            //Input Parameters//ps.setint (1,5);//ps.setstring (2, "Eee");//ps.setstring (3, "888");//            //Ps.execute (); //precompilation: Modifying Records//String sql= "update student set pass=? where Name=?";//preparedstatement ps=conn.preparestatement (SQL); //ps.setstring (1, "666"); //ps.setstring (2, "AAA"); //Ps.execute (); //precompilation: Deleting Records//String sql= "Delete from student where sid=?";//preparedstatement ps=conn.preparestatement (SQL);//Ps.setint (1, 1);//Ps.execute (); //precompilation: FindingString sql= "SELECT * from student where name is like?"; PreparedStatement PS=conn.preparestatement (SQL); Ps.setstring (1, "%a%"); ResultSet RS=Ps.executequery (); //Output Results             while(Rs.next ()) {System.out.println (rs.getstring ("Sid") + ";" +rs.getstring ("name") + ";" +rs.getstring ("Pass")); } System.out.println ("Record added successfully"); //System.out.println ("database connection succeeded"); //System.out.println ("record added successfully");}Catch(Exception e) {//e.printstacktrace ();SYSTEM.OUT.PRINTLN ("database connection failed =" +e.getmessage ()); }    }}

JAVA-JDBC Connecting 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.