[英]trying to connect to mysql database
I'm trying to connect to my database. 我正在尝试连接到我的数据库。 I added the MySQL connector driver jar by
我通过添加了MySQL连接器驱动程序jar
my code looks like this: 我的代码如下所示:
public static void main(String[] args) {
try {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "user", "pass");
Statement myStat = myConn.createStatement();
ResultSet myRs = myStat.executeQuery("Select * from city");
while (myRs.next()) {
System.out.println(myRs.getString("Name"));
}
}
catch (Exception exc) {
exc.printStackTrace();
}}
ERROR: 错误:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/world
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at javademo4.driver.main(driver.java:8)
I'm using Eclipse Luna and mysql-connector-java-5.0.8-bin.jar. 我正在使用Eclipse Luna和mysql-connector-java-5.0.8-bin.jar。 I also definitely have a database called world and a table called city which has a column called Name
我当然也有一个名为world的数据库和一个名为city的表,其中有一个名为Name的列
First of all you have to register your mysql driver, add this line above the Connection
line Class.forName("com.mysql.jdbc.Driver")
首先,您必须注册您的mysql驱动程序,在
Connection
行Class.forName("com.mysql.jdbc.Driver")
上方添加此行
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.