PDA

View Full Version : Java Console and MySQL


UrbanTwitch
09-23-2009, 01:08 AM
I think this is a correct code I made but It won't work.

this a console application which grabs the table members and displays the name.

import java.sql.*;

public class chub {

public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/crafthub", "root", "");

PreparedStatement statement = con.prepareStatement(
"select * from members");

ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(1));
}

}

}

I created this in Netbeans and used this tutorial: http://www.youtube.com/watch?v=Q6VZIyYUyQg
However, when I ran it in textPad.. i got the error:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driv
er
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at chub.main(chub.java:6)
Press any key to continue . . .

It worked in NetBeans but not TextPad...

help?

PS i am new to Java but not MySQL.

Fou-Lu
09-23-2009, 02:47 AM
Take a stabs here (old thread dug up):
http://www.codingforums.com/archive/index.php/t-113578.html

I assume you javac'd you're program when notepad was used? It will not configure the resources you will use, so you need to manually add it to the class path indicating that the com.mysq.jdbc.connector (been awhile, you know what I mean) will be used with this project and should be added to the 'classpath' (what you're java program can find).

Methinks thats the problem. Awesome, chub. You should call it chud instead (canabalistic humanoid underground dweller / contamination hazard urban disposal). That would rock.

UrbanTwitch
09-23-2009, 03:20 AM
So.. I am guessing I have to import a class or something?

Fou-Lu
09-23-2009, 03:28 AM
Nope, the import is there. You need to add a classpath when you're compiling:

> javac -cp /path/to/mysql-connector-java.jar;. chub.java

Something like that (its been a couple years since I've done cli compiling in java as well). IDE's take care of this for use when we choose to import a resource (not the same as importing the package).

I'm trying to recall if the .java is needed at the end, or if it just wants the classname. Meh, compile it with both to see the result. Obviously, change the path to you're path. Alternatively, you can alter you're enviornment CLASSPATH to add the path into it; javac can find it then.

UrbanTwitch
09-23-2009, 03:45 AM
I am using TextPad to compile my java... what should I do in this case?

PS - This looks right...
javac -cp C:/mysql-connector-java-5.1.9/mysql-connector-java-5.1.9-bin.jar;. chub.java
Right?

edit : didn't work.. :\ same error

Fou-Lu
09-23-2009, 04:15 AM
I am using TextPad to compile my java... what should I do in this case?

PS - This looks right...
javac -cp C:/mysql-connector-java-5.1.9/mysql-connector-java-5.1.9-bin.jar;. chub.java
Right?

Aww javac is not a recognized command.

PSS - I am using Windows XP.. I don't know if its a 32 or 64 bit. :\

Is TextPad an IDE? I assumed you meant notepad, so I shouldn't have made that assumption >.<
If its an IDE there should be a spot to add this in. If not, configure you're environment instead by adding the path to you're OS CLASSPATH.


For you're edit, try changing the path to use windows notation separators: \ instead of /
*crosses fingers....*

Also, found a link that should help more!
http://docs.cs.cf.ac.uk/html/602/node8.html

UrbanTwitch
09-23-2009, 05:24 AM
OK placing this in CMD
java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar dan

Nuttin'. Still same error.

here is my code in dan.java WHICH is placed in C:\ and I am running from THAT location.
package mysqlconnection;

import java.sql.*;

public class dan {

public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/crafthub", "root", "");

PreparedStatement statement = con.prepareStatement(
"select * from members");

ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(1));
}

}

}


Argh... :\

Fou-Lu
09-23-2009, 06:09 AM
o.O
Y'know, I'm going over this api, and I think its whiny because thats what the Class.forName is throwing. I'm looking here: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#forName(java.lang.String)
Note how the forName has a clause on the signature for throws ClassNotFoundException? That creates a checked exception, meaning you are required to try catch it, and the main you have just rethrows the exception (which is no problem if its say a constructor being called by another class, but it will prove to be problematic when it comes to the use of the main).

So, looks like you'll need to alter like so:

package mysqlconnection;

import java.sql.*;

public class dan {

public static void main(String[] args)throws Exception{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/crafthub", "root", "");

PreparedStatement statement = con.prepareStatement(
"select * from members");

ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(1));
}
}
catch (ClassNotFoundException cnfex)
{
System.out.println("Oops, looks like a problem loading the sql drivers...");
}
catch (SQLException sex) // Lol
{
System.out.println("Oops, SQL is unhappy...");
}
catch (Exception ex)
{
System.out.println("Exception tossed.");
}
}

}

