Skip navigation links

MySQL Forums :: Connector/JDBC and Java :: Unknown database error


Advanced Search

Unknown database error
Posted by: Greg Sherrod ()
Date: November 19, 2006 01:00PM

Hi JDBC Forum Members:

I can't get Connector\J to connect to mySql through local host. The
following code (please excues the lack of pretty printing, Phorum does not preserve indentation, apparently):

-------------------------------------------------
/** Try to connect to the mySql database using the DriverManager class.*/
import java.sql.*;

public class TestCon {
public static void main (String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception d) {
System.err.println("Did not get the com.mysql.jdbc.Driver class.\n");
}

Connection conn = null;
String url = "jdbc:mysql://localhost/sky:?user=guest&password=welcome";

try {
conn = DriverManager.getConnection(url);
System.out.println("Database connection established");
} catch (SQLException e) {
System.err.println("Cannot connect to database server because:\n" +
e.getMessage() + ",\nCause:\n" + e.getCause() +
"\nState:\n" + e.getSQLState() + ".");

// Perhaps it didn't like the URL. Try again.
String pw = "welcome";
String usr = "guest";
url = "jdbc:mysql://localhost/sky:";
try {
conn = DriverManager.getConnection(url, usr, pw);
System.out.println("Database connection made on the second try.");
} catch (SQLException f) {
System.err.println("Still didn't connect to the database server " +
"because:\n" + f.getMessage() + ",\nCause:\n" +
f.getCause() + "\nState:\n" + e.getSQLState() + ".");
}
}
}
}
-----------------------------------

Produces the following output:
Cannot connect to database server because:
Unknown database 'sky:',
Cause:
null
State:
42000.
Still didn't connect to the database server because:
Unknown database 'sky:',
Cause:
null
State:
42000.

FWIW, Perl has no problem. This code works:

------------------------------------

#! /usr/bin/perl -w
use strict;
use DBI;

my $server = 'localhost';
my $db = "sky";
my $usr = "guest";
my $pw = "welcome";
my $dbh = DBI->connect("dbi:mysql:$db:$server", $usr, $pw);

my $sql = "select * from brightStar " .
"where declination > -0.1 and declination < 0.1;";
my $c1 = $dbh->prepare($sql);
$c1->execute();

print "Bright stars between ", r2d(-0.6981), " and ", r2d(1.0472),
" declination.\n";
while (my $r = $c1->fetchrow_arrayref) {
if (defined($r)) {
print "$$r[0], ";
my $ra = r2r($$r[6]);
my $dc = r2d($$r[7]);
print "a:$ra, d:$dc, Mv:$$r[8], Sp:$$r[9] ";
if ($$r[1] !~ /None/) { print ", $$r[1]"; }
print "\n";
}
}
$c1->finish();
$dbh->disconnect;
-----------------------------------

Some other details about my setup:

Slackware 11.0,

Telnet has been enabled. (Perl worked without telnet, BTW).

My CLASSPATH:

/usr/local/jdk1.5.0_09
/usr/local/jdk1.5.0_09/bin
/usr/local/jdk1.5.0_09/include
/usr/local/jdk1.5.0_09/lib/mySql504.jar
/home/guest/work/javaDev/classes/

The mySql504.jar file is just mysql-connector-java-5.0.4-bin.jar, renamed.

I'm quite stumped by this, but I suspect that it's some Slackware/Java/MySql
configuration problem that is obvious to you, but totally opaque to me.

Thanks in Advance,

Greg

Options: ReplyQuote


Subject Written By Posted
Unknown database error Greg Sherrod 11/19/2006 01:00PM
Re: Unknown database error Bill Karwin 11/20/2006 02:33PM
Re: Unknown database error Greg Sherrod 11/20/2006 06:07PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.