m0_70002184
2022-04-22 11:14
采纳率: 25%
浏览 48
已结题

eclipse中用jdbc连接MySQL运行发生错误

密码检查过没有错误,MySQL输入密码可以用但是在eclipse里面连接MySQL就出现错误,求解答!谢谢大家了!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConMysql {

public static void main(String[] args) {
    Connection conn=null;
    Statement stmt=null;
    ResultSet rs=null;
    String url="jdbc:mysql://localhost:3306/world?"+
    "useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
    String user="root";
    String password="12345";
    String sql="select * from country";
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn=DriverManager.getConnection(url,user,password);
        stmt=conn.createStatement();
        rs=stmt.executeQuery(sql);
        rs.last();
        System.out.println("共有"+rs.getRow()+"行记录:");
        rs.beforeFirst();
        int i=0;
        System.out.println("No"+""+"Code"+""+"Name");
        while(rs.next()) {
            i+=1;
            System.out.println(Integer.toString(i)+""+rs.getString(1)+""+rs.getString("name"));
        }
    }catch(ClassNotFoundException e) {
        System.out.println("加载驱动异常");
        e.printStackTrace();
    }catch(SQLException e) {
        System.out.println("数据库异常");
        e.printStackTrace();
    }finally {
        try {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(conn!=null)
                conn.close();
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }
}

}

img

img

img

img

img

4条回答 默认 最新

相关推荐 更多相似问题