Home > Database > Mysql Tutorial > How to create a connection in mysql

How to create a connection in mysql

下次还敢
Release: 2024-04-14 19:24:16
Original
911 people have browsed it

The following steps are required to establish a MySQL connection: 1. Import the MySQL client library; 2. Create a connection object; 3. Create a cursor object; 4. Execute the query; 5. Retrieve the results; 6. Close the connection.

How to create a connection in mysql

How to create a MySQL connection

Establishing a MySQL connection requires the following steps:

1. Import the MySQL client library

First, you need to import the necessary MySQL client library, which provides an interface for the application to communicate with the MySQL database.

For Python:

import mysql.connector
Copy after login

For Java:

import java.sql.*;
Copy after login

2. Create a connection object

Create a connection using the client library Object that will represent a connection to the MySQL database.

For Python:

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="my_password",
    database="my_database"
)
Copy after login
  • host: The address or hostname of the database server
  • user: Has a connection Permission username
  • password: User’s password
  • database: Name of the database to be connected

For Java:

// 使用 JDBC 创建连接对象
Connection connection = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/my_database",
    "root",
    "my_password"
);
Copy after login

3. Create a cursor object

Create a cursor object for executing queries and retrieving results.

For Python:

cursor = connection.cursor()
Copy after login

For Java:

Statement statement = connection.createStatement();
Copy after login

4. Execute the query

Use the cursor object to execute the query and put The results are stored in the result set.

For Python:

cursor.execute("SELECT * FROM my_table")
Copy after login

For Java:

ResultSet resultSet = statement.executeQuery("SELECT * FROM my_table");
Copy after login

5. Retrieval results

Traverse the result set and retrieve individual rows.

For Python:

for row in cursor.fetchall():
    print(row)
Copy after login

For Java:

while (resultSet.next()) {
    System.out.println(resultSet.getInt("id"));
}
Copy after login

6. Close the connection

After using the connection, please close the connection object to release resources.

For Python:

cursor.close()
connection.close()
Copy after login

For Java:

statement.close();
connection.close();
Copy after login

The above is the detailed content of How to create a connection in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template