9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python and MySQL JDBC to connect to a database?


Connecting to a database with Python and MySQL JDBC is an easy process. To do so, you'll need to install the MySQL JDBC driver and set up the connection.

  1. Install the MySQL JDBC driver:

    • Download the driver from the MySQL website: MySQL Connector/J.
    • Unzip the driver and add the mysql-connector-java-<version>-bin.jar file to your classpath.
  2. Set up the connection:

    import java.sql.DriverManager
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/<database>", "<user>", "<password>")
  3. Execute queries:

    cursor = conn.cursor()
    cursor.execute("SELECT * FROM <table>")
    print(cursor.fetchall())
  4. Close the connection:

    cursor.close()
    conn.close()

For more information, please refer to the MySQL Connector/J documentation.

Edit this code on GitHub