我正在构建一个需要远程数据库连接的Java应用程序。我已经使用Eclipse为mysql导入了JDBC驱动程序,据我所知,所有内容都正确输入。服务器主机已启用我的IP进行更改,并且已确认用户名和密码。
public class Shop {
Connection connection = null;
String driverName = "com.mysql.jdbc.Driver";
String username = "username";
String password = "password";
String hostURL = "jdbc:mysql://IPAddress:Port/databaseName";
public Shop() {};
public boolean connectDB() {
try {
Class.forName(driverName);
connection = DriverManager.getConnection(hostURL,username,password);
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException : "+e.getMessage());
return false;
} catch (SQLException e) {
System.out.println(e.getErrorCode());
System.out.println(e.getMessage());
return false;
}
return true;
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Store");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800,600));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
Shop s = new Shop();
System.out.println("Connection : " + s.connectDB());
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}