Of course you can extract the particular message and code out of the exception object. Man, I can't believe I missed the checked exceptions. BTW, you'll still need to javac with the -cp to add the jar for the mysql driver.
Try that, otherwise we'll need to wait for one of the java masters.


Y'know what, this still won't fix this problem. Since you're main is still passing up the stack, it will compile and on top of that the exception is definitely whining about the Class.forName. I'll keep digging (but I'm off work soon so I'll be awhile between that time :P)

UrbanTwitch
09-23-2009, 02:26 PM
Stupid school assignment <_<
What exactly do I type in CMD to compile with that connector jar?

jolly_nikki
09-23-2009, 06:16 PM
Compile the class first

javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar dan.java

You were trying to run the class file

java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar dan

the above command runs the class file . But you have to compile it by setting the classpath before running

UrbanTwitch
09-23-2009, 08:41 PM
What do you mean set the classpath? :S
Also, I tried the java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar dan and got the same error (posted above...)

:(

Fou-Lu
09-23-2009, 11:39 PM
Classpath has to be set for anything that the application cannot find (pretty much anything outside of the directory that its being run in, or the system path). So on windows, you need to both compile the application using the javac -cp /path/to/resource/files.jar;. MyClass.java and run with the java -cp /path/to/resource/files.jar;. MyClass. Note that running you should not provide it with either .java or .class, rather its just the class name (MyClass). Compilation requires the .java (I wasn't 100% certain earlier, but I tested it an it worked). Using java <ClassName> will not work unless javac was first used to compile the application.

BTW, did you have a chance to test it with the try/catch? I don't think it will actually solve the problem, but some of what I'm reading around the net indicates that the classNotFound exception seems to always be thrown if you don't try catch the Class.forName method. Interesting though, I thought it would throw a more relevant exception.

UrbanTwitch
09-23-2009, 11:43 PM
The code posted here: http://www.codingforums.com/showpost.php?p=868318&postcount=8 doesn't work. Same error.

Also..I am compiling it correctly...
javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar dan.java
see?

Fou-Lu
09-24-2009, 12:35 AM
You're missing the current directory from the classpath; I'm not 100% certain that will effect it though.
BTW, can you remove the checked exception from the main method and try recompiling and running it?

javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;. dan.java


Can you verify as well when you try running it next time that this:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driv
er

is still the message you're receiving?

UrbanTwitch
09-24-2009, 01:42 AM
What do you mean remove the checked exception?

I also I tried compiling your way and ran it and got this:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;.
dan.java

C:\>java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar da
n
Exception in thread "main" java.lang.NoClassDefFoundError: dan
Caused by: java.lang.ClassNotFoundException: dan
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: dan. Program will exit.

C:\>

Fou-Lu
09-24-2009, 02:22 AM
Does this throw the same exception (or mysql connection exception)?

java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;. dan


Checked exceptions are ones that have the throws keyword in its method signature. I've never done a throws on a main method before, so I'm curious if its the the vm complaining since there is nothing else thats catching the throw. I don't think thats the case; I would have expected that compilation would have caught this, but it can't hurt to try (there is nowhere to go above the main, so it doesn't matter if you remove the throws clause from the signature).

UrbanTwitch
09-24-2009, 02:50 AM
For the code you posted above this post.. I got the following error:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;.
dan
Exception in thread "main" java.lang.NoClassDefFoundError: dan (wrong name: mysq
lconnection/dan)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: dan. Program will exit.

C:\>

------

Ah... I hate this.. I think I'll save myself the trouble and take the F instead... :\

Fou-Lu
09-24-2009, 03:28 AM
For the code you posted above this post.. I got the following error:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;.
dan
Exception in thread "main" java.lang.NoClassDefFoundError: dan (wrong name: mysq
lconnection/dan)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: dan. Program will exit.

C:\>

------

Ah... I hate this.. I think I'll save myself the trouble and take the F instead... :\

Wait, did I miss the package? I didn't see one declared originally...

java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;. mysqlconnection.dan

That should be you're {packagename}.{classname}. Try that out.

BTW, I understand you're frustration. Even now, I use IDE's just so I can avoid doing this manually >.<. I'd rather write a manual makefile.

UrbanTwitch
09-24-2009, 03:52 AM
Nope java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;. mysqlconnection.dan

gave me ClassDef not found blah blah (same error)

It's OK, I'll just take the F and learn it when my prof teaches me.

Thanks for all your help.

Fou-Lu
09-24-2009, 05:33 AM
I'm kinda at a loss here.
I pm'd ckeyrouz to take a look at this one - he definitely knows his java, so I hope he can be of more help than I.

ckeyrouz
09-24-2009, 05:48 AM
Hi guys.
I think the problem is not in the execution but in the compilation process.

the compilation should be like this:

javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar mysqlconnection\dan.java


and the execution should be like this:

java -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar mysqlconnection.dan


Note that the dan.java file should be inside a folder named mysqlconnection to respect the hierarchy of java.

UrbanTwitch
09-24-2009, 01:51 PM
OK ckey, the compile code you posted and I got this error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar
mysqlconnection\dan.java
javac: file not found: mysqlconnection\dan.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\>

Hm.. its in the folder C:\ and mysql connector folder. :S
Weird.

ckeyrouz
09-24-2009, 02:49 PM
Sorry about that, I posted my answer late at night and I forgot one important thing.
When compiling you need to assign the classpath, when executing you don't.

Let us brief everything to make sure all the steps are followed thoroughly:

You should have a folder named mysqlconnection under C and in it a file named dan.java

Then this is how you compile:
javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar mysqlconnection\dan.java

and this is how you execute:
java mysqlconnection.dan

No need for the -cp flag when executing.

The compiler has already added these information to the classpath of the JVM.

UrbanTwitch
09-24-2009, 08:27 PM
OK, we are getting there! :)
I got it compiled.
But when I tried to run it I got this error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar
mysqlconnection\dan.java
javac: file not found: mysqlconnection\dan.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\>javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar
mysqlconnection\dan.java

C:\>java mysqlconnection.dan
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driv
er
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at mysqlconnection.dan.main(dan.java:8)

C:\>

ckeyrouz
09-24-2009, 08:36 PM
try executing it like this:


set CLASSPATH=%CLASSPATH%;C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar;


then press enter after it execute the following line:
java mysqlconnection.dan

ckeyrouz
09-24-2009, 08:42 PM
If you are using a blank password, you will face a problem connecting to the database like this:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crafthub", "root", "");

You cannot pass a blank password like this.

you should pass the blank password like this:

Properties props = new Properties();
props.put("user", "root");
Connection con = DriverManager.getConnection(dbURL, props);


Not to mention that you have to do the following import:

import java.util.*;

UrbanTwitch
09-24-2009, 08:45 PM
Ok so replace this:
"jdbc:mysql://localhost:3306/crafthub", "root", "");
with this:
"jdbc:mysql://localhost:3306/crafthub", "root");

