how to access connection object in another class?


X

xodepp shrestha

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);
}
}
 
Ad

Advertisements

A

Arved Sandstrom

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);
}
}
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
 
J

jlp

Le 19/02/2013 09:18, xodepp shrestha a écrit :
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);
}
}
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) ...
 
M

markspace

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.
 
L

Lew

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/
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top