0
我有一個用PHP編寫的類,用於與我的網站數據庫進行通信。我也在編寫一個Java應用程序,它也將與數據庫進行通信。我想知道是否有可能使用相同的結構,但將其轉換爲Java,並有任何問題,我需要考慮,如果我這樣做。這是我的PHP類...將PHP源代碼轉換爲Java源代碼?
<?php
class Database {
private static $instance = null;
private $connection;
private $selected_database;
private $last_query;
public static function getInstance() {
if (self::$instance != null) {
return self::$instance;
} else {
return new self;
}
}
function __construct() {
$this->open_connection();
}
private function open_connection() {
$this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if (!$this->connection) {
die('Database connection failed: ' . mysql_error());
} else {
$this->selected_database = mysql_select_db(DB_NAME, $this->connection);
if (!$this->selected_database) {
die('Database selection failed: ' . mysql_error());
}
}
}
private function close_connection() {
if (isset($this->connection)) {
mysql_close($this->connection);
unset($this->connection);
}
}
public function query($sql) {
$this->last_query = $sql;
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result) {
if (!$result) {
die('Database query failed: ' . mysql_error() . '<br>Last SQL query: ' . $this->last_query);
}
}
public function escape_value($string) {
return mysql_real_escape_string($string);
}
public function fetch_array($result) {
return mysql_fetch_array($result);
}
public function fetch_assoc($result) {
return mysql_fetch_assoc($result);
}
public function num_rows($result) {
return mysql_num_rows($result);
}
public function insert_id() {
return mysql_insert_id($this->connection);
}
public function affected_rows() {
return mysql_affected_rows($this->connection);
}
public function get_fields_from($table_name) {
$result = $this->query("SHOW COLUMNS FROM `{$this->escape_value($table_name)}`");
$fields = array();
while ($row = $this->fetch_assoc($result)) {
$fields[] = $row['Field'];
}
return $fields;
}
}
?>
如果確定,是的,你可以將程序從任何語言轉換爲其他語言,當然你必須自己編寫代碼! –
我不希望任何人爲我寫代碼,那裏的樂趣在哪裏?我只是想知道任何我應該注意的事情,同時轉換此代碼 –
如果我一直對我發佈的每個問題感到不滿,因爲上週我反應的方式不同,那麼我會在這裏停止提問。我正在建立一個在線撲克網站,你真的想錯過看到一些代碼的機會嗎? –