连接MYSQL数据库的问题。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import
java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.
UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import
java.sql.SQLException;
import java.sql.Statement;
public class NetClient {
private static int Port = 8088;
private String url= "jdbc:mysql://localhost:3306/meal ";
private String username= "root ";
private String password= "111 ";
private Connection conn=null;
private Statement statement=null;
private ResultSet result=null;
public NetClient()
{
ConnectData();
System.out.prinln( "connect success ");
}
public void ConnectData()
{
try { //下面这行就出错了。
Class.forName( "com.mysql.jdbc.Driver " );
conn = DriverManager.getConnection(
url,username,password );
} catch (
ClassNotFoundException e) {
// TODO 自动生成 catch 块
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。 " );
e.printStackTrace();
System.exit( 1 );
} catch (
SQLException e) {
// TODO 自动生成 catch 块
System.err.println( "无法连接数据库 " );
e.printStackTrace();
System.exit( 1 );
}
}
public static void main(String args[])
{
new NetClient();
}
}
报错:========================================================
装载 JDBC/ODBC 驱动程序失败。
java.lang.ClassNotFoundException: com.mysql.jdbc.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.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at NetClient.ConnectData(NetClient.java:55)
at NetClient. <init> (NetClient.java:43)
at NetClient.main(NetClient.java:219)
------解决方案--------------------顶,,,哪位高人指点一下啊。。谢谢
------解决方案--------------------导入驱动没?
------解决方案--------------------可能是没有导入mysql-connector-java-5.0.5-bin.jar驱动,应该把mysql-connector-java-5.0.5-bin.jar放到工程项目的lib文件夹中