JDBC Connects to Oracle database

Source: Internet
Author: User

I remember sophomore time, we went to Java class, when the teacher said that JDBC this thing, also not very good to learn, until now, I do not until what JDBC is, I know this thing can extract data from the database, then what is JDBC?

JDBC is the abbreviation of Java Database connectivity, meaning that the connection of Java databases, do not need to explain more, I feel!

So how do we make a connection like this? Once you've used one, you'll find that it's actually very easy to remember a few steps, and there's the ability to import the necessary driver packages, and here are a few important steps:

The first step is that you need to import the necessary database driver packages in your project environment, I'm going to use the Oracle 11g database as a sample of the demo, I'm importing into the project and adding to the classpath of the Web project, Oracle, Now you can import the Class12.jar package.

The second step is to start writing the code and instantiate your driver package:

      Class.forName ("Oracle.jdbc.driver.OracleDriver");//Other databases just change the string number.
Step three: Create the connection, here we need to use the DriverManager drive tube class

      Connection conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:cgzs", "System", "hj123");   Just started to learn, the teachers are talking about this operation class, in fact, now very few people use this, specific, I also have a blog there will be introduced
Fourth step: The driver instantiation, also get to connect, then start querying the database bar, we need a database SQL state object, also need a query result set receive object, each is statement object and ResultSet object

Statement s = conn.createstatement (); ResultSet rs = Null;rs = S.executequery ("SELECT ID from A"), while (Rs.next ()) {System.out.println (rs.getstring ("id"));}
The last step: don't forget that the statement object we used earlier, the ResultSet object, and the connection object all need to close the connection, or else it will be finished.

Well, that's all, still very easy, the following stickers I wrote a complete demo!

public class Demo {/** * @param args * @throws SQLException * @throws classnotfoundexception */public static void Main (STR Ing[] args) throws Sqlexception,classnotfoundexception {//TODO auto-generated method stub//Instantiate drive Class.forName (" Oracle.jdbc.driver.OracleDriver ");//Establish connection Connection conn = Drivermanager.getconnection (" jdbc:oracle:thin:@ Localhost:1521:cgzs "," System "," hj123 ");//Statement object used to interact with the database statement s = conn.createstatement ();// Run the query operation ResultSet rs = s.executequery ("SELECT * from V_cancle"), while (Rs.next ()) {//Print query Results System.out.println ( Rs.getstring ("WWID"));} Close connection, critical conn.close (); S.close (); Rs.close ();}}









JDBC Connects 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.