0

i am trying to do java database connectivity in eclipse juno using eclipse but i am getting the following error comes

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.lang.NullPointerException

suggest me some solutions.......... this is my code :

package example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Connect {

public static Connection getConnection()
{
    String url="jdbc:mysql://localhost:3306/demo";
    String drive="com.mysql.jdbc.Driver";
    //String databse="demo";
    String user="root";
    String password="abc";
    Connection conn=null;
    try
    {
        Class.forName(drive);
        conn=DriverManager.getConnection(url, user, password);
    }
    catch (Exception e)
    {
        System.out.println(""+e);
    }
    return conn;
}
public static void main(String[] args)
{
    Connection conn=null;
    PreparedStatement pstmt=null;
    try
    {
        conn=getConnection();
        conn.setAutoCommit(false);
        pstmt=conn.prepareStatement("insert into testlongtele(address,name)values(?,?)");
        pstmt.setString(0, "NIRAV");
        pstmt.setString(1, "KAMANI");
        pstmt.executeUpdate();
        pstmt.close();
        conn.commit();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println(""+e);
    }
}

}
3
  • What does catch(Exception e) { System.out.println(""+e); } printed? Commented Mar 23, 2013 at 12:00
  • 1
    Do you have this jar file mysql-connector.jar set in your classpath? Commented Mar 23, 2013 at 12:01
  • Instead of System.out.println(""+e); call e.printStackTrace(); and show us the output... Commented Mar 23, 2013 at 12:01

3 Answers 3

2

From This link:

Possible Cause of this error is:

1) You don't have mysql-connector.jar in your Classpath. as stated earlier this jar file contains "com.mysql.jdbc.Driver" class it must be present in classpath in order to successful connection to mysql database. you can downlad mysql-connector.jar from mysql.com.

2) mysql-connector.jar is in your classpath but somehow your classpath is getting overridden. Classpath is tricky in Java and classpath specified in jar may override CLASSPATH path variable. See how classpath works in Java to understand this issue in detail.

3) mysql-connector.jar is in classpath but current user doesn't have read permission. This problem often happens in Unix or Linux operating system which has sophisticated file and directory permission based on user, group and owner level. just get the right permission and run your program again.

Sign up to request clarification or add additional context in comments.

5 Comments

i found solution by adding mysql connector.jar file in my project by right cliking on project and then go to build path and then add external archives project....now its working but can you tell me why i have to set classpath and how can i do it???? before some days i tried this code at that time it works good without setting classpath but when i am trying yesterday it was giving this error so i think that there is no need to add classpath??? waiting for your reply????
when you add "com.mysql.jdbc.Driver" in your project , how can the program know the path of the jar ?? you didn't say when you put the jar , so we add the jar files in classpath , because it's the path where the java search , like if you add "t.txt" it mean that you add this text file in your classpath or currently path , or you will need to enter the full path if it not in currently path
i understood little but can you elaborate it because i am still not getting it!!!??And really thanks for your solutions
You are welcome any time :) , try to read this link:javarevisited.blogspot.com/2011/08/… , espasial "Examples of classnotfoundexception in java" , it will help you to understand the error and the soluton , and if you need to ask about anything , you are welcome :)
ok thanks for your solution i am getting it and one more thing that i want to ask is i am new to database connectivity options and all these classes like PREPAREDSTATEMENT,CONNECTION,STATEMENT,CLASS.FORNAME and many more so can you tell me from where i have to start for good understanding because i am having my first project in my education line and one more thing that is i know the core java and that is basics....waiting for your replay....
0

You can add:

class.forName(driver).newInstance();

Comments

