Connecting to database on my website

Hey everyone, I wasnt sure where to put this so i thought this might be a good place, sorry if it is in the wrong section though.

Basically what im trying to do is connect to a database that is being hosted with my website (I created the database using cpanel), but now I am wondering how I could connect to it using java.

I have everything set up, bu i get an error " Exception: Access denied for user…"

Is there something you have to do when connecting to a database hosted somewhere besides localhost?

Here is what im doing to access my database right now

try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql://cksstudios.com:3306/cksstud_Test",
        "cksstud_root", "password");

I even tried replacing cksstudios.com with the IP its being hosted at but still no luck, aslo i am not sure which port its being hosted at, so that might be a problem.

Thanks for the help

Is there a security manager? (eg are you running your code as an applet?) If yes then it’s sandboxed. sign your applet, redirect a port on one server to the other, or move the applet to the same server are your options.

No, im not running the program from an applet or anything, its just a basic application made in netbeans.

Here is the code.


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

public class Test {

  public static void main(String args[]) {
    Connection con = null;

    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql://cksstudios.com:3306/dbname",
        "myname", "mypw");

      if(!con.isClosed())
        System.out.println("Successfully connected to " +
          "MySQL server using TCP/IP...");

    } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    } finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
    }
  }
}

Do you think I would have to run this console app from my website (where my server is located?) But how would i do that with a console app…?

I made it into an applet and put it on my server but i get the “Exception: com.mysql.jdbc.Driver” How do i include the driver into my server? I did upload the mysql-connector-java…jar into the same folder as the applet but that didnt work.

maybe your database isnt set up to accept outside connections? If you only have web hosting you might try communicating with the database via a serverside script.

Would there be anyway to check if my database can or cannot accept outside connections?

And please forget about my applet questions because I heard connecting to a database through an applet is generally a bad idea and plus, i want to do it through an application anyway. I might just fire up a local server and see if i can connect to that.

Even if you could set up your database to be accessed directly by a java applet, you should not. It’s too easy
to reverse engineer java, and use the information to hack your database from a rogue process. It’s also too
easy to let bugs creep into a java app - one “oops” in the code that writes queries can trash your entire database.

Instead, keep your database queries on the host, using whatever tightly controlled and extremely debugged
process you prefer. You can access the database from remote applications by standard CGI or whatever other
means you use to communicate with the applet. BUT don’t make the mistake of simply feeding database queries
in from outside. It’s really important that the database queries are constructed by a trusted agent.

I know what you mean about java applets being insecure thats why i just scratched the whole applet thing, but I am not quite sure of what you are saying after that. Are you saying I shouldnt access my database from an app either? or were you still referring to the applet? if not, are you saying I should just access my database with JSP or something?

Sorry for all the questions, im just a “little” confused :smiley:

About uploading jar(mysql) with respect to applet you need to list it explicitly. see here

but you dropped that idea, for good reason no doubt, but if it is because the simplest of reason; eg you can’t hide the username+ password in de code. This still applies to any other application you distribute: you should give out acounts on a per person basis.

Using a database directly
-auses a administrative problem of user accounts.
-can’t limit the users options (it can always directly connect to the database as the username and password is kown and run any sql)

If your application is not something you distribute you can ignore the above and post a stacktrace, it will be the quickest way to diagnose.

//edit
and that newInstance() is not nessasery just use Class.forName(“foo”);

Anything that runs on the client side ought to be regarded as untrusted. Untrusted sources shouldn’t be allowed any
direct access to your database, or be allowed to compose database queries or pieces of queries directly. It’s very
tempting to allow clients to specify a “where” clause more or less directly, for example. Don’t.

So what would be a good way to create a High score table for my game or something similar to that?

Wel reading the table the easiest way is to download a file and parse it.

Entering high scores is a different ball game. asserting that the score is legitimate that is. Since you can’t trust the client you need to assert that the highscore was achieved legitimately. Running the logic on the serverside gets you far, but is a somewhat big requirement. If your don’t a low profile game and you just want to compaire scores with your frends I wouldn’t bother taking that road.