Connecting to a database

Source: Internet
Author: User

In Java, the interfaces and classes for all database operations are in java.sql

One class: DriverManager class

Four interfaces: Connection,statement,resultset,preparedstatement.

All JDBC Connection database operations are fixed

1. Fake database driver (load to container)

2. Database connection (completed by DriverManager class, connection indicates connection)

3. Crud for data (Statement,preparedstatement,resultset)

4. Close the database and connect (close the connection directly);

1. Loading drivers

All JDBC is a database driver provided by a different database manufacturer, which is given in the jar file, so the ouch uses it to configure CLASSPATH and then sets the driver's Bao. class

-Driver found: D:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar

Oracle Driver: Oracle.jdbc.driver.OracleDriver

-Load class using Class.forName ("Oracle.jdbc.driver.OracleDriver");

2. Connect to the database

-Database connection Address jdbc:oracle: How to connect

-MLDN database to connect to this machine Jdbc:oracle:thin:localhost:1521:mldn;

-User name for database Scott

-Password for the database

-Database service to open listener and that MLDN

To connect to a database you must rely on the DriverManager class Getconnection method

public static Connection getconnection (string url, string user, String password) throws SQLException


In JDBC every connection to a database is encapsulated with a connection object.

4. Close the database

The Close method of connection

Try to connect to the database:

    Private StaticFinal String Dbdriver ="Oracle.jdbc.driver.OracleDriver"; Private StaticFinal String Dburl ="Jdbc:oracle:thin: @localhost: 1521:mldn"; Private StaticFinal String USER ="Scott" ; Private StaticFinal String Passworld ="Tiger" ;  Public Static voidMain (string[] args) throws exception{//The first step is to load the database driver, which does not need to be instantiated at this time, and there will be a container responsible for managingClass.forName (Dbdriver); //The second step connects the databaseConnection conn =drivermanager.getconnection (Dburl,user,passworld); System. out. println (conn); Conn.close ();

Most of the time, the database is not connected:

Cause Analysis:

-Someone likes to change the computer name

-There may be an error listening to the service, the host name being monitored is not the computer name of the machine, and do not use the IP address

Listen for network-in profile directory Addmin->listener.ora (listening files) and TNSNames (listen for named files)

Listener files:

If there is a problem with the listener, there will be a notification host error.

  

May not find the specified SID database name is SID many times the name does not sleep automatically register, only the database name does not have SID name

Open the Database administration tool

Find the Net Manager

Add a Database

Will modify the listener file

Structure Summary

-----The entire database connection operation can be done in the same steps

DriverManager class Get Connection connection

Factory design Pattern

Connecting to a database

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.