JDBC连接远程云服务器上的MySQL

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ncomer/article/details/102614011

1 开放端口

云服务器开放连接数据库的端口(默认3306),若连接时出现连接超时等错误提示,可能就是此问题,具体步骤不在赘述。

2 解决MySQL不允许连接问题

若直接连接会出现类似……Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server的错误提示。
这是因为MySQL默认不允许其他主机访问数据库。
解决步骤:

  1. 登录远程服务器,输入mysql -u root -p,再输入密码,进入数据库。
  2. 输入use mysql
  3. 输入select user,host from user,查看host列,默认包含localhost
  4. 输入如下命令(两种方式):
方式一:允许用户root使用123456做为密码从任何主机连接到mysql服务器 
grant all privileges on *.* to 'root'@'%' identify by '123456' with grant option;
方式二:允许用户root从ip为192.168.12.35的主机连接到mysql服务器,并使用123456做为密码
grant all privileges on *.* to root@'192.168.12.35' identify by '123456' with grant option;
  1. 再输入flush privileges;重新加载一下mysql权限,这一步必须有。
  2. 再输入select user,host from user;查看host列新增加%或者ip

3 使用JDBC连接数据库

代码如下:

//Major.java
public class Major {
    private Integer majorId;
    private String majorName;
    private String majorType;
    private String majorIntroduction;

    public Integer getMajorId() {
        return majorId;
    }

    public void setMajorId(Integer majorId) {
        this.majorId = majorId;
    }

    public String getMajorName() {
        return majorName;
    }

    public void setMajorName(String majorName) {
        this.majorName = majorName;
    }

    public String getMajorType() {
        return majorType;
    }

    public void setMajorType(String majorType) {
        this.majorType = majorType;
    }

    public String getMajorIntroduction() {
        return majorIntroduction;
    }

    public void setMajorIntroduction(String majorIntroduction) {
        this.majorIntroduction = majorIntroduction;
    }

    @Override
    public String toString() {
        return "Major{" +
                "majorId=" + majorId +
                ", majorName='" + majorName + '\'' +
                ", majorType='" + majorType + '\'' +
                ", majorIntroduction='" + majorIntroduction + '\'' +
                '}';
    }
}
//JDBCTest.java
public class JDBCTest {
    public static void main(String[] args) {
        String sql = "select * from major";
        ResultSet res = null;
        Statement statement = null;
        Connection con = null;
        String url = "jdbc:mysql://xxx.xxx.xxx.xxx:3306/desheng?characterEncoding=utf8&useSSL=false";
        String user = "root";
        String psw = "root";
        Major major = new Major();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url,user,psw);
            statement = con.createStatement();
            res = statement.executeQuery(sql);
            while (res.next()){
                major.setMajorId(res.getInt("majorId"));
                major.setMajorName(res.getString("majorName"));
                major.setMajorType(res.getString("majorType"));
                major.setMajorIntroduction(res.getString("majorIntroduction"));
                System.out.println(major.toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ncomer/article/details/102614011