连接数据库时遇到了如下问题:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

evoloyeu 2008-04-01 03:11:23
error:
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)
我用jsp调用时成功了,但是我用另外的应用程序调用时结果出了问题。
调用数据库连接的程序代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.net.*;
import java.io.*;
import java.util.*;
import DBConnector.*;
/**
*
* @author evol
*/
public class Receiver implements Runnable{
Connector conn;
//conn.connect();
public void setConnector(){}
public Connector getConnector(){return this.conn;}
public static void main(String args[]){
Receiver rec=new Receiver();
}
public Receiver(){
conn=new Connector();
conn.connect();
Thread thread=new Thread(this);
thread.start();
}
public void run(){
DatagramPacket receivePack=null;
DatagramSocket receiveSocket=null;
byte data[]=new byte[8192];
try{
receivePack=new DatagramPacket(data,data.length);
receiveSocket=new DatagramSocket(8888);
}catch(Exception e){e.printStackTrace();}
while(true){
if(receiveSocket==null)
break;
else{
try{
receiveSocket.receive(receivePack);
String ip=receivePack.getSocketAddress().toString();//get the client ip & port
String msg=new String(receivePack.getData(),0,receivePack.getLength());
System.out.println("ip:"+ip+"\nmsg:"+msg);
dataType(msg,ip);
}catch(Exception e){e.printStackTrace();}
}
}//while
}
public String getIP(String ip){
String IP;
int index=0;
for(index=0;index<ip.length();index++)
if(ip.charAt(index)==':'||ip.charAt(index)==':')
break;
IP=ip.substring(2, index);
return IP;
}
public void dataType(String msg,String ip){
//conn.connect();
char rMsgType;
if((!msg.equals(""))&&msg!=null){
rMsgType=msg.charAt(1);
switch(rMsgType){
case 'R':registerUser(msg,ip,conn);break;
case 'D':
case 'M':
case 'B':
String data[]=getData(msg);
double lat=Long.parseLong(data[1]);
double lng=Long.parseLong(data[2]);
float speed=Float.parseFloat(data[4]);
float degree=Float.parseFloat(data[5]);
//long dateTime=Date.parse(data[3]);
String updateString="insert into location(id,latitude,longitude,date,speed,degree)values("+data[0]+","+lat+","+lng+","+data[3]+","+speed+","+degree+")";
conn.executeUpdate(updateString);
break;
case 'S':break;
case 'L':break;
default:break;
}
}
}
public void registerUser(String msg,String ip,Connector conn){
String id=msg.substring(2);
String IP=getIP(ip);
conn.executeUpdate("insert into account(ip,id,pwd)values("+IP+","+id+"1234)");
}
public String[] getData(String msg){
String []data={"","","","","",""};
int index[]={0,0,0,0,0};
int k=0;
for(int i=0;i<msg.length();i++){
if(msg.charAt(i)==','||msg.charAt(i)==','){
index[k]=i;
k++;
}
}
data[0]=msg.substring(2,index[0]-1 );//id
data[1]=msg.substring(index[0]+1,index[1]-1);//latitude
data[2]=msg.substring(index[1]+1,index[2]-1);//longitude
data[3]=msg.substring(index[2]+1,index[3]-1 );//time
data[4]=msg.substring(index[3]+1,index[4]-1 );//speed
data[5]=msg.substring(index[4]+1);//degree
return data;
}
}
连接数哭的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package DBConnector;
import java.sql.*;
import java.io.*;
/**
*
* @author Administrator
*/
public class Connector {
//variables
Connection conn=null;
//constuctor
public Connector(){
}
//methods
public void connect(){
String url="jdbc:mysql://localhost/GPSUserInfo";
String driver="com.mysql.jdbc.Driver";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url, "root","root");
System.out.println("hey,I've got it.I got the DBLink.");
}catch(Exception e){
e.printStackTrace();
System.out.println("hello,evoloyeu,DB connection failed!");
}
}
public ResultSet executeQuery(String sql){
ResultSet rs=null;
try{
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
//conn.close();
}catch(SQLException e){e.printStackTrace();}
return rs;
}
public int executeUpdate(String sql){
int result=0;
try{
Statement stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
//conn.close();
}catch(SQLException e){e.printStackTrace();}
return result;
}
}
...全文
648 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
SUBINGGUI 2011-04-14
  • 打赏
  • 举报
回复
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 没有mysql 的包啊,,,下载一个mysql.jar包 放在web/lib 下
evoloyeu 2008-04-01
  • 打赏
  • 举报
回复
我终于解决了,大家说的很有道理哈,谢谢大家的指点!
最终解决办法是:web就不用说了哈,上面已经说过了。在一般的java应用程序中,需要将驱动程序导入到应用程序的库里面。
牛肉干 2008-04-01
  • 打赏
  • 举报
回复
你放在你的项目中看看,用Add jar哪个加载到你的项目中
kokobox 2008-04-01
  • 打赏
  • 举报
回复
要放到WEB-INF/lib/下
waykim 2008-04-01
  • 打赏
  • 举报
回复
找不到DRIVER驱动
包没导好?
evoloyeu 2008-04-01
  • 打赏
  • 举报
回复
不好意思哈,我的驱动mysql-connector-java-5.1.6-bin.jar并且放到了apache的lib文件夹里了....
那些我都试过了,但还是不行呀....
button1314 2008-04-01
  • 打赏
  • 举报
回复
没有加载数据库驱动的包,将下载的包放在lib文件夹下
zjc_love 2008-04-01
  • 打赏
  • 举报
回复
使用JDBC/ODBC连接数据库的第一步 不就是要加载驱动吗?
你的包呢 ?
longshenls 2008-04-01
  • 打赏
  • 举报
回复
是不是没导进来啊
Liebestraum 2008-04-01
  • 打赏
  • 举报
回复
很明显,是缺少了com.mysql.jdbc.Driver这个包,是不是项目忘记加载了Jar包
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 helloqiner 的回复:]
运行程序时,需要引用 mysql 的 jar 包!
[/Quote]

需要下载比如mysql-connector-java-5.0.8-bin.jar的一个包
helloqiner 2008-04-01
  • 打赏
  • 举报
回复
运行程序时,需要引用 mysql 的 jar 包!
evoloyeu 2008-04-01
  • 打赏
  • 举报
回复
还有异常:
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 DBConnector.Connector.connect(Connector.java:24)
at Receiver.<init>(Receiver.java:23)
at Receiver.main(Receiver.java:19)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