I am new to java. My problem is
I have created a class for all the database connections.Now i want to access that class methods in another class.So i have created an object of that class. But when i am using that object to access the methods of database class it is showing the error 开发者_如何学运维that
"dbcon(object) cannot be resolved to a type"
I can't understand what is the issue.
Please help.
Make a valid reference.
The error you are getting is because your code which uses the class is not able to reference the class. If these classes are in different packages, you will need to add an import
line to the top of the class you're working in to import the dbcon
class.
You should also be aware that case matters. dbcon
is not the same as DBCon
. In Java, it is a standard that all class names should begin with uppercase letters and all variables with lowercase letters. So, the class dbcon
is at the very least named improperly. This standard helps to identify and resolve problems like the one you're having before they ever happen.
Finally, I would highly suggest that you work with some sort of IDE like Eclipse. This software can make your development process much easier by helping you solve common problems like these through suggested steps.
Heres a working reference example. Look at your code if it is close or similiar (apart from class and methodnames):
package com.example.util;
// imports if required
public class MyConnection {
// attribute storing the real connection
Connection conn = null;
// constructor to create a connection
public MyConnection() {
this.conn = magicMethodToCreateConnection(); // to be implemented
}
// getter method for that connection
public Connection getConnection() {
return conn;
}
}
package com.example.application;
import com.example.util.*;
// more imports if required
public class ConenctionUser {
public static useConnection() {
MyConnection myConn = new MyConnection();
Connection conn = myConn.getConnecion();
// now we can use conn
}
}
精彩评论