PolarDog
PolarDog

Reputation: 43

How to connect a Java application to db4free.net using JDBC?

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.

The basic user login system works fine when used on XAMPP's MariaDB MySQL branch via localhost.

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.

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.

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.

Here's the "connection module" Java class that handles the connection:

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;            
        }
   }
}

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):

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);
    }

}

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.

What am I doing wrong?

Upvotes: 1

Views: 3610

Answers (1)

The Impaler
The Impaler

Reputation: 48865

I just created a database (for the first time) in db4free.com and I used:

  1. MySQL 8.x JDBC driver (I used the JDBC driver 8.0.11).

  2. The URL is:

jdbc:mysql://db4free.net:3306/database-name

In MySQL 8.x, however, it's better to add the following parameters to avoid dealing with tons of warnings:

jdbc:mysql://db4free.net:3306/database-name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false

It worked like a charm at once. Did you confirm the email they sent? Is your account active? Are you using the JDBC driver 8.x?

Upvotes: 5

Related Questions