尝试连接到允许远程连接的免费mysql数据库主机,但我收到此错误:
没有为jdbc找到合适的驱动程序:mysql://xx2-23-x1-2x1-172.compuxe-1.xmazonaws.com:3306
我使用下面的代码
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
}
catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}
try{
String host = "jdbc:mysql://xx2-23-x1-2x1-172.compuxe-1.xmazonaws.com:3306/xxxxx";
conn = DriverManager.getConnection(host,"xxxxx","xxxxx");
System.out.println("Connection Established");
}
提前致谢。
EDIT
堆栈跟踪如下:
java.sql.SQLException: No suitable driver found for jdbc:mysql://xx2-23-x1-2x1-172.compuxe-1.xmazonaws.com:3306 at java.sql.DriverManager.getConnection(DriverManager.java:604) at java.sql.DriverManager.getConnection(DriverManager.java:221) at DatabaseConnectivityModule.(DatabaseConnectivityModule.java:21) at UserFrontEnd.(UserFrontEnd.java:34) at UserFrontEnd$8.run(UserFrontEnd.java:399) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:671) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
答案 0 :(得分:-1)
您应该使代码看起来如下:
try {
Class.forName("com.mysql.jdbc.Driver");
String host = "jdbc:mysql://xx2-23-x1-2x1-172.compuxe-1.xmazonaws.com:3306/xxxxx";
conn = DriverManager.getConnection(host,"xxxxx","xxxxx");
} catch (Exception e) {
e.printStackTrace();
}
同时验证MySQL JDBC库是否在您的类路径中。该库可以从here下载。