Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to access connection object in another class?

1,178 views
Skip to first unread message

xodepp shrestha

unread,
Feb 19, 2013, 3:18:52 AM2/19/13
to
Here is the source code.
This is the code to connect the database.

package stundentrecord;

import java.sql.Connection;
import java.sql.DriverManager;

public class dbconnect {

public void conect(){
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "studentRecord";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
if(con==null){
System.out.println("Connection cannot be established");
}

// con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

AND HERE I WANT TO USE connection OBJECT BUT IT IS SHOWING ERROR.WHAT TO DO ??

if(source==login){
if(username!=null && password!=null){

Connection conn= null;
Statement stmt = null;
dbconnect db = new dbconnect();


String query = "SELECT * from userlogin";
try{
stmt=(Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String user = rs.getString("username");
String pass=rs.getString("password");
System.out.println("Welcome "+user);
}
}catch(SQLException ex){
ex.getMessage();
}
StundentRecord SR = new StundentRecord();

}else{
JOptionPane.showMessageDialog(null,"Username or password field is empty","error !!",JOptionPane.ERROR_MESSAGE);
}
}

Arved Sandstrom

unread,
Feb 19, 2013, 3:48:25 AM2/19/13
to
Where is that second set of code, what class and package? Is it in the
same package, likely not. Do you have an import statement that refers to
the "dbconnect" class? If not, fully qualify it when you reference it.

Other notes:

1. I realize your 1st language is not English; nevertheless
professionalism (and respect for any language that you use) demands that
I point out a few typos - StudentRecord -> StudentRecord, conect() ->
connect().

2. CamelCase your class names, e.g. DbConnect.

3. Your 'url' + 'db' concatenation, *everything* joined is potentially
the JDBC URL, including username and password. Since you're hard-coding
the DB name anyway, just add it to the 'url' variable.

4. Your username and password validation: do you think that's sufficient
to see if either is null? Is an empty username OK?

5. If your code got multiple rows back from the 'userlogin' table it
wouldn't think that was a problem. Also, don't just copy code withoput
understanding it - just because you get a ResultSet doesn't mean you
have to also run a while() loop on it; if you expect one row and must
have one row, a while() loop would be clumsy and obscure that logic.

6. Don't - even in learning code - do that absolute noop with an
exception. Please.

AHS

jlp

unread,
Feb 19, 2013, 3:50:12 AM2/19/13
to
Le 19/02/2013 09:18, xodepp shrestha a �crit :
your method "conect" must return a Connection not void and you must call
Connection con=new new dbconnect().conect();

Note : To make your code more readable, you have also to learn how to
name Classes ( First letter in upper case), methods, attributes (First
letter in lower case) ...

--
Cordialement
Jean-Louis Pasturel

lipska the kat

unread,
Feb 19, 2013, 4:18:53 AM2/19/13
to
On 19/02/13 08:18, xodepp shrestha wrote:
> Here is the source code.
> This is the code to connect the database.
>
> package stundentrecord;
>
> import java.sql.Connection;
> import java.sql.DriverManager;
>

I think you probably need something like this

public class ConnectionManager{

public Connection getNewConnection() throws ...{
Connection conn = null;
//get your connection then return it
...
return conn;
}

public void closeConnection(connection conn) throws ...{
//manage the close connection process
...
}
}

now you can do as you wish e.g

class SomeClass{

public SomeType someMethod(){

//should really be a singleton but what the heck
ConnectionManager connections = new Connectionmanager();

Connection conn = connections.getNewConnection();
...
//use the connection
...
//close it
...
connections.closeConnection(conn);
...
// etc
}
}

HTH

lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun

markspace

unread,
Feb 19, 2013, 12:43:55 PM2/19/13
to
On 2/19/2013 12:18 AM, xodepp shrestha wrote:

> AND HERE I WANT TO USE connection OBJECT BUT IT IS SHOWING ERROR.WHAT TO DO ??
>

What we really need is the actual error message it is showing. Please
copy and paste the whole thing into your email and send it to this group.

The problem from our end is your code is incomplete, so we can't guess
what error you are seeing. I have an idea, but as Arved pointed out
there are actually a lot of questions that still need to be answered.

When your error message refers to line numbers, please point out in your
source code which lines those are. Since you have edited the code for
the email message, we can't guess which line numbers correspond to your
error message.


Lew

unread,
Feb 19, 2013, 2:39:09 PM2/19/13
to
markspace wrote:
> xodepp shrestha wrote:
>> AND HERE I WANT TO USE connection OBJECT BUT IT IS SHOWING ERROR.WHAT TO DO ??

No need to shout.

> What we really need is the actual error message it is showing. Please
> copy and paste the whole thing into your email and send it to this group.
>
> The problem from our end is your code is incomplete, so we can't guess
> what error you are seeing. I have an idea, but as Arved pointed out
> there are actually a lot of questions that still need to be answered.
>
> When your error message refers to line numbers, please point out in your
> source code which lines those are. Since you have edited the code for
> the email message, we can't guess which line numbers correspond to your
> error message.

Better yet,
http://sscce.org/

--
Lew

Roedy Green

unread,
Mar 1, 2013, 3:20:12 AM3/1/13
to
There is nothing magic about a Connection object.

See http://mindprod.com/jgloss/scope.html

--
Roedy Green Canadian Mind Products http://mindprod.com
One thing I love about having a website, is that when I complain about
something, I only have to do it once. It saves me endless hours of
grumbling.
0 new messages