Then compile and run, right?

ckeyrouz
09-24-2009, 08:46 PM
Not at all.

Replace this:
"jdbc:mysql://localhost:3306/crafthub", "root", "");

with this:

Properties props = new Properties();
props.put("user", "root");
Connection con = DriverManager.getConnection(dbURL, props);


and at the top of the file add the following import:
import java.util.*;

then compile.

UrbanTwitch
09-24-2009, 08:49 PM
OK, I put in a password in the root account and tried it and got this error:
C:\>javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar
mysqlconnection\dan.java

C:\>set CLASSPATH=%CLASSPATH%;C:\mysql-connector-java-5.1.9\mysql-connector-java
-5.1.9-bin.jar;

C:\>java mysqlconnection.dan
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@
'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3996)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:284)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at mysqlconnection.dan.main(dan.java:10)

C:\>

my code
package mysqlconnection;

import java.sql.*;
import java.util.*;

public class dan {

public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
Properties props = new Properties();
props.put("user", "root");
Connection con = DriverManager.getConnection(dbURL, props);

PreparedStatement statement = con.prepareStatement(
"select * from members");

ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(2));
}

}

}

when i tried to compile that ^^^ i got this error:
C:\mysqlconnection\dan.java:11: ')' expected
Properties props = new Properties();
^
C:\mysqlconnection\dan.java:11: illegal start of expression
Properties props = new Properties();
^
C:\mysqlconnection\dan.java:11: ';' expected
Properties props = new Properties();
^
3 errors

