当前位置: 技术问答>linux和unix
Linux下无法读取mysql驱动
来源: 互联网 发布时间:2016-07-21
本文导语: windows环境下编译好的代码如下: package po; import java.sql.*; public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { String url="jdbc:mysql://127.0.0.1:3306/metro?user...
windows环境下编译好的代码如下:
package po;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
String url="jdbc:mysql://127.0.0.1:3306/metro?user=root&password=root";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con;
try {
con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
String query = "select * from news";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
System.out.println("用户名"+rs.getString("title")+" "+"密码"+rs.getString("content"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
导出为jar包后在Linux下运行报如下错误:
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:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at po.Test.main(Test.java:9)
各位高手请指教小弟一下为什么?在线跪等?
package po;
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
String url="jdbc:mysql://127.0.0.1:3306/metro?user=root&password=root";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con;
try {
con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
String query = "select * from news";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
System.out.println("用户名"+rs.getString("title")+" "+"密码"+rs.getString("content"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
导出为jar包后在Linux下运行报如下错误:
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:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at po.Test.main(Test.java:9)
各位高手请指教小弟一下为什么?在线跪等?
|
你的jar包要将mysql的包解压缩完毕后包括进去,实际上意思就是Class.forName("com.mysql.jdbc.Driver").newInstance(); 这个应该在jar包里,而不是在其他地方