JDBC Connection Oracle Syntax

Source: Internet
Author: User

 Public classLangdemo { Public Static voidMain (string[] args)throwsexception{Try {                 //Load DriverClass.forName ("Oracle.jdbc.driver.OracleDriver"); //Create a connectionConnection cn=drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:xe", "hh", "HH"); Statement SM=cn.createstatement (); //get the result setResultSet rs=sm.executequery ("Select Name,nickname,id from a"); //traversing result sets                  while(Rs.next ()) {System.out.println ("Name:" +rs.getstring (1));//rs.getstring ("name")System.out.println ("Nickname:" +rs.getstring (2));//rs.getint ("id")}        } Catch(Exception e) {e.printstacktrace (); }finally{//Rs.close (); //conn.close ();                 }    }}
 Public classLangdemo { Public Static voidMain (string[] args)throwsexception{Connection con=NULL;//Create a connection to a databasePreparedStatement pre =NULL;//Create precompiled statement objects, usually with this instead of statementResultSet result =NULL;//Create a result set object     Try{class.forname ("Oracle.jdbc.driver.OracleDriver");//load the Oracle driverSystem.out.println ("Start trying to connect to the database!") "); String URL= "Jdbc:oracle:" + "thin:@127.0.0.1:1521:xe";//127.0.0.1 is a native address, XE is the default database name for Lite OracleString user = "System";//user name, system default account nameString Password = "147";//The password you selected when you installed the settingscon = drivermanager.getconnection (url, user, password);//Get ConnectionsSYSTEM.OUT.PRINTLN ("Connection Successful! "); String SQL= "SELECT * from student where name=?";//precompiled statement, "? The delegate parameterPre = con.preparestatement (SQL);//Instantiating precompiled StatementsPre.setstring (1, "Zhang San");//The parameter is set, and the previous 1 indicates the index of the parameter, not the index of the column name in the tableresult = Pre.executequery ();//Execute the query, note that no additional arguments are required in parentheses         while(Result.next ())//When the result set is not emptySystem.out.println ("School Number:" + result.getint ("id") + "Name:" + result.getstring ("name")); }      Catch(Exception e) {e.printstacktrace (); }       finally{..... }     }}

JDBC Connection Oracle Syntax

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.