java数据库连接错误与mysql

时间:2013-03-23 11:56:21

标签: java mysql

我正在尝试使用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);
    }
}

}

3 个答案:

答案 0 :(得分:2)

来自This link:

此错误的可能原因是:

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

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

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

答案 1 :(得分:0)

您可以添加:

class.forName(driver).newInstance();

答案 2 :(得分: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;
    }

}