Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / August 2006

Tip: Looking for answers? Try searching our database.

Unable to establish Java connection to mySQL

Thread view: 
Richard Schulman - 18 Aug 2006 21:50 GMT
Java/mySQL veterans: Is there something wrong with the logon string in
the DriverManager.getConnection method below? I'm getting a
NullPointerException when I run the code below.

Here's the entire error message:

"Error location #2
SQL Exception: You have an error in your SQL syntax: check the manual
that corresponds to your MySQL server version for the right syntax to
use near '|' at line 1
SQLState: 42000
VendorError: 1064
Exception in thread 'main' java.lang.NullPointerException at
Test2.main(Test2.java:44)"

[Appended note: the statement at that line is
 stmt = conn.createStatement(); ]

Environment: MySQL database 5.0.21-community-nt, MySQL Client version
5.0.11, MS XP Home with Service Pack 2, mysql-connector-java-3.1.13,
Unicode database.

The classpath set before running the application is:

SET
CLASSPATH=.;c:\mysql-connector-java-3.1.13\mysql-connector-java-3.1.13-bin.jar

Here is the program. (It's not intended to do anything but
successfully access the database.):

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

public class Test2
{   

    public static void main(String[] args)
    {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try
        {
        // The newInstance() call is a work around for some
        // broken Java implementations
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
    catch (Exception ex)
        {
        System.out.println("Error location #1");
        System.out.println("SQLException: " +
ex.getMessage());
        }
   
    try
        {
        conn =
DriverManager.getConnection("jdbc:mysql:///mydbname?user=myusername&password=mypassword&useUnicode=true&characterEncoding=utf-8");
        // The above, unfortunately, is generating a
NullPointerException
        }
    catch (SQLException ex)
        {
        // handle any errors
        System.out.println("Error location #2");
        System.out.println("SQLException: " +
ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " +
ex.getErrorCode());
        }

    // assume that conn is an already created JDBC connection
    try
        {
        stmt = conn.createStatement();
        }
    catch (SQLException ex)
        {
        System.out.println("Error location #3");
        System.out.println("SQLException: " +
ex.getMessage());
        }
   
    try
        {
        rs = stmt.executeQuery("SELECT count(*) from poets");
        System.out.println("The query apparently executed");
        }
    catch (SQLException ex)
        {
        System.out.println("Error location #4");
        System.out.println("SQLException: " +
ex.getMessage());
        }
    /* or alternatively, if you don't know ahead of time that
      the query will be a SELECT...
         if (stmt.execute("SELECT count(*) from poets"))
        jdbc:mysql:///chinese?user=root&password=lqi5B   
        rs = stmt.getResultSet();
    Now do something with the ResultSet ....
    */
    finally
        {
        // it is a good idea to release  resources in a
finally{} block
        // in reverse-order of their creation if they are
no-longer needed
        if (rs != null)
            {
            try
                {
                rs.close();
                }
            catch (SQLException sqlEx)
                { // ignore }
                rs = null;
                }
            }
           
        if (stmt != null)
            {
            try
                {
                stmt.close();
                }
            catch (SQLException sqlEx)
                { // ignore }
                stmt = null;
                }
            }
        }
    }
}

Thanks in advance,
R. Schulman (To email me, remove 'xx' in reply-to)
Lothar Kimmeringer - 19 Aug 2006 00:27 GMT
> DriverManager.getConnection("jdbc:mysql:///mydbname?user=myusername&password=mypassword&useUnicode=true&characterEncoding=utf-8");
>         // The above, unfortunately, is generating a
> NullPointerException

Where's the server-ip? The URL should look like this:
jdbc:mysql://127.0.0.1:3306/mydbname
(if the database is running on the same system listening on port
3306.

Best regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Richard Schulman - 19 Aug 2006 04:21 GMT
Richard Schulman:
>>DriverManager.getConnection("jdbc:mysql:///mydbname?user=myusername&password=mypassword&useUnicode=true&characterEncoding=utf-8");
>>         // The above, unfortunately, is generating a
>> NullPointerException

Lothar Kimmeringer:
>Where's the server-ip? The URL should look like this:
>jdbc:mysql://127.0.0.1:3306/mydbname
>(if the database is running on the same system listening on port
>3306.

Thanks for your interest, Lothar.

Actually, the URL of 127.0.0.1:3306 doesn't need to be included, since
it is the default. But just to be sure, I inserted the URL and
recompiled: the same NullPointerException message occured.

Any other suggestions?

For those who may have missed my earlier post, here again is the
source code, environmental information, and error message at run time:

Environment: MySQL database 5.0.21-community-nt, MySQL Client version
5.0.11, MS XP Home with Service Pack 2, mysql-connector-java-3.1.13,
Unicode database.

The classpath set before running the application is:

SET
CLASSPATH=.;c:\mysql-connector-java-3.1.13\mysql-connector-java-3.1.13-bin.jar

Here is the program. (It's not intended to do anything but
successfully access the database.):

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

public class Test2
{   

    public static void main(String[] args)
    {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try
        {
        // The newInstance() call is a work around for some
        // broken Java implementations
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
    catch (Exception ex)
        {
        System.out.println("Error location #1");
        System.out.println("SQLException: " +
ex.getMessage());
        }
   
    try
        {
        conn =
DriverManager.getConnection("jdbc:mysql:///mydbname?user=myusername&password=mypassword&useUnicode=true&characterEncoding=utf-8");
        // The above, unfortunately, is generating a
NullPointerException
        }
    catch (SQLException ex)
        {
        // handle any errors
        System.out.println("Error location #2");
        System.out.println("SQLException: " +
ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " +
ex.getErrorCode());
        }

    // assume that conn is an already created JDBC connection
    try
        {
        stmt = conn.createStatement();
        }
    catch (SQLException ex)
        {
        System.out.println("Error location #3");
        System.out.println("SQLException: " +
ex.getMessage());
        }
   
    try
        {
        rs = stmt.executeQuery("SELECT count(*) from poets");
        System.out.println("The query apparently executed");
        }
    catch (SQLException ex)
        {
        System.out.println("Error location #4");
        System.out.println("SQLException: " +
ex.getMessage());
        }
    /* or alternatively, if you don't know ahead of time that
      the query will be a SELECT...
         if (stmt.execute("SELECT count(*) from poets"))
        jdbc:mysql:///chinese?user=root&password=lqi5B   
        rs = stmt.getResultSet();
    Now do something with the ResultSet ....
    */
    finally
        {
        // it is a good idea to release  resources in a
finally{} block
        // in reverse-order of their creation if they are
no-longer needed
        if (rs != null)
            {
            try
                {
                rs.close();
                }
            catch (SQLException sqlEx)
                { // ignore }
                rs = null;
                }
            }
           
        if (stmt != null)
            {
            try
                {
                stmt.close();
                }
            catch (SQLException sqlEx)
                { // ignore }
                stmt = null;
                }
            }
        }
    }
}

Richard Schulman (To email me, remove the 'xx' from the reply
address.)
IchBin - 19 Aug 2006 06:35 GMT
> Richard Schulman:
>>> DriverManager.getConnection("jdbc:mysql:///mydbname?user=myusername&password=mypassword&useUnicode=true&characterEncoding=utf-8");
[quoted text clipped - 140 lines]
> Richard Schulman (To email me, remove the 'xx' from the reply
> address.)

The connect statement I use look s like this:

 Connection con = DriverManager.getConnection(
                           "jdbc:mysql://localhost/targetDB",
               MYSQL_LOGINID,
                            MYSQL_LOGINPSWD
                            );

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA              http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
Richard Schulman - 20 Aug 2006 21:28 GMT
>  Connection con = DriverManager.getConnection(
>                            "jdbc:mysql://localhost/targetDB",
>               MYSQL_LOGINID,
>                             MYSQL_LOGINPSWD
>                             );

You must define those two variables somewhere: otherwise, all you'll
get is a compiler error -- unrecognized variables.
Richard Schulman (For email reply, please remove the antispamming 'xx' characters.)
IchBin - 21 Aug 2006 06:11 GMT
>>  Connection con = DriverManager.getConnection(
>>                            "jdbc:mysql://localhost/targetDB",
[quoted text clipped - 5 lines]
> get is a compiler error -- unrecognized variables.
> Richard Schulman (For email reply, please remove the antispamming 'xx' characters.)

That is correct. They are defined as CONSTANTS here. My assumption is:
that is what they would be recognized as...

--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA              http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
jcsnippets.atspace.com - 20 Aug 2006 22:21 GMT
<snipped problem>

How to connect to a MySQL database:
http://jcsnippets.atspace.com/java/database/connect-to-mysql-database.html

Best regards,

JayCee
Signature

http://jcsnippets.atspace.com/
a collection of source code, tips and tricks



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.