日期:2014-05-18  浏览次数:20700 次

找不到驱动
大家好!我写了一段连接数据库的程序,我现在想把它测试一下,可是又是抱ClassNotFoundException:com.jdbc.mysql.Driver.错误 

下面是我的测试程序 DBUtil.java 

package nm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class DBUtil 
{
static String driverName = "com.mysql.jdbc.Driver";
static String dbUrl = "jdbc:mysql://";
static String dbUser = "root";
static String dbPwd = "zgy01";

public DBUtil()
{

}

public static Connection connectToDB(String hostName,
String databaseName) throws Exception
{
String connName = dbUrl + hostName +":3306/" +databaseName;

Class.forName(driverName).newInstance();
Connection connection = DriverManager.getConnection(connName,
dbUser,dbPwd);
return connection;
}

public static Connection connectToDB(String databaseName)
throws Exception
{
return (connectToDB("localhost",databaseName));
}

public static Connection connectToDB()
throws Exception
{
return (connectToDB("localhost","buy"));
}

public static void main(String[] args)
{
try
{
String name = "com.jdbc.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";
Class.forName(name).newInstance();
Connection con = DriverManager.getConnection(url,"root","zgy01");
System.out.println("连接MySQL数据库成功!!!");

PreparedStatement pStmt = null;
ResultSet rs = null;

int id = 0;
String title = null;
String content = null;

String strSql = new String("select * from notice");

try
{
pStmt = con.prepareStatement(strSql,
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = pStmt.executeQuery();

if(rs.next())
{
id = Integer.parseInt(rs.getString("ID"));

title = rs.getString("title");
title = new String(title.getBytes("ISO-8859-1"),"GB2312");

content = rs.getString("content");
content = new String(content.getBytes("ISO-8859-1"),"GB2312");

System.out.println(id);
System.out.println(title);
System.out.println(content);
}
}
/*
catch(Exception ex)
{
ex.printStackTrace();
}
*/
finally
{
try
{
rs.close();
pStmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}



抱错信息 

java.lang.ClassNotFoundException: com.jdbc.mysql.Driver 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClassInternal(Unknown Source) 
at java.lang.Class