Java database Connection (JDBC)

Source: Internet
Author: User
Tags stmt

In modern program development, a lot of development is based on database, the connection of Java database is particularly important. JDBC provides a platform-agnostic standard Java API for executing SQL statements that facilitates uniform operation of multiple relational databases, consisting of a set of classes and interfaces written in Java.

Core components of JDBC {

DriverManager class;//The operation class used to get the database connection interface (Connection) object.

connection Port;//Represents a database connection, which is the type returned after a database connection, used to receive an interface to a connected database;

statement Port;//Perform database operations and instantiate using the Createstatement () method in the connection interface.

PreparedStatement Interface,//pre-processing, statement sub-interface, the operation of the specific contents of the SQL statement is not set first, this method is often used when inserting data, high frequency in the javaweb.

ResultSet interface;//store data record, and remove.

}

To connect to a database:

1--> load the database driver, each database driver is not the same, by each database vendor (typically *.zip or *.jar) is configured by the Clsspath path or directly into the project structure node of Eclipse. The relevant data-driven packages can be downloaded to the database's official website.

2--> connection database, need address, each database address is different, such as MySQL connection address is (jdbc:mysql://localhost:3306/db), where localhost when your host name, 3306 is the port number when you install the database, and DB is the name of the database you created yourself. The database connection address can be queried online or queried for database driver packages. The connection to the general database also requires the user name and password of the database (which you configured when you installed MySQL). Once connected, you can manipulate the database.

3--> operates using database operation statements.

4--> close the database connection.

Specific code to connect to the database (for example, MySQL):

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.SQLException;4 ImportJava.sql.*;5 6 Importcom.mysql.jdbc.Statement;7 8 9  Public classBdhelpe {Ten     Private StaticString driver= "Com.mysql.jdbc.Driver"; One     Private Final StaticString url= "jdbc:mysql://localhost:3306/db"; A     Private Final StaticString username= "Root"; -     Private Final StaticString userpwd= "Password"; -      Public Static voidMain (string[] args) { the         Try { -Class.forName (driver);//Load Database driver -Connection conn=drivermanager.getconnection (URL,USERNAME,USERPWD);//connect to database based on specified content -Statement stmt= conn.createstatement ();//Database Operations Object +String sql= "select * from Db.student;"; -ResultSet rs=stmt.executequery (SQL);// Execute query statement  +System.out.println ("School number \ t name \ t Gender"); A              while(Rs.next ()) { at                 intId=rs.getint (1); -String name=rs.getstring (2); -String sex=rs.getstring (3); -System.out.println (id+ "\ t" +name+ "\ T" +sex); -}*/ - conn.close (); in              -}Catch(SQLException e) { to             //TODO auto-generated Catch block + e.printstacktrace (); -}Catch(ClassNotFoundException e) { the             //TODO auto-generated Catch block * e.printstacktrace (); $         }Panax Notoginseng          -     } the  +}

Java database Connection (JDBC)

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.