1

i am using xampp mysql, this code is for JDBC program. actually there are two class one is dbconnect.java and another is login.java. I want to access the connection object (i.e. conn) in another class(i.e. login.java). But i don't have proper idea, i have included the code here please suggest me what is the problem and what are the solutions?

This is the code of dbconnect.java

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 is the code from another class named login.java

if(source==login){
    if(username!=null && password!=null) {
        Connection conn= null;
        Statement stmt = null;
        dbconnect db = new dbconnect();
        db.conect();
        String query = "SELECT * from userlogin";
        try{
            stmt=(Statement) conn.createStatement(); // here is the problem
            ResultSet rs = stmt.executeQuery(query);  // here is the problem 
            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);
    }
}

What is the real problem and how to solve it?

11
  • 1
    Please format your code, this is unreadable. And follow Java conventions (classes are first uppercase, etc...) Commented Feb 19, 2013 at 15:00
  • @SotiriosDelimanolis if you read the code you would understand the design problem Commented Feb 19, 2013 at 15:01
  • 1
    @LuiggiMendoza if you can read anything in there, that is. Commented Feb 19, 2013 at 15:02
  • There's a lot to explain, but to start, change your conect function to return the Connection. Commented Feb 19, 2013 at 15:02
  • conn is null and throws null pointer exception Commented Feb 19, 2013 at 15:02

2 Answers 2

6

The easiest way would be to make the connect method non void and return the connection:

public Connection 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");
        }
        return con;
    } catch (Exception e) {
        System.out.println(e);
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should return your CONNECTION object from you connection class and assign it to your login class... Now your connection object is null...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.