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 / March 2005

Tip: Looking for answers? Try searching our database.

getconnection is making me so angry

Thread view: 
dodo1581 - 05 Mar 2005 01:17 GMT
Hi-

In the code below the forName method works but i cant get getConnection
to compile without errors.  I have tried everything except standing on
my head.

if i create a new server in mysql control center i can connect with
super user privlileges with root and no password.

any ideas why getConnection is zapping me like some 3rd evil force?

thank you,
jim

import java.sql.*;

public class testConnection {
   
    static final String JDBC_DRIVER ="com.mysql.jdbc.Driver";
    static  final String DATABASE_URL="jdbc:mysql://localhost/test";
   
   
   
   
    public static void main(String args[]) {
       
    Connection con=null;
    Statement stmt=null;
    System.out.println("what in the world am i doing wrong?");
   
    String query="Select password('boogity bop')";
   
   
try {
        Class.forName(JDBC_DRIVER);
       
        con = DriverManager.getConnection(DATABASE_URL,"root","");
       
               
Alex Molochnikov - 05 Mar 2005 01:51 GMT
This does not make any sense. You said that you cannot get the getConnection
compile without errors. Then you said that you can connect as root. In other
words, you managed to run the program - inspite the compiler errors?

If it does not compile, what is the compiler's complaint?
If it compiles, but does not connect other than as root, what is the
exception?

Either way, just saying "it does not work"... well... does not work.

> Hi-
>
[quoted text clipped - 29 lines]
>
> con = DriverManager.getConnection(DATABASE_URL,"root","");
dodo1548 - 05 Mar 2005 02:48 GMT
> This does not make any sense. You said that you cannot get the
> getConnection compile without errors. Then you said that you can
[quoted text clipped - 9 lines]
>> thank you,
>> jim

"if i create a new server in mysql control center i can connect with
super user privlileges with root and no password."

i said i can connect with the mysql control center as quoted above.
the compiler error doesnt tell me anything.  it is next to useless.
i included it though because maybe it means someone to someone besides
me.

no.  i cannot compile the program with the errors and get it to run.

thanks,
jim

Severity    Description    Resource    In Folder    Location    Creation
Time
2    Unhandled exception type SQLException    testConnection.java
    jdbcTest    line 37    March 4, 2005 8:14:20 PM
Alex Molochnikov - 05 Mar 2005 03:41 GMT
> Severity Description Resource In Folder Location Creation
> Time
> 2 Unhandled exception type SQLException testConnection.java
> jdbcTest line 37 March 4, 2005 8:14:20 PM

Well, the exception says it all: the getConnection() method must be enclosed
in the try-catch block. You code snippet shows the "try" part, but it is
incomplete. You need to have something like this:

try {
   Class.forName(JDBC_DRIVER);
   con = DriverManager.getConnection(DATABASE_URL,"root","");
}
catch (SQLException e) {
   // do something about the exception, e.g. print it out
}

If your code does indeed include the try-catch block, post it here.

AM
dodo1548 - 05 Mar 2005 19:57 GMT
hi-

for completeness i posted the entire code.
thanks for taking a look.

for some reason my newsreader xnews is not working so
i had to use outlook express.  this explains the delay between my posts.

it is greatly appreciated.

later
jim

import java.sql.*;

public class testConnection {

static final String JDBC_DRIVER ="com.mysql.jdbc.Driver";
static  final String DATABASE_URL="jdbc:mysql://localhost/test";

public static void main(String args[]) {

 Connection con=null;
 Statement stmt=null;
 System.out.println("what in the world am i doing wrong?");

String query="Select password('boogity bop')";

try {
 Class.forName(JDBC_DRIVER);

 con = DriverManager.getConnection(DATABASE_URL,"root","");
 stmt= con.createStatement();
 ResultSet rs=stmt.executeQuery(query);
      ResultSetMetaData metaData = rs.getMetaData();
      int numberOfColumns = metaData.getColumnCount();

   while (rs.next()){

   }
}

catch (ClassNotFoundException classNotFound)
  {
        classNotFound.printStackTrace();

          System.exit(1);

  }
         finally
         {
              try
              {
                  stmt.close();
                   con.close();
              }
                      catch (Exception exception)
                     {
                         exception.printStackTrace();
                         System.exit(1);
                    }

       }

   }

}
David Harper - 05 Mar 2005 20:39 GMT
> try {
>   Class.forName(JDBC_DRIVER);
[quoted text clipped - 11 lines]
>
> catch (ClassNotFoundException classNotFound)

You appear to be catching a ClassNotFoundException here, but not an
SQLException, which -- as Alex Molochniko has already pointed out -- is
what the compiler error is telling you:

> Unhandled exception type SQLException

You need to add another catch block, something like

catch (SQLException sqle) {
  sqle.printStackTrace();
  System.exit(1);
}

before your finally block.

Oh, and another word of advice. In your first post, you said:

> if i create a new server in mysql control center i can connect with
> super user privlileges with root and no password.

You really *don't* want to have a root user without a password. This is
very poor security. Anyone with access to your MySQL server can come
along and take it over. There are *bad* people on the Internet who will
sniff out your insecure MySQL server and make your life miserable.

Please, give your root user a secure password and don't create any MySQL
users without passwords.

David Harper
Cambridge, England
dodo1548 - 05 Mar 2005 21:12 GMT
thanks david and alex.
i made the change and added the catch block you suggested.
i got some descriptive error messages this time.
i will try and google late tonight when i get home and see what advice was
offered
when someone else came across one of those error messages.

i will change fix the root password tonight as well.
i will keep my computer off line until i fix it.

i have to head to work now though.

later,
jim
Alex Molochnikov - 05 Mar 2005 20:56 GMT
The other poster already responded to you. You are missing the catch clause
for SQLException. The compiler spelled it out for you. You could get away
with one "catch-all" generic Exception clause, like this:

catch (Exception e)
{
}

if you did not care to handle different error situations individually.

AM

> hi-
>
[quoted text clipped - 63 lines]
>
> }
dodo1548 - 06 Mar 2005 05:41 GMT
> The other poster already responded to you. You are missing the catch
> clause
[quoted text clipped - 8 lines]
>
> AM

Alex & David-
thanks again.  the catch clause really made a difference.  it helps so much
when i am trying to
diagnose the problem to have a hint as to what is going on.

my problem was basically that i needed to point to the .jar file for the
jdbc driver in the eclipse
build path.

that was my first test of any java program code except playing around with
the eclipse environment
while reading about it.

when i took some basic C++ classes the compiler always gave a decent
description if a line of code had a problem.
 i never went far enough in C++ to get into exception handling.  i can see
where it will be really important in
java.

i am looking forward to having lots of fun with java.

have a great weekend,
jim
Alex Molochnikov - 06 Mar 2005 07:15 GMT
> Alex & David-
> thanks again.  the catch clause really made a difference.  it helps so much
[quoted text clipped - 4 lines]
> jdbc driver in the eclipse
> build path.

Glad we could help. However, normally your build path in Eclipse does not
need to have the JDBC driver classes. They are only needed at runtime, in
the program classpath.

> when i took some basic C++ classes the compiler always gave a decent
> description if a line of code had a problem.

So does the Java compiler. In your case, you just did not realize that the
error message from the compiler had to be taken literally.

>   i never went far enough in C++ to get into exception handling.  i can see
> where it will be really important in
> java.

There is hardly a unit of code in Java that works without handling one
exception or another. You will find it out very quickly.

Anyway, good luck to you.

AM
dodo1548 - 06 Mar 2005 05:41 GMT
> The other poster already responded to you. You are missing the catch
> clause
[quoted text clipped - 8 lines]
>
> AM

hi david and alex-

thanks for all the help.

i needed to add the .jar file to the buildpath in eclipse.  the catch clause
really helped.

have a great weekend,
jim


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.