Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Connecting to database on my website  (Read 1615 times)
0 Members and 1 Guest are viewing this topic.
Offline Swattkidd7

Junior Duke





« Posted 2008-02-18 18:18:19 »

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
1  
2  
3  
4  
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
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #1 - Posted 2008-02-18 18:46:02 »

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.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Swattkidd7

Junior Duke





« Reply #2 - Posted 2008-02-18 18:55:02 »

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

Here is the code.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline g666

Junior Duke





« Reply #3 - Posted 2008-02-18 19:40:54 »

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.

desperately seeking sanity
Offline Swattkidd7

Junior Duke





« Reply #4 - Posted 2008-02-18 19:53:51 »

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.
Offline ddyer
« Reply #5 - Posted 2008-02-18 19:59:26 »

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.
Offline Swattkidd7

Junior Duke





« Reply #6 - Posted 2008-02-18 20:19:05 »

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 Cheesy
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #7 - Posted 2008-02-18 21:56:46 »

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

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline ddyer
« Reply #8 - Posted 2008-02-19 01:17:10 »

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?


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.
Offline Swattkidd7

Junior Duke





« Reply #9 - Posted 2008-02-20 05:32:49 »

So what would be a good way to create a High score table for my game or something similar to that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #10 - Posted 2008-02-20 14:36:23 »

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.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!