-1

So I was just wondering, what (and probably, how much...) have I done wrong here with this code?

try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection c = DriverManager.getConnection("jdbc:mysql://mysql1.000webhost.com/mydatabase", "myusername", "mypassword");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
}

As I've triple-checked the username/password, I'm guessing it's something wrong with the host name. The database is only on the server (I don't have any kind of saved local version or anything...do I need to?).

And also, can someone just tell me if I'm on the right lines for what I want to do? Basically I've created a piece of software with a free version and a very cheap paid version. I was thinking that to prevent piracy, since the program requires internet connection anyway, I could store their email address as the username, then their computer's MAC address would be the password (each time the program was run, I would compare the MAC address on their PC with the one registered along with their email in the database. I've got no idea whether that is a good anti-piracy measure, but I was just wondering, if I manage to get the connection working, is that something that I'd be able to do or would there be e.g. security issues with that?

Anyway, thanks in advance :)

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
Josh M
  • 21
  • 1
  • 4
  • 2
    What exception are you getting exactly? – Mat Jan 10 '15 at 12:43
  • 1
    Please open command prompt and run the following command "telnet mysql1.000webhost.com PORT" where PORT is the MYSQL port on which database listens for connection -- check the output – user1428716 Jan 10 '15 at 12:54

5 Answers5

1

if it is not localhost i cannot comment on the host but you also have to give port number.It is missing.

Connection  con = DriverManager
                .getConnection("jdbc:mysql://"+pHost+":"+pPort+"/Your_mysql_schema_name",username, password);

and also in MYSQL your schema name would be your database name.Ensure that you are giving schema name and also port number.Usually for MYSQL its 3306

  • Yeah I'm not sure why none of the answers worked. I've decided I'll just use a different method now, but even when you ping mysql.000webhost.com you get nothing received.. – Josh M Jan 10 '15 at 19:33
  • Hi @JoshM sorry for the late response.Hope you have already got this issue fixed.But i think if you are not able to ping your MYSQL server mysql.000webhost.com.Make sure it is up n running,May be its url is different.But idelly this should work. – Mani manasa mylavarapu Jan 13 '15 at 09:44
0

Writing a piece of java code to operate your database from a remote connection is not a good idea. Someone could reverse engineer your code and change your data.

You should atleast Implement an simple service on the net that could handle the spam you might receive, and protect your data.

Daniel Persson
  • 602
  • 7
  • 17
  • Yeah I definitely need to look into this stuff for the future, but in this case, I'm only gonna be selling the software for like 5$...hopefully people have better things to do than try and crack a 5$ piece of software ;) – Josh M Jan 10 '15 at 19:35
0

I Think you missed the database port no in your URL .Try this :

try {
    Class.forName("com.mysql.jdbc.Driver"); // Not Required for JDBC 4.0 onwards
    Connection c = DriverManager.getConnection("jdbc:mysql://mysql1.000webhost.com: 3306/mydatabase", "myusername", "mypassword");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
Amitesh Rai
  • 846
  • 11
  • 21
  • I did miss that :) For some reason it still doesn't work though. It doesn't really matter to be honest, as I think I'll just go through the old fashioned way of manually sending people their security key... – Josh M Jan 10 '15 at 19:28
  • @JoshM Can you please post the StackTrace of the Exception you are getting so that I can figure out what exactly is going wrong.? – Amitesh Rai Jan 10 '15 at 19:38
  • @JoshM, Also try replacing "mysql1.000webhost.com" inyour URL with "localhost" OR your computer name – Amitesh Rai Jan 10 '15 at 19:40
0

Try instead of mysql1.000webhost.com to change with server IP address.
Example,

try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection c = DriverManager.getConnection("jdbc:mysql://123.456.789.012:3306/mydatabase", "myusername", "mypassword");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
Voqus
  • 159
  • 1
  • 11
  • No, unfortunately that doesn't work. On 000Webhost its says: "Important: MySQL Host for any database in this account is mysql5.000webhost.com , do not use localhost!" However, when I try pinging that server, I get no packets received all the time...oh well :( – Josh M Jan 10 '15 at 19:26
0

I would recommend you some reading first. This slide show might present you how Java EE applications are build.

Next you might want to read a bit more how to connect your application with a database.

Hibernate is one of the most widely used tools for establishing connection between database and your Java program. It allows you to separate your connection data (e.g. username, password, connection url) from your code with use of configuration files in xml format. The line:

Connection c = DriverManager.getConnection("jdbc:mysql://mysql1.000webhost.com/mydatabase", "myusername", "mypassword");

Is a very dangerous way of establishing connetion, as you are providing confidential credentials inside the code. There are ways to retreive this information from binary files.

You also asked, if is it worth having some local version of your database. The anwser is: Yes. Having your database locally might significantly speed up the time required for development and testing. It also allows you to work on your code even when no internet connection is available.

Providing authentication with use of MAC address is a very dangerous idea. Those addresses are attached to given machines. In other words the user will be able to connect to your application only with machine, on which he or she created an account. When using other computer (e.g. laptop at work) authentication will be denied.

Arjan Tijms
  • 37,353
  • 12
  • 107
  • 135
Tinki
  • 1,398
  • 16
  • 30
  • Yeah I definitely need to do a lot of reading! I'll check those links and Hibernate tommorow :) To be honest, if I was developing a more professional program and/or had secure data stored, then obviously this would be a no-no, but since the only things that would be stored is their email and MAC address I wouldn't be too worried about security... – Josh M Jan 10 '15 at 19:30
  • But the reason I decided to use MAC addresses was exactly that...1 license for 1 computer. It's a relatively simple (and slightly niche) program anyway, so I don't really think anyone would care about whether they coudl use it on another device. – Josh M Jan 10 '15 at 19:32
  • In this case remember, that just in a few clicks MAC addesses can be changed or cloned to external devices (e.g. rooters). It means that this way of validation can be easily skipped when somebody has access to the computer with a valid licence. – Tinki Jan 12 '15 at 09:24