title: Convert PHP source to Java source?
body: <p>I have a class written in PHP which I use to communicate with my website databases. I am also writing a Java application which will communicate with a database too. I was wondering if it was possible to use the same structure but convert it to java and are there any issues I would need to consider if I do this. Here is my php class...</p>
  
  <pre><code>&lt;?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-&gt;open_connection();
      }
  
      private function open_connection() {
          $this-&gt;connection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
          if (!$this-&gt;connection) {
              die('Database connection failed: ' . mysql_error());
          } else {
              $this-&gt;selected_database = mysql_select_db(DB_NAME, $this-&gt;connection);
              if (!$this-&gt;selected_database) {
                  die('Database selection failed: ' . mysql_error());
              }
          }
      }
  
      private function close_connection() {
          if (isset($this-&gt;connection)) {
              mysql_close($this-&gt;connection);
              unset($this-&gt;connection);
          }
      }
  
      public function query($sql) {
          $this-&gt;last_query = $sql;
          $result = mysql_query($sql, $this-&gt;connection);
          $this-&gt;confirm_query($result);
          return $result;
      }
  
      private function confirm_query($result) {
          if (!$result) {
              die('Database query failed: ' . mysql_error() . '&lt;br&gt;Last SQL query: ' . $this-&gt;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-&gt;connection);
      }
  
      public function affected_rows() {
          return mysql_affected_rows($this-&gt;connection);
      }
  
      public function get_fields_from($table_name) {
          $result = $this-&gt;query("SHOW COLUMNS FROM `{$this-&gt;escape_value($table_name)}`");
          $fields = array();
          while ($row = $this-&gt;fetch_assoc($result)) {
              $fields[] = $row['Field'];
          }
          return $fields;
      }
  
  }
  
  ?&gt;
  </code></pre>