日期:2014-05-20  浏览次数:20829 次

关于jdbc连接mysql数据库的问题 求解
Java code

package book.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public  class DBConnector {
    public static Connection getConnection (String driverClassName,String dbURL,String userName,String password)
    throws ClassNotFoundException,SQLException{
        Connection con=null;
        Class.forName(driverClassName);
        con=DriverManager.getConnection(dbURL,userName,password);
        return con;
    }
    public static Connection getMYSQLConnection(String driverClassName,String serverHost,String serverPort,String dbName,String userName,String password)
    throws ClassNotFoundException,SQLException{
        if (driverClassName==null){
            driverClassName="com.mysql.jdbc.Driver";
        }
        if(serverHost==null){
            serverHost="127.0.0.1";
        }
        if(serverPort==null){
            serverPort="3306";
        }
        String dbURL="jdbc:mysql://"+serverHost+":"+serverPort+"/"+dbName;
        return getConnection(driverClassName,dbURL,userName,password);
    }
    public static void main(String[] args)
    throws ClassNotFoundException,SQLException{
        String mySQLDirver ="com.mysql.jdbc.Driver";
        String dbName="books";
        String userName="root";
        String password="123456";
        Connection con=DBConnector.getMYSQLConnection(mySQLDirver,null,null,dbName,userName,password);
        System.out.println("连接mysql数据库成功!");
        con.close();
        System.out.println("成功关闭与mysql数据库的连接!");
        String url="jdbc:mysql://127.0.0.1:3306/"+dbName;
        con=DBConnector.getConnection(mySQLDirver,url,userName,password);
        System.out.println("连接mysql数据库成功!");
        con.close();
        System.out.println("成功关闭与mysql数据库的连接");
        }
}


只是书上的一个例子。。。照上面代码在eclipse上运行  
出现错误
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at book.database.DBConnector.getConnection(DBConnector.java:11)
at book.database.DBConnector.getMYSQLConnection(DBConnector.java:27)
at book.database.DBConnector.main(DBConnector.java:35)
这是eclipse环境没有弄好还是什么的。。我是菜鸟什么都不懂。。。

------解决方案--------------------
探讨
引用:

你没有把mysql的jar包导入项目

怎么导入的。。。

------解决方案--------------------
如果楼主用的是eclipse的话,把mysql的驱动包加到项目的环境属性中。选择项目名称右键选"Build Path"-"Configure Build Path"-"Libraries"-"Add External JARS"添加。添加成功后会在Referenced Libraries中看到。经测试以下代码可以成功连接数据库,供楼主参考
Java code

package com.student.test3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnector
{
    public static Connection getConnection(String driverClassName,
            String dbURL, String userName, String password)
            throws ClassNotFoundException, SQLException
    {
        Connection con = null;
        Class.forName(driverClassName);
        con = DriverManager.getConnection(dbURL, userName, password);
        return con;
    }

    public static Connection getMYSQLConnection(String driverClassName,
            String serverHost, String serverPort, String dbName,
            String userName, String password) throws ClassNotFoundException,
            SQLException
    {
        if (driverClassName == null)
        {
            driverClassName = "com.mysql.jdbc.Driver";
        }
        if (serverHost == null)
        {
            serverHost = "127.0.0.1";
        }
        if (serverPort == null)
        {
            serverPort = "3306";
        }
        String dbURL = "jdbc:mysql://" + serverHost + ":" + serverPort + "/"
                + dbName;
        return getConnection(driverClassName, dbURL, userName, password);
    }

    public static void main(String[] args) throws ClassNotFoundException,
            SQLException
    {
        String mySQLDirver = "com.mysql.jdbc.Driver";//这里的驱动包里面一定要有这个Driver
        String dbName = "study_java";
        String userName = "root";
        String password = "mysqladmin";
        Connection con = DBConnector.getMYSQLConnection(mySQLDirver, null,
                null, dbName, userName, password);
        System.out.println("连接mysql数据库成功!");
        con.close();
        System.out.println("成功关闭与mysql数据库的连接!");
        String url = "jdbc:mysql://127.0.0.1:3306/" + dbName;
        con = DBConnector.getConnection(mySQLDirver, url, userName, password);
        System.out.println("连接mysql数据库成功!");
        con.close();
        System.out.println("成功关闭与mysql数据库的连接");
    }
}