0

I'm making a Sign Up form and I made everything but still it wont connect. I hope someone here can help.

My myConnection.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class myConnection {
    public static Connection getConnection() {
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://turtlenetwork.net/contact" + "user=IDKLMAO&password=OkIKnowYouTriedButNOLOL");
        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        return con;
    }
}

SignUp

private void jLabelCreateMouseClicked(java.awt.event.MouseEvent evt) {                                          
        Connection con = myConnection.getConnection();
        PreparedStatement ps;
        try {
            ps = con.prepareStatement("INSERT INTO `user`(`username`, `pass`, `mail`) VALUES (?,?,?)");
            ps.setString(1, jTextField1.getText());
            ps.setString(2, String.valueOf(jPasswordField1.getPassword()));
            ps.setString(3, jTextField3.getText());
            if (ps.executeUpdate() != 0) {
                JOptionPane.showMessageDialog(null, "Account Created");
            } else {
                JOptionPane.showMessageDialog(null, "Failed");
            }
                    } catch (HeadlessException | SQLException ex) {
            Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Here is the error that I get when I run click the create button.

https://pastebin.com/ce44LYzW

It's supposed to write everything on the database and make that account.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JustStanix
  • 15
  • 8
  • 2
    Do you get any error? – Pavel Smirnov Mar 24 '19 at 00:05
  • For the root cause, see https://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql and its duplicate. Part of your problem is that you are ignoring exceptions and then continue on as if nothing happens. That is not proper exception handling. Logging exceptions is the minimum you should do, but you should take corrective action or abort what you are doing. – Mark Rotteveel Mar 24 '19 at 14:59

2 Answers2

0

Connect Java to a MySQL database

https://www.javatpoint.com/example-to-connect-to-the-mysql-database

Whenever you make a connection in java, you need to make sure to add in the username and the password with two coma's.

con = DriverManager.getConnection("jdbc:mysql://turtlenetwork.net/contact", "username", "password");

Make sure to separate user and password with a coma.

Also, check out DriverManager documentation here

https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html

pyth12
  • 9
  • 1
  • 7
0

I have managed to solve the issue on my own. Here is the new myConnection.java

package ContactList;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class myConnection {
    // Init Database Constants
    private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/contact";
    private static final String USERNAME = "NOPE";
    private static final String PASSWORD = "NOPE";
    private static final String MAX_POOL = "250";

    // Init Connection Object
    private Connection connection;
    // Init Properties Object
    private Properties properties;

    // Create Properties
    private Properties getProperties() {
        if (properties == null) {
            properties = new Properties();
            properties.setProperty("user", USERNAME);
            properties.setProperty("password", PASSWORD);
            properties.setProperty("MaxPooledStatements", MAX_POOL);
        }
        return properties;
    }

    // Connect Database
    public Connection connect() {
        if (connection == null) {
            try {
                Class.forName(DATABASE_DRIVER);
                connection = DriverManager.getConnection(DATABASE_URL, getProperties());
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    // Disconnect Database
    public void disconnect() {
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    PreparedStatement prepareStatement(String insert_into_userusername_pass_mail_VALUES) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

And then there is the SignUp which I changed aswell.

private void jLabelCreateMouseClicked(java.awt.event.MouseEvent evt) {                                          
        myConnection con = new myConnection();
        String sql = "INSERT INTO `user`(`username`, `pass`, `mail`) VALUES (?, ?, ?)";
        try {
            PreparedStatement ps = con.connect().prepareStatement(sql);
            ps.setString(1, jTextField1.getText());
            ps.setString(2, String.valueOf(jPasswordField1.getPassword()));
            ps.setString(3, jTextField3.getText());
            if (ps.executeUpdate() != 0) {
                JOptionPane.showMessageDialog(null, "Account Created");
            } else {
                JOptionPane.showMessageDialog(null, "Failed");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            con.disconnect();
        }
    }

Thanks to everyone that tried to help.

JustStanix
  • 15
  • 8