JDBC Connection for Oracle

Source: Internet
Author: User

has been using MySQL, has recently switched to Oracle, Oracle's first step is the JDBC connection

The first step: Download the jar package, I use the oracle11g is Oracle6.jar

Step two: Load the jar package

Then find the downloaded Jar location.

Step Three: Code section

 PackageCom.lc.jdbc;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.util.ArrayList;Importjava.util.List; Public classORACLE_JDBC {StaticString Driver = "Oracle.jdbc.driver.OracleDriver";//database-driven classes   StaticString username = "ATM";//Database user name   StaticString password = "ATM";//Database Password   StaticString url = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";//the address of the connection database//Traverse    PublicList<userinfo>Select () {Connection con;       PreparedStatement PS; String SQL= "SELECT * from UserInfo"; List<Userinfo> userinfolist =NewArraylist<userinfo>(); Try{class.forname (driver);//Load Drivercon = drivermanager.getconnection (URL, username, password);//Create a connectionPS = con.preparestatement (SQL);//to create a precompiled SQL DeclarationResultSet rs =Ps.executequery ();  while(Rs.next ()) {Userinfo U=NewUserinfo (); U.setcustomerid (Rs.getstring ("CustomerID")); U.setcustomername (Rs.getstring ("CustomerName")); U.setpid (Rs.getstring ("pid")); U.settelephone (Rs.getstring ("Telephone")); U.setaddress ("Address");           Userinfolist.add (U);           } rs.close ();           Ps.close (); Con.close ();//Close Connection}Catch(Exception e) {e.printstacktrace (); }        returnuserinfolist; }}

The above is just a simple traversal, like adding and removing changes almost like, can not

Entity class table: corresponds to Oracle table one by one

Package Com.lc.jdbc;public class Userinfo {@Overridepublic String toString () {return "Userinfo [customerid=" + CustomerID + ", customername=" + CustomerName + ", pid=" + pid + ", telephone=" + telephone + ", address=" + address + "]";} public string Customerid;public string Customername;public string Pid;public string telephone;public string address; Public String Getcustomerid () {return customerid;} public void Setcustomerid (String customerid) {This.customerid = CustomerID;} Public String Getcustomername () {return customername;} public void Setcustomername (String customername) {this.customername = CustomerName;} Public String Getpid () {return PID;} public void Setpid (String pid) {this.pid = pid;} Public String Gettelephone () {return telephone;} public void Settelephone (String telephone) {this.telephone = telephone;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}

This is a success,

Here is the test code and the results of Oracle's operation

 Packagecom.lc.test;Importjava.util.List;ImportCom.lc.jdbc.Oracle_jdbc;ImportCom.lc.jdbc.Userinfo; Public classTest { Public voidTest () {System.out.println ("Oracle Query ..."); ORACLE_JDBC OJ=NewOracle_jdbc (); List<Userinfo> list =OJ.         Select ();  for(Userinfo u:list) {System.out.println (u.tostring ()); }    }     Public Static voidMain (string[] args) {Newtest (). Test (); }}

The above is the JDBC connection for Oracle!

 

JDBC Connection for Oracle

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.