title: How to connect a Java application to db4free.net using JDBC?
body: <p>I'm doing a college project where we have to build a fully functioning Java application, complete with a basic user login system using JDBC.</p>
  
  <p>The basic user login system works fine when used on XAMPP's MariaDB MySQL branch via localhost.</p>
  
  <p>Obviously, the system wouldn't work outside of this particular network, so I looked around for ways to allow me to host a MySQL database online at all times so regardless of where I go, this basic user login system works and can be shown to anyone who's interested.</p>
  
  <p>I then found db4free.net, and so far everything looks in order - PHPMyAdmin works, and I managed to successfully export and import the database with the usernames and passwords from the localhost version to db4free's version.</p>
  
  <p>But I'm having trouble on how to actually point my application to connect to db4free's systems so the login system works as intended.</p>
  
  <p>Here's the "connection module" Java class that handles the connection:</p>
  
  <pre><code>package com.fixer.dal;
  
  import java.sql.*;
  
  public class Connection_Module {
      public static Connection connector(){
          java.sql.Connection cnx = null;
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://db4free.net:3306/db_fixer";
          String user = "myuser";
          String password = "mypassword";
  
          try {
              Class.forName(driver);
              cnx = DriverManager.getConnection(url, user, password);
              return cnx;
          } catch (Exception e) {
              return null;            
          }
     }
  }
  </code></pre>
  
  <p>And here's the function that checks with the database to see if the username and password match with the recorded data (and if it does, it closes the login screen and opens the "main" screen):</p>
  
  <pre><code>Connection cnx = null;
  PreparedStatement pst = null;
  ResultSet rs = null;
  
  public void login(){
  
      String sql = "select * from usertable where username=? and password=?";
      try {
          pst = cnx.prepareStatement(sql);
          pst.setString(1, Screen_Login_Username_Field.getText());
          pst.setString(2, Screen_Login_Password_Field.getText());
          rs = pst.executeQuery();
          if(rs.next()){
              Screen_Main Main = new Screen_Main();
              Main.setVisible(true);
              this.dispose();
              cnx.close();
          }else{
              JOptionPane.showMessageDialog(null, "Invalid user or password.");
          }
      } catch (Exception e) {
          JOptionPane.showMessageDialog(null, e);
      }
  
  }
  </code></pre>
  
  <p>And that's mostly my problem. db4free.net gave me a host ("db4free.net") and a port ("3306"), but I don't know exactly where do they go. I've tried some methods to get it to work based off other questions here but none successfully connected me to my database on their systems.</p>
  
  <p>What am I doing wrong?</p>