Eclipse中连接MySQL的问题!
用Eclipse连接MySQL的问题。我安装了Eclipse3.2和MySQL 5.0和mysql-connector-java-5.0.4.zip。写了一个程序但是不好用:
package mypackage;
import java.sql.*;
public class DataConnection {
public static void main(String [] args)
{
String driver = "com.mysql.jdbc.Driver ";
String url = "jdbc:mysql://localhost/mydatabase ";
String user = "root ";
String password = "123123 ";
try
{
Class.forName(driver);
Connection con = DriverManager.getConnection(url,user,password);
String insert = "insert friends(id,name) values(?,?) ";
PreparedStatement prestatement = con.prepareStatement(insert);
String [] names = { "Rachel ", "Rose ", "Phoebe ", "Monica ", "Chandler ", "Joey "};
for(int i = 0;i < 6;i++ )
{
prestatement.setInt(1,i);
prestatement.setString(2,names[i]);
prestatement.execute();
}
String query = "select * from friends ";
ResultSet result = prestatement.executeQuery(query);
System.out.println( "The Results is: ");
while (result.next())
{
System.out.println(result.getString( "name "));
}
con.close();
}catch(SQLException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在下面显示以下问题,请教大家是什么问题?
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 mypackage.DataConnection.main(DataConnection.java:17)
[解决办法]
.zip--> .jar
[解决办法]
解压mysql-connector-java-5.0.4.zip,选择里面的mysql-connector-java-5.0.4-bin.jar,加入tomcat的lib,jdk的lib,jre\lib,然后全部设置环境变量为classpath应该就没问题了,前提是你的程序没错,我的是这样的,可以用
[解决办法]
4
[解决办法]
哥们,jdbc的lib包都没解压出来,你就用啊。明显下载时没看文档
[解决办法]
我也出现这个问题了,不过连过JAR包之后就好了。
但RUN 的时候又出现了下面的问题
如果run as -run on a server 时提示
"The slection did not contain anything that can be run on a server "
[解决办法]
各位回答的大虾们啊 ,你们可不可以说的再详细一点啊,我也碰到了这个问题,象你说的我都试过了,怎么还不好使呢?
[解决办法]
你程序报错是没能找到JAR包,加到你的LIB中就应该解决了(如果你用JDBC连接的话,要是用POOL连接要放到TOMCAT/COMMON/LIB中)用这个run as -run on a server你要先建立SERVER还有你建立的是什么样的工程?