0

我正在尝试使用 eclipse 在 eclipse juno 中进行 java 数据库连接,但出现以下错误

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

建议我一些解决方案............这是我的代码:

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

}
4

3 回答 3

2

这个链接:

此错误的可能原因是:

1) 你的 Classpath 中没有 mysql-connector.jar。如前所述,此 jar 文件包含“com.mysql.jdbc.Driver”类,它必须存在于类路径中才能成功连接到 mysql 数据库。您可以从 mysql.com 下载 mysql-connector.jar。

2) mysql-connector.jar 在你的类路径中,但不知何故你的类路径被覆盖了。Java 中的类路径很棘手,jar 中指定的类路径可能会覆盖 CLASSPATH 路径变量。查看类路径在 Java 中的工作原理以详细了解此问题。

3) mysql-connector.jar 在类路径中,但当前用户没有读取权限。这个问题经常发生在具有基于用户、组和所有者级别的复杂文件和目录权限的 Unix 或 Linux 操作系统中。只需获得正确的许可并再次运行您的程序。

于 2013-03-23T11:59:53.783 回答
0

你可以加:

class.forName(driver).newInstance();
于 2013-03-23T12:05:44.507 回答
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;
    }

}
于 2020-02-19T04:05:24.230 回答