Getting Started with Oracle (5)--java Connecting to Oracle Database

Source: Internet
Author: User
Tags getdate

"This article describes"

The previous several said so much, finally did not talk about how to connect to the database with Java, this article is practical, talk about how to connect the database.

"Java Connection to Oracle database"

1 . Import the JDBC driver: see here, busy looking for the driver on the Internet? No, the installation of Oracle has its own driver, the path is: The installation directory \product\11.2.0\dbhome_1\jdbc\lib, below a number of jar packages, choose the one that suits you (the simplest way is to import the project to see if you can connect, Can be connected is the appropriate driver)

2. Code:

about the URL: @ After the IP: port, these two do not know how to check the blog: http://www.cnblogs.com/xiaoMzjm/p/3884470.html

about SQL statements : You see inside the table name in the "" enclosed, the thing is, I use the graphical tool navicat for Oracle to build the table, to access the table to add "", the command line to build the table is not used. this deserves attention! "User" and user are two different tables!

1  Packagecom.zjm.www.test;2  3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.Statement;7  8  Public classTest {9  Ten      Public Static voidgetDate () { OneString driverclass= "Oracle.jdbc.driver.OracleDriver";  AString url= "Jdbc:oracle:thin: @localhost: 1521:orcl";  -String username= "Test";  -String password= "Aaa38324836";  theString sql= "select * from \" Myuser\ ""; -         Try{   -Class.forName (Driverclass). newinstance ();//Load Driver -Connection conn=drivermanager.getconnection (Url,username,password);//Get Connected +Statement stmt=conn.createstatement ();  -ResultSet rs=stmt.executequery (SQL);  +              while(Rs.next ()) { ASystem.out.println (rs.getstring ("name"));  at                 }            -}Catch(Exception e) { - E.printstacktrace ();  -                 }         -         } -       in     /*** Main function*/  -      Public Static voidMain (string[] args) { to getDate (); +     } -}

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.