线程“ main”中的异常com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接。尝试重新连接3次。放弃。
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();
}
}
答案 0 :(得分:0)
请检查您的MySQL Server是否正在运行。
答案 1 :(得分:0)
检查您的MySQL连接是否正常,然后尝试如下更改网址:
String url= "jdbc:mysql://localhost/first/?useUnicode=yes&autoReconnect=true&useSSL=false";
答案 2 :(得分:0)
您可以尝试这种方法
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/first";
String user="root";
String pass="****";
con=DriverManager.getConnection(url,user,pass);
String query="select name from sample_t where rollno=?";
pstmt=con.prepareStatement(query);
System.out.println("Enter user id : ");
pstmt.setInt(1, sc.nextInt());
rs=pstmt.executeQuery();
//Process the result
if(rs.next())
{
System.out.println("Roll No. : " +rs.getInt(1));
}
//closing connections
sc.close();
pstmt.close();
rs.close();
con.close();
}
catch (Exception e)
{`
e.printStackTrace();
}