将java代码连接到mysql时遇到问题

将java代码连接到mysql时遇到问题,java,mysql,jdbc,database-connection,Java,Mysql,Jdbc,Database Connection,线程“main”com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException中出现异常:无法创建到数据库服务器的连接。尝试重新连接3次。放弃 公共类MyClass{ 公共静态void main(字符串[]args)引发异常{ String url=“jdbc:mysql://localhost:3306/first/?useUnicode=yes&autoReconnect=true&useSSL=false"; 字符串

线程“main”com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException中出现异常:无法创建到数据库服务器的连接。尝试重新连接3次。放弃

公共类MyClass{
公共静态void main(字符串[]args)引发异常{
String url=“jdbc:mysql://localhost:3306/first/?useUnicode=yes&autoReconnect=true&useSSL=false";
字符串uname=“root”;
字符串传递=“****”;
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(url、uname、pass);
语句st=con.createStatement();
结果集rs=st.executeQuery(“从样本中选择名称,其中rollno=1”);
rs.next();
字符串名称1=rs.getString(“名称”);
系统输出打印项次(名称1);
圣克洛斯();
con.close();
}
}

请检查您的MySQL服务器是否正在运行。

检查您的MySQL连接是否已启动,并尝试更改url,如下所示:

String url=“jdbc:mysql://localhost/first/?useUnicode=yes&autoReconnect=true&useSSL=false";
您可以尝试这种方法


请发布完整的异常堆栈跟踪
    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();
    }