AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unable to establish Java connection to mySQL

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
Richard Schulman
Guest





PostPosted: Sat Aug 19, 2006 1:50 am    Post subject: Unable to establish Java connection to mySQL Reply with quote



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)
Back to top
Lothar Kimmeringer
Guest





PostPosted: Sat Aug 19, 2006 4:27 am    Post subject: Re: Unable to establish Java connection to mySQL Reply with quote



Richard Schulman wrote:

Quote:
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
--
Lothar Kimmeringer E-Mail: spamfang (AT) kimmeringer (DOT) de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
Back to top
Richard Schulman
Guest





PostPosted: Sat Aug 19, 2006 7:10 am    Post subject: Re: Unable to establish Java connection to mySQL Reply with quote



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

Lothar Kimmeringer:
Quote:
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.)
Back to top
IchBin
Guest





PostPosted: Sat Aug 19, 2006 7:10 am    Post subject: Re: Unable to establish Java connection to mySQL Reply with quote

Richard Schulman wrote:
Quote:
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.)

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-)
Back to top
Richard Schulman
Guest





PostPosted: Mon Aug 21, 2006 1:28 am    Post subject: Re: Unable to establish Java connection to mySQL Reply with quote

On Sat, 19 Aug 2006 01:35:01 -0400, IchBin <weconsul (AT) ptd (DOT) net> wrote:

Quote:
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.)
Back to top
jcsnippets.atspace.com
Guest





PostPosted: Mon Aug 21, 2006 2:21 am    Post subject: Re: Unable to establish Java connection to mySQL Reply with quote

Richard Schulman <raschulmanxx (AT) verizon (DOT) net> wrote in
news:1t8ce296f4svafa028naa1easq4tl20i2f (AT) 4ax (DOT) com:

<snipped problem>

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

Best regards,

JayCee
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
Back to top
IchBin
Guest





PostPosted: Mon Aug 21, 2006 7:10 am    Post subject: Re: Unable to establish Java connection to mySQL Reply with quote

Richard Schulman wrote:
Quote:
On Sat, 19 Aug 2006 01:35:01 -0400, IchBin <weconsul (AT) ptd (DOT) net> wrote:

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.)

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-)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.