Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
public class MyClass {
public static void main(String[] args) throws Exception {
String url= "jdbc:mysql://localhost:3306/first/?useUnicode=yes&autoReconnect=true&useSSL=false";
String uname="root";
String pass="****";
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection(url,uname,pass);
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select name from sample_t where rollno=1");
rs.next();
String name1=rs.getString("name");
System.out.println(name1);
st.close();
con.close();
}
}
最佳答案
请检查您的 MySQL 服务器是否正在运行。
关于java - 将java代码连接到mysql时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58474582/