MySQL是目前比较流行的关系型数据库,支持多种编程语言对其进行访问和操作。本文将介绍MySQL的几种连接技术。
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.connectormydb = 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的几种连接技术,开发者可以根据自己的需求和熟练程度选择合适的连接方式。