Java Connection to Oracle database

Source: Internet
Author: User
Tags oracleconnection

package com.db;            import  java.sql.connection;      import java.sql.drivermanager;       import java.sql.PreparedStatement;      import  Java.sql.resultset;            public class  DBConnection {          //  Connect to Oracle database            public void oracleconnection ()  {               Connection con =  Null;              preparedstatement  pre = null;               ResultSet rs = null;                     try {                   // 1.  Load Oracle Drivers                    class.forname ("Oracle.jdbc.driver.OracleDriver");                                      //  2.  set up Oracle database basic information                    String url =  "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";                   String  user =  "Scott"; &nbsP;                 string  password =  "Goodluck";                         // 2.  Get Connections                    con =  Drivermanager.getconnection (Url, user, password);         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----> Connection  success! ");                         // 3.  Execute SQL statements                    String sql =  "select *  from emp";                   Pre = con.preparestatement (SQL);                         // 4.  Get result sets                    rs  = pre.executequery ();                   while  (Rs.next ())  {                       system.out.println ("No.:"  +  rs.getstring ("Empno")                                          +  "; Name:"  + rs.getstring ("ename")                                         +  ";  work:"  +  Rs.getstring ("Job")                                         +  ";  leader:"  + rs.getstring ("Mgr")                                         +  ";   Employment Date: " + rs.getstring (" HireDate ")                                         +  ";  Salary:"  + rs.getstring ("Sal")                                         +  ";   Bonus: " + rs.getstring (" comm ")                                         +  ";  Department:"  + rs.getstring ("Deptno"));                   }               } catch  ( Exception e)  {                   E.printstacktrace ();               }  finally {                   try {                       if  (rs != null)                             rs.close ();                       if  (pre != null)                             pre.close ();                       if  (con != null)                             Con.close ();                 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----> Connection End <-----");                   }  catch  (exception e)  {                       e.printstacktrace ();                   }               }          }                 public static  Void main (String[] args)  {               dbconnection db = new dbconnection ();               db. OracleConnection ();          }       }


Java Connection to 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.