Results 1 to 13 of 13
  1. #1
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default question about MySQL and java connectivity

    Hi
    I am kind of new to Database but I am quite familiar with java, I want to do the following:
    I want to have my PC running a java application and that application should send data let us say "current time" to a MySQL database on a website, I have read about Java and MySQL but till now I didnot know what should I do on my CPANEL (i think some settings must be set correctly on the web site to enable it to receive the data), can you recommend a good books or tutorials regarding my issue.
    I have found this very basic code, and I think it serves me well but what to do with the CPANEL and where to write the user name and the password of the database:

    Java Code:
     1 import java.sql.*;
     2
     3 public class SimpleJdbc {
     4   public static void main(String[] args)
     5       throws SQLException, ClassNotFoundException {
     6     // Load the JDBC driver
     7     Class.forName("com.mysql.jdbc.Driver");
     8     System.out.println("Driver loaded");
     9
    10     // Establish a connection
    11     Connection connection = DriverManager.getConnection
    12       ("jdbc:mysql://mywebsite/test");
    13     System.out.println("Database connected");
    14
    15     // Create a statement
    16     Statement statement = connection.createStatement();
    17
    18     // Execute a statement
    19     ResultSet resultSet = statement.executeQuery
    20       ("select firstName, mi, lastName from Student where lastName "
    21         + " = 'Smith'");
    22
    23     // Iterate through the result and print the student names
    24     while (resultSet.next())
    25       System.out.println(resultSet.getString(1) + "\t" +
    26         resultSet.getString(2) + "\t" + resultSet.getString(3));
    27
    28     // Close the connection
    29     connection.close();
    30   }
    31 }

    Best Regards.
    Anderson.
    Last edited by mr_anderson; 07-12-2010 at 11:10 PM.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by mr_anderson View Post
    where to write the user name and the password of the database

    usually the name and password should be given in the connection. example:

    Java Code:
    Connection connection = DriverManager.getConnection
             ("jdbc:mysql://mywebsite/test", userID, passwd);

  3. #3
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    Thank you for your reply, the question now is what to do in the CPANEL ?
    i.e what settings should be made ?

    Regards.
    Anderson

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Not that I've ever actually done this for MySQL, but I think you need to tell it to allow connections from the IP address of your client machine. There should be stuff in the MySQL documentation for that, which is online...

  5. #5
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    But I want to make access to the Database from any pc, thatz why I am writing my program.
    Thank you for your reply.
    Regards.
    Anderson.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by mr_anderson View Post
    Thank you for your reply, the question now is what to do in the CPANEL ?
    i.e what settings should be made ?

    the CPanel should have two fields where the user can enter his UserId and passwd, then read the input and pass the entries to your userId and passwd variables and then instantiate the connection with these arguments.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    He's talking about the CPanel of the website his MySQL resides on.
    He needs to have the MySQL set up to accept connections from outside the web server.

    Which does remind me that a lot of hosts don't actually allow this.

  8. #8
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    I guess that is what is happening with me because I am getting this error Message from the server when I try to log in:

    jdbc:mysql://websiteurl.com:3306/
    java.sql.SQLException: Access denied for user 'myTESTusername'@'d58-110-200-85.mas9.nsw.optusnet.com.au' (using password: YES).
    when I try the same code on my local machine it works just fine but when I try it on my website i get this response.
    Thank you guys you were really helpful, I think I found the it under the name "Remote Database Access Hosts", in which I have to specify the IP of the remote machine which will be granted to access the Database, since I want to enable my software to connect to the DB from any PC, I will set address of the remote machine to wildcard:
    %.%.%.%
    I will try it and inform you.
    Regards.
    Anderson.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Isn't '*' the wildcard?

  10. #10
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    Not as I can see in my Cpanel :D, he denotes it with % instead of *
    Regards.
    Anderson.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Oh right.
    I always have to look these things up.

  12. #12
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    at the begining i was getting this code (before I made any configuration in the cpanel):

    jdbc:mysql://websiteurl.com:3306/
    java.sql.SQLException: Access denied for user 'myTESTusername'@'mywebsite' (using password: YES).

    After I set the address of the "Remote Database Access Hosts" in my cpanel to wild card I got this message:


    java.sql.SQLException: Unknown initial character set index '192' received from server. Initial client character set can be forced via the 'characterEncoding' property.
    at com.mysql.jdbc.Connection.configureClientCharacter Set(Connection.java:2633)
    at com.mysql.jdbc.Connection.initializePropsFromServe r(Connection.java:3239)
    at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:1803)
    at com.mysql.jdbc.Connection.<init>(Connection.java:4 50)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:411)
    at java.sql.DriverManager.getConnection(DriverManager .java:582)
    at java.sql.DriverManager.getConnection(DriverManager .java:207)
    at javaapplication4.Connect.main(Connect.java:29)

    Regards.
    Anderson.
    Last edited by mr_anderson; 07-16-2010 at 05:07 PM.

  13. #13
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    I found the solution :D.
    I searched it over google and I found that the solution was to change the character set to UTF-8 in the connection URL statement.
    Regards.

Similar Threads

  1. Connectivity issue-MySQL and Java
    By Pragya in forum JDBC
    Replies: 9
    Last Post: 01-27-2010, 05:28 PM
  2. Connectivity to jbpm through java
    By subrahmanyam.adapa in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-31-2009, 06:33 AM
  3. MySQL connectivity (hours wasted)
    By ribbs2521 in forum New To Java
    Replies: 13
    Last Post: 10-21-2009, 07:18 AM
  4. Java Database Connectivity
    By Java Tutorial in forum Java Tutorial
    Replies: 2
    Last Post: 02-08-2009, 02:15 AM
  5. java with database connectivity
    By thamizhisai in forum New To Java
    Replies: 2
    Last Post: 04-26-2008, 10:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •