java连接mySQL的问题,报错No suitable driver found for jdbc:mysql:
代码如下:importjava.sql.*;publicclasstest_mySQL{publicstaticvoidmain(String[]args)throwsS...
代码如下:
import java.sql.*;
public class test_mySQL {
public static void main(String[] args)
throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:..\\MySQL-5.0.90\\data\\mydb";
String userName = "root";
String password = "aptx4869";
Connection connection = DriverManager.getConnection(url,userName,password);
Statement statement= connection.createStatement();
String sql = "SELECT * FROM userinfo";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
String name = resultSet.getString(2);
String address = resultSet.getString(3);
System.out.println(name + " " + address);
System.out.println();
}
resultSet.close();
statement.close();
connection.close();
}
}
报错如下:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:..\MySQL-5.0.90\data\mydb
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test_mySQL.main(test_mySQL.java:9)
是不是驱动没导入对啊我的mySQL是5.0.90版本,用的mysql-connector-java5.0.8和5.1.7都下了,应该用哪个,怎么导入啊?还要不要设置环境变量啊? 展开
import java.sql.*;
public class test_mySQL {
public static void main(String[] args)
throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:..\\MySQL-5.0.90\\data\\mydb";
String userName = "root";
String password = "aptx4869";
Connection connection = DriverManager.getConnection(url,userName,password);
Statement statement= connection.createStatement();
String sql = "SELECT * FROM userinfo";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
String name = resultSet.getString(2);
String address = resultSet.getString(3);
System.out.println(name + " " + address);
System.out.println();
}
resultSet.close();
statement.close();
connection.close();
}
}
报错如下:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:..\MySQL-5.0.90\data\mydb
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test_mySQL.main(test_mySQL.java:9)
是不是驱动没导入对啊我的mySQL是5.0.90版本,用的mysql-connector-java5.0.8和5.1.7都下了,应该用哪个,怎么导入啊?还要不要设置环境变量啊? 展开
展开全部
String url = "jdbc:mysql:..\\MySQL-5.0.90\\data\\mydb"; 这里错了
格式应该是:
jdbc:mysql://MySQL服务器ip:端口(默认是3306)/数据库名";
如果哦不行试试下面:
com.mysql.jdbc.Driver
改为
org.gjt.mm.mysql.Driver
格式应该是:
jdbc:mysql://MySQL服务器ip:端口(默认是3306)/数据库名";
如果哦不行试试下面:
com.mysql.jdbc.Driver
改为
org.gjt.mm.mysql.Driver
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的URL不对,
正解:
连接字符串(url): jdbc:mysql://localhost:3306/数据库名字
正解:
连接字符串(url): jdbc:mysql://localhost:3306/数据库名字
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用5.0.8那个就行,直接放到lib包下就行了!
追问
我用的是eclipse,什么叫放到lib包下?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询