0
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mindtree.kalinga.exception.ConnectionfailedException;
public class JdbcConnection {
    private static JdbcConnection jdbcConnection;
    private static Connection dbConnection;
    private JdbcConnection() {
    }
    public static JdbcConnection getInstance() {
        if (jdbcConnection == null)
            jdbcConnection = new JdbcConnection();
        return jdbcConnection;
    }
    public static void createConnection() throws ConnectionfailedException {
        try {
            dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mindtree", "root", "Welcome123");
        } catch (SQLException e) {
            throw new ConnectionfailedException("Error creating the connection to database", e);
        }
        System.out.println("Connection to db Established!");
    }
    public static Connection getConnection() {
        return dbConnection;
    }
    public static void closeConnection() throws ConnectionfailedException {
        try {
            dbConnection.close();
        } catch (SQLException e) {
            throw new ConnectionfailedException("connection  not closed properly", e);
        }
    }
}
--------------------------------------
Main
--------------------------------------
package com.mindtree.kalinga.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.mindtree.kalinga.entity.Mindtree;
import com.mindtree.kalinga.exception.ConnectionfailedException;
import com.mindtree.kalinga.service.Mindtreeservice;
import com.mindtree.kalinga.serviceimp.Mindtreeserviceimp;
import com.mindtree.kalinga.utility.JdbcConnection;
public class MindtreeMain {
    static Scanner in = new Scanner(System.in);
    static Mindtreeservice ms = new Mindtreeserviceimp();
    public static void main(String[] args) {
        try {
            JdbcConnection.createConnection();
        } catch (ConnectionfailedException e) {
            System.out.println("Not able to connect to database! Terminating application!");
            e.printStackTrace();
        }
        MindtreeMain mm = new MindtreeMain();
        int i = 1;
        while (i == 1) {
            System.out.println("1. to enter the detail\n" + "2. fetch the detail\n" + "3. to exit");
            int key = in.nextInt();
            switch (key) {
            case 1:
                Mindtree m = mm.createminds();
                break;
            case 2:
                List<Mindtree> ml = new ArrayList<>();
                mm.display(ml);
                break;
            case 3:
                i = 0;
                break;
            default:
                break;
            }
        }
    }
    private Mindtree createminds() {
        // TODO Auto-generated method stub
        System.out.println("enter the id of mind");
        int id = in.nextInt();
        in.nextLine();
        System.out.println("enter the name of mind");
        String name = in.nextLine();
        System.out.println("ente the address of minds");
        String address = in.nextLine();
        Mindtree m = new Mindtree(id, name, address);
        return m;
    }
    private void display(List<Mindtree> mind) {
        for (Mindtree i : mind) {
            System.out.println(i.getMid());
            System.out.println(i.getName());
            System.out.println(i.getAddress());
        }
    }
}
-------------------------------------------------
service
-------------------------------------------------
import java.util.List;
import com.mindtree.kalinga.entity.Mindtree;
public interface Mindtreeservice {
    public void insertmind(Mindtree m);
    public List<Mindtree> getAllminds();
}
---------------------------------------------
serviceimp
---------------------------------------------
package com.mindtree.kalinga.serviceimp;
import java.util.List;
import com.mindtree.kalinga.dao.Mindtreedao;
import com.mindtree.kalinga.daoimp.Mindtreedaoimp;
import com.mindtree.kalinga.entity.Mindtree;
import com.mindtree.kalinga.service.Mindtreeservice;
public class Mindtreeserviceimp implements Mindtreeservice {
    Mindtreedao md = new Mindtreedaoimp();
    @Override
    public void insertmind(Mindtree m) {
        // TODO Auto-generated method stub
        md.insertMindToDb(m);
    }
    @Override
    public List<Mindtree> getAllminds() {
        // TODO Auto-generated method stub
        return md.getAllMindFromDb();
    }
}
--------------------------------------------
dao
--------------------------------------------
import java.util.List;
import com.mindtree.kalinga.entity.Mindtree;
public interface Mindtreedao {
    public void insertMindToDb(Mindtree m);
    public List<Mindtree> getAllMindFromDb();
}
-------------------------------------------
daoimp
-------------------------------------------
package com.mindtree.kalinga.daoimp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mindtree.kalinga.dao.Mindtreedao;
import com.mindtree.kalinga.entity.Mindtree;
import com.mindtree.kalinga.utility.JdbcConnection;
import com.mysql.jdbc.Statement;
public class Mindtreedaoimp implements Mindtreedao {
    @Override
    public void insertMindToDb(Mindtree m) {
        Connection con = JdbcConnection.getConnection();
        String query = "insert into mindtree values(?,?,?);";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(query);
            ps.setInt(1, m.getMid());
            ps.setString(2, m.getName());
            ps.setString(3, m.getAddress());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }
    }
    @Override
    public List<Mindtree> getAllMindFromDb() {
        // TODO Auto-generated method stub
        List<Mindtree> mtl = new ArrayList<Mindtree>();
        Connection con = JdbcConnection.getConnection();
        String query = "select * from mindtree;";
        Statement st = null;
        ResultSet rs = null;
        try {
            st = (Statement) con.createStatement();
            rs = st.executeQuery(query);
            while (rs.next()) {
                Mindtree m = new Mindtree(rs.getInt(1), rs.getString(2), rs.getString(3));
                mtl.add(m);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mtl;
    }
}
-----------------------------------------------
exception
package com.mindtree.kalinga.exception;
public class ConnectionfailedException extends Exception {
    public ConnectionfailedException() {
        super();
        // TODO Auto-generated constructor stub
    }
    public ConnectionfailedException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
        super(arg0, arg1, arg2, arg3);
        // TODO Auto-generated constructor stub
    }
    public ConnectionfailedException(String arg0, Throwable arg1) {
        super(arg0, arg1);
        // TODO Auto-generated constructor stub
    }
    public ConnectionfailedException(String arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }
    public ConnectionfailedException(Throwable arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }
}
-----------------------------
entity
-----------------------------
package com.mindtree.kalinga.entity;
public class Mindtree {
    int Mid;
    String name;
    String address;
    public Mindtree(int mid, String name, String address) 
{

        Mid = mid;
        this.name = name;
        this.address = address;
    }
    public int getMid() {
        return Mid;
    }
    public void setMid(int mid) {
        Mid = mid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

1 Comment

Explanation please

Your Answer

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