Home > Database > Mysql Tutorial > body text

How to create a connection in mysql

下次还敢
Release: 2024-04-14 19:24:16
Original
726 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:

<code class="python">import mysql.connector</code>
Copy after login

For Java:

<code class="java">import java.sql.*;</code>
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:

<code class="python">connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="my_password",
    database="my_database"
)</code>
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:

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

3. Create a cursor object

Create a cursor object for executing queries and retrieving results.

For Python:

<code class="python">cursor = connection.cursor()</code>
Copy after login

For Java:

<code class="java">Statement statement = connection.createStatement();</code>
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:

<code class="python">cursor.execute("SELECT * FROM my_table")</code>
Copy after login

For Java:

<code class="java">ResultSet resultSet = statement.executeQuery("SELECT * FROM my_table");</code>
Copy after login

5. Retrieval results

Traverse the result set and retrieve individual rows.

For Python:

<code class="python">for row in cursor.fetchall():
    print(row)</code>
Copy after login

For Java:

<code class="java">while (resultSet.next()) {
    System.out.println(resultSet.getInt("id"));
}</code>
Copy after login

6. Close the connection

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

For Python:

<code class="python">cursor.close()
connection.close()</code>
Copy after login

For Java:

<code class="java">statement.close();
connection.close();</code>
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:
source:php.cn
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template