ckeyrouz
09-24-2009, 08:53 PM
Let us brief some things:

Your code compiled.
Your code executed.
Error in db connection.


So the intial problems were solved.
Your only problem now is that you are not able to connect to the database with the current username and password.

The main problem for such error is an invalid password.
Make sure that you have put in your code the same password of the root account.

If you are sure that you have the same password, try restarting the mysql service.

UrbanTwitch
09-24-2009, 09:05 PM
1. My code didn't compile. hence the errors :P

ckeyrouz
09-24-2009, 09:09 PM
That is because you wrote it in a wrong way:
this is your corrected code.
package mysqlconnection;

import java.sql.*;
import java.util.*;

public class dan {

public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");

Properties props = new Properties();
props.put("user", "root");
Connection con = DriverManager.getConnection(dbURL, props);

PreparedStatement statement = con.prepareStatement(
"select * from members");

ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(2));
}

}

}

I removed this un-necessary line:
Connection con = DriverManager.getConnection(

UrbanTwitch
09-24-2009, 09:13 PM
Mkay, when compiling now. .I got this:

C:\mysqlconnection\dan.java:13: cannot find symbol
symbol : variable dbURL
location: class mysqlconnection.dan
Connection con = DriverManager.getConnection(dbURL, props);
^
1 error

ckeyrouz
09-24-2009, 09:15 PM
This is my mistake :D

replace this:
Connection con = DriverManager.getConnection(dbURL, props);


with this:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crafthub", props);

UrbanTwitch
09-24-2009, 09:19 PM
YES YES YES!!!

I knew that dbURL looked fishy. :P
I got it to work!

Now i need to know how it all works.
IF you want you can comment on how it works. Like step by step (line by line)
Well I know how MOST of it works. I just need help on understanding the packages and compiling of why it needs that special code.

Final code:
package mysqlconnection;

import java.sql.*;
import java.util.*;

public class dan {

public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");

Properties props = new Properties();
props.put("user", "root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crafthub", props);

PreparedStatement statement = con.prepareStatement(
"select * from members");

ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(2));
}

}

}

Output:
C:\>javac -cp C:\mysql-connector-java-5.1.9\mysql-connector-java-5.1.9-bin.jar
mysqlconnection\dan.java

C:\>java mysqlconnection.dan
Guard
aaa
C:\>

Thank you SOOOO much.

ckeyrouz
09-24-2009, 09:27 PM
With pleasure:
package mysqlconnection;

import java.sql.*;
import java.util.*;

public class dan
{
public static void main(String[] args)throws Exception
{
//returns an instance of the class com.mysql.jdbc.Driver that is used to connect to the database
Class.forName("com.mysql.jdbc.Driver");
//creating a property container in which we should put username and/or password
Properties props = new Properties();
props.put("user", "root");
//creating a connection using the location of the database and username and/or password
//and taking into consideration the instance of the class that was instantiated at the beginning of the function
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crafthub", props);

//Preparing a jdbc statement that will allow to execute the query
PreparedStatement statement = con.prepareStatement("select * from members");

//executing the query and putting the result into a ResultSet object that allows us to loop on the retrieved data
ResultSet result = statement.executeQuery();
//looping on the result set and outputting the result
while(result.next())
{
//result.getString(2) means we are getting the value of the second field of the table
// or the second field coming in the query.
System.out.println(result.getString(2));
}

}
}



Am glad you finally made it work.

If you have additional information you would like to acquire do not hesitate of posting your questions.

ckeyrouz
09-24-2009, 09:38 PM
Well I know how MOST of it works. I just need help on understanding the packages and compiling of why it needs that special code.

We had to use the properties because the mysql Driver class will check if the properties contains the value user only it will connect without a password to the databse.

But if we use this:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crafthub", "root","");

The Driver will consider that you are forcing the connection using username and password (even it is blank it will try to connect with it).

In fact the password is not blank, it does not exist at all.

The problem was between:
Connecting using password =YES and the value is blank.
Connection with use password=NO


Hope it is clear for you.

UrbanTwitch
09-24-2009, 09:43 PM
Aw great, Thanks so much.

Onto my next assignment...
Taking a random generated number and inserting into database. xD

At least I learned something. :)