aspose file tools*
The moose likes JDBC and the fly likes [resolved] Newbie JDBC Connection Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "[resolved] Newbie JDBC Connection Issue" Watch "[resolved] Newbie JDBC Connection Issue" New topic
Author

[resolved] Newbie JDBC Connection Issue

Dan Byers
Greenhorn

Joined: Sep 16, 2008
Posts: 5
Hi guys, first post. I'm stuck.

I'm making a little app, and I'm having trouble connecting it to my DB. I have a GUI taking the inputs and passing them to this connection class (I realise a lot of this is superfluous, but I'll tidy up once I get things working).

I'm applying for jobs and want to practice in the meantime :O)

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package my.JobApplicationOrganiser;

import java.sql.DriverManager;


/**
*
* @author Dan
*/
public class Connection {

Connection conn = null;
String url = "";
String userName = "";
String password = "";

public void setURL(String s) {
url = "jdbc:mysql://" + s + "/jao";
System.out.println(url);
}

public void setName(String s) {
userName = s;
System.out.println(userName);
}

public void setPass(String s) {
password = s;
System.out.println(password);
}

public void connect(){
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance( );
System.out.println ("almost...");


System.out.println (url); // Checkig AGAIN it has the right stuff...
System.out.println (userName);
System.out.println (password);


conn = (Connection)DriverManager.getConnection(url, userName, password);

System.out.println ("Connected");

}
catch (Exception e)
{
System.err.println ("Cannot connect to server"+e);
}

}
}




The error message I get is Cannot connect to serverjava.lang.ClassCastException: com.mysql.jdbc.JDBC4Connection cannot be cast to my.JobApplicationOrganiser.Connection

I'm using netbeans, on vista32, mysql 5.0.51b. Netbeans is able to connect to the DB if I do it manually (through services), it can select and alter tables fine, just not with MY code.

Any ideas anyone? I can't really progress until I get this connection up. I've seen similar questions up, but none where the solution helps me.

Regards,
Dan
[ September 16, 2008: Message edited by: Dan Byers ]
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

The reason is simple.

Why do you need to do a cast it to my.JobApplicationOrganiser.Connection?


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Dan Byers
Greenhorn

Joined: Sep 16, 2008
Posts: 5
Cos I get this if I don't? :O)


Compiling 1 source file to C:\Projects\JobApplicationOrganiser\build\classes
C:\Projects\JobApplicationOrganiser\src\my\JobApplicationOrganiser\Connection.java:49: incompatible types
found : java.sql.Connection
required: my.JobApplicationOrganiser.Connection
conn = DriverManager.getConnection(url, userName, password);
1 error
BUILD FAILED (total time: 0 seconds)



With the cast there, as it was, I was able to get quite far - if I supplied the wrong username, it would halt me, and then again with the wrong password - so it's clearly communicating with the DB, just not happy with the connection.

Thanks for the input - what do you think I should have done? How should this have been arranged? I thought a separate class for connection stuff would make things clearer - is that the reason for my errors?
[ September 16, 2008: Message edited by: Dan Byers ]
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1132

Dan,
I suggest that you don't call your class "Connection" because it clashes with the interface "java.sql.Connection".
(It apparently managed to confuse the java compiler ;-)

Good Luck,
Avi.
Dan Byers
Greenhorn

Joined: Sep 16, 2008
Posts: 5
Thanks Avi, I hadn't considered that - after refactoring, however, I still get:

Cannot connect to serverjava.lang.ClassCastException: com.mysql.jdbc.JDBC4Connection cannot be cast to my.JobApplicationOrganiser.DBinfo
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

After some thorough investigation, before you refactored your code. You declared an instance variable called Connection.

Once you do that, you cam remove the cast in this line.


After you refactored you Connection class into DBInfo, I guess it should be easier to debug. Have fun and welcome to JavaRanch
Dan Byers
Greenhorn

Joined: Sep 16, 2008
Posts: 5
Thanks :O)


I figured it out, after hours of staring at the wall. It should have been:


instead of what I had - no cast needed. I think the renaming was sensible though, cheers for the help everyone :O)
Dan Byers
Greenhorn

Joined: Sep 16, 2008
Posts: 5
You were totally right Freddy - I only half read your post first time round because I'd just figured it out myself :O)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [resolved] Newbie JDBC Connection Issue