英文:
Java MySQL connection not working: No suitable driver found
问题
public static void main(String args[]) {
try {
Connection myConn = DriverManager.getConnection(
"jdbc:mysql://localhost/a3_eindopdracht_2", "", "");
Statement myStm = myConn.createStatement();
ResultSet myRs = myStm.executeQuery("SELECT * FROM namen");
while (myRs.next()) {
System.out.println(myRs.getString("voornaam") + " " + myRs.getString("achternaam"));
}
} catch (Exception exc) {
exc.printStackTrace();
}
}
英文:
So I am trying to make a connection with JDBC using XAMPP, but it doesn't work, What am I doing wrong here?
public static void main(String args[]) {
try {
Connection myConn = DriverManager.getConnection(
"http://localhost/phpmyadmin/sql.php?db=a3_eindopdracht_2&table=namen&pos=0", "", "");
Statement myStm = myConn.createStatement();
ResultSet myRs = myStm.executeQuery("SELECT * FROM namen");
while (myRs.next()) {
System.out.println(myRs.getString("voornaam") + " " + myRs.getString("achternaam"));
}
} catch (Exception exc) {
exc.printStackTrace();
}
</details>
# 答案1
**得分**: 1
首先,你找不到 Driver。你必须通过调用 Drive 类来加载它们,就像这样:
```java
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// 无法找到 MySQL 的驱动程序
}
然后,你正在尝试使用 HTTP 协议连接到你的数据库。但是,数据库有它们自己的协议(默认情况下使用端口 3306),所以你必须使用类似这样的地址:
jdbc:mysql://myserver.com/schema
最后:不要忘记在 getConnection 方法的最后两个字段中添加用户名和密码。
英文:
Firstly, you don't find Driver. You have to load them, by calling the Drive class like that :
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// Cannot find driver for MySQL
}
Then, you are trying to connect to your databse with HTTP protocol. But, DB have they own (with port 3306 by default), so you have to use address like that:
jdbc:mysql://myserver.com/schema
Finally: don't forget to add username and password on last 2 fields of getConnection method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论