Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[resolved] Newbie JDBC Connection Issue

 
Dan Byers
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is simple.

Why do you need to do a cast it to my.JobApplicationOrganiser.Connection?
 
Dan Byers
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were totally right Freddy - I only half read your post first time round because I'd just figured it out myself :O)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic