首先,
<?php
echo "hello, world!";
?>
而
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
那么,
第一,使用
// Java code
public class Calculation{
public static int add(int x, int y){
return x+y;
}
}
// PHP code
require_once("java/Java.inc");
$calc = new Java("Calculation");
echo $calc->add(3, 4);
第二,我們可以使用
// Java code
public class DBHelper{
private Connection conn;
private String url;
private String user;
private String password;
public DBHelper(String url, String user, String password){
this.url = url;
this.user = user;
this.password = password;
}
public Connection getConnection(){
if(conn != null){
return conn;
}
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public void close(Statement stmt, ResultSet rs){
try{
if(rs != null){
rs.close();
}
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
// PHP code
require_once("java/Java.inc");
$dbHelper = new Java("DBHelper", "jdbc:mysql://localhost/test", "root", "");
$conn = $dbHelper->getConnection();
$stmt = $conn->prepareStatement("SELECT * FROM user");
$rs = $stmt->executeQuery();
while($rs->next()){
echo $rs->getString("name");
}
$conn->close($stmt, $rs);
第三,使用
綜合而言,