package pokemon;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
getConnection();
}
public static Connection getConnection() throws Exception{
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306:Pokedex";
String username = "test";
String password = "password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return conn;
} catch(Exception e){System.out.println(e);}
return null;
}
}
我得到错误
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
由于底层异常,无法加载连接类:
'java.lang.NumberFormatException:对于输入字符串:“ 3306:Pokedex”。
我只是想看看我是否已连接到数据库,以便可以在表中输入数据。
最佳答案
您输入的网址错误。请用此行替换
String url = "jdbc:mysql://localhost:3306/Pokedex";
https://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html