Java Learning--a little bit of sharing about JDBC

Source: Internet
Author: User

1. Download the driver and deploy
We need to connect to the database before we operate on the database (this is the case with IntelliJ idea connected to MySQL).

(1), create a New Idea project, download the database connection driver, and put it under Idea's project folder.

(2), loading the JDBC driver

Before connecting to a database, you first load the driver of the database you want to connect to the JVM (Java Virtual machine), which can be implemented using the forname (String className) method.

2. Connect to the database and get the database connection object

(1) Parameters required for JDBC Connection (User,password,url)

A, user username

b, password password

c, the URL defines the protocol, sub-protocol, and data source identity when the database is connected.

(2) Creating a connection to a database
A, to connect to the database, you need to request to Java.sql.DriverManager and get the Connection object, which represents a database connection.
B, use DriverManager's Getconnectin (string URL, string username, string password) method to pass in the path of the specified database to be connected, the user name of the database, and
Password to get it.

(3) Create a PreparedStatement

To execute the SQL statement, you must obtain the Java.sql.Statement instance, which is divided into the following 3 types of statement instances:
A, execute static SQL statements. Typically implemented through statement instances.
B, execute the dynamic SQL statement. Typically implemented through PreparedStatement instances.
C, execute the database stored procedure. Typically implemented by CallableStatement instances

3. Execute SQL statements
The statement interface provides three ways to execute SQL statements: ExecuteQuery, executeupdate, and execute

The database Connection object Conn calls the above statement method (static SQL statement) and assigns the SQL statement as a string stu_mgr_select_select_all

Two scenarios for manipulating the result set:
1. The update returned is the record affected by this operation.
2. The result of executing the query is a ResultSet object (the following is a query)

4. Close the JDBC Object resource

After the operation is complete, all the JDBC objects used are closed to release the JDBC resource.

====================================================================================== Split Line

First blog, if there is an incorrect place, you are welcome to correct me.

Source code down face, driver can go to Sun Company to download the latest version

Package Studentscoremgrsystem;
Introduce interfaces, connect to databases, perform database operations, use JDBC
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Class Selectstudentmssg_handler implements Studentmssg_interface {
public void Selectstu () {
System.out.println ("Find student Information");
Load the JDBC driver package
String Driver = "Com.mysql.jdbc.Driver";
Database connection string
String url = "Jdbc:mysql://127.0.0.1:3306/curd";
User name
String username = "root";
Password
String password = "4950";
Connection conn = null;
Statement stu_mgr_select_stmt = null;
ResultSet stu_mgr_select_rs = null;
try {
1. Load the database driver (after a successful load, the instance of the driver class is registered in the DriverManager Class)
Class.forName (driver);
2. Get a database connection
conn = drivermanager.getconnection (URL, username, password);

if (!conn.isclosed ())
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");
3. Get Database Operations objects
stu_mgr_select_stmt = Conn.createstatement ();
4. Define SQL statements for operations
String stu_mgr_select_select_all = "SELECT * from Stu_score";
5, perform the database operation, the result is saved in stu_mgr_select_
Stu_mgr_select_rs = Stu_mgr_select_stmt.executequery (Stu_mgr_select_select_all);
6. Get and manipulate the result set

} catch (Exception e) {
E.printstacktrace ();
}finally {
7. Close objects, recycle database resources
if (stu_mgr_select_rs! = null) {//close result set object
try {
Stu_mgr_select_rs.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
if (stu_mgr_select_stmt! = null) {//Close Database Operations Object
try {
Stu_mgr_select_stmt.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
IF (conn! = null) {//Close database Connection object
try {
if (!conn.isclosed ()) {
Conn.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
}

}

}

Java Learning--a little bit of sharing about JDBC

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.