JAVA和PHP语法对比 - 读取mysql数据

302 阅读1分钟
  • PHP语法

    $con = mysqli_connect(
        'localhost',
        'root',
        'root',
        'database',
        '3306'
    );

    $result = mysqli_query($con, 'select * from db_name');

    while ($row = mysqli_fetch_row($result)) {
        echo 'column:'.$row[1]."<br>";
    }

    // 释放资源
    mysqli_free_result($result);
    // 关闭链接
    mysqli_close($con);

  • JAVA语法

   Connection conn = DriverManager.getConnection(
	"jdbc:mysql://localhost:3306/database?useSSL=false", 
	"root",
	"root"
    );

    Statement sta = conn.createStatement();
	
    String sql = "select * from db_name";

    ResultSet res = sta.executeQuery(sql);
    
    while(res.next()) {
    	System.out.println("column:" + res.getString(2));
    }

    // 关闭连接:
    conn.close();
    sta.close();
    res.close();