MySQL是目前比較流行的關(guān)系型數(shù)據(jù)庫,支持多種編程語言對其進(jìn)行訪問和操作。本文將介紹MySQL的幾種連接技術(shù)。
1. JDBC連接
import java.sql.*; public class MySQLDemo { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test_db?useSSL=false"; String user = "root"; String password = "123456"; try { Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM user"); while (rs.next()) { System.out.println(rs.getString("username") + "," + rs.getString("password")); } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
2. PHP連接
0) { while($row = mysqli_fetch_assoc($result)) { echo "username: " . $row["username"]. ", password: " . $row["password"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ?>
3. Python連接
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", password="123456", database="test_db" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM user") myresult = mycursor.fetchall() for x in myresult: print(x) mydb.close()
以上為MySQL的幾種連接技術(shù),開發(fā)者可以根據(jù)自己的需求和熟練程度選擇合適的連接方式。