Introduction to JDBC and connection to the database

Source: Internet
Author: User
Tags sybase server port

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

Introduction to JDBC:

1.JDBC Design Concept:
Java relies on the database to store the data that the program needs, we write a program in Java, we will store the data needed for this program in the database, when the client runs the program, the program needs to read the data in the database, the implementation of this operation is JDBC. and in order to standardize, in order to unified management, the responsibility to the database developers (in detail in the interface), Java is only responsible for writing a series of interfaces, as to how to achieve "learning Java, to the Kay Brothers School Kaige123.com" These interfaces, it is the database developers of things, So in addition to the JDBC API, there is one operation class and the other is the interface.

2. What is a driver class:

To enable Java programs to connect to their own databases, database developers have to write their own classes that implement these interfaces and then turn them into jar packages for programmers, so that the program can read the data in the database as long as the programmer adds the jar packages to the project. The operation classes that implement interfaces are called driver classes.

3.JDBC Common Interface Introduction (below the java.sql package):

Dirvermanager (Driver Administrator): This is an action class that is used to create a connection object. Connection: Database Connection object Statement:sql execution object Preparedstatement:sql preprocessing execution object resultset: Result set of query ResultSetMetaData: query table structure callablestatement: Save Stored Procedure Call
To connect to a database step:

1. To connect to what database to find what database driver and then add to the project, in fact, is to download the corresponding jar package (below to connect MySQL database as an example):

1.1Maven Engineering:

Go to the Central library to find this jar package and add dependencies in the POM:

1.2 Common Java Engineering:

Locate the project jar package, copy it to the project, then select the jar package in the project, right-click Add to build Path, the jar package can be downloaded by itself, and here I use the jar package downloaded from the above Maven project.

Note: If you want to delete this jar package, you need to select the corresponding small bottle pattern and then right-select Remove from Build path, and then delete the pasted jar package.

2 start the database to ensure that the database can be connected in network mode

3 Load Driver

3.1 Load Drive principle and Purpose: Load the driver is actually to load the driver class, so that the static blocks inside the driver class will be executed, after the execution will be immediately registered driver to the driver administrator class. is the contents of the driver class:

So first we're going to load the driver class: There are several ways to load a class, you can new its object, you can use a method that specifically loads the class Class.forName (), and so on, generally we tend to use the load class method, code such as:

3.2 Some databases have actually automatically helped us load the driver class, so even if we don't load the driver class in our code, the runtime will automatically load it for us, like MySQL and Oracle databases.

3.3 How to tell if the database does not automatically help us load the driver class: In the database jar package to see if there is no automatic loading files in the Meta-inf/servies directory. In MySQL, for example, found in its directory Java.sql.Driver file, the contents are as follows:

4. Create a Connection object using the DriverManager administrator

4.1 When creating an object, we need to pass in three parameters: URL, username, password

4.2 URL: Used to tell the class what database to connect to, the address and port of the server, the name of the database, and other settings (such as font settings)

4.3 The URL in MySQL:

4.3.1 default local and default port 3306:

jdbc:mysql:///database name

4.3.2 Custom:

jdbc:mysql://Server Address: Server port/Database name if you write the default in this way, that is jdbc:mysql://localhost (write local address): 3306/mysql

4.3.3 solve the problem of Chinese garbled (if the database is Chinese and is not Chinese or add in is Chinese, but in the database is garbled in the form of the existence can be used in this way to solve)

Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8

Note: The charset set here is consistent with the character set in the database, this is UTF-8, and the database must also be UTF-8.

4.4 Code implementation:

5. Key points for code hints for various database connections:

Oracle database            driverClass:oracle.jdbc.driver.OracleDriver            url:jdbc:oracle:thin:@127.0.0.1:1521: Dbnamemysql Database            DriverClass:com.mysql.jdbc.Driver

(PS: Sometimes, the MySQL driver class will also see the use of the Org.gjt.mm.mysql.Driver case, Org.gjt.mm.mysql.Driver is the early driver name, later renamed to Com.mysql.jdbc.Driver, now generally recommended to use Com.mysql.jdbc.Driver. In the latest version of MySQL JDBC driver, in order to maintain the old version of compatibility, still retained the org.gjt.mm.mysql.Driver, but in fact Org.gjt.mm.mysql.Driver called the Com.mysql.jdbc.Driver, so now the two drivers are no different. )

URL:JDBC:MYSQL://LOCALHOST:3306/MYDBDB2 database            driverClass:com.ibm.db2.jcc.DB2Driver            url:jdbc:db2:// 127.0.0.1:50000/dbnamesybase database            driverClass:com.sybase.jdbc.SybDriver            url:jdbc:sybase:Tds:localhost : 5007/dbnamepostgresql database            driverClass:org.postgresql.Driver            url:jdbc:postgresql://localhost/dbnamesql Server2000 database            driverClass:com.microsoft.jdbc.sqlserver.SQLServerDriver            url:jdbc:microsoft:sqlserver:// Localhost:1433;databasename=dbnamesql Server2005 Database            DriverClass:com.microsoft.sqlserver.jdbc.SQLServerDriver        url:jdbc:sqlserver://localhost:1433; Databasename=dbname

Introduction to JDBC and connection to the 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.