Java NetBeans8连接到HTTP mysql数据库



如何从Java Netbeans8连接http (Web Hosting) mysql数据库?我使用jdbc驱动程序

package javaswimingapplication;
public class JavaSwimingApplication {
    public static void main(String[] args) {
        DBConnect connect = new DBConnect();
        connect.getData();
    }
}

package javaswimingapplication;
import java.sql.*;
public class DBConnect {
    private Connection con;
    private Statement st;
    private ResultSet rs;
    public DBConnect(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://web server ip/database name","database user","password");
            st = con.createStatement();
        }catch(Exception ex){
            System.out.println("Error: "+ex);
        }
    }
    public void getData(){
        try{
            String query = "select * from persons";
            rs = st.executeQuery(query);
            System.out.println("Rexords from database");
            while(rs.next()){
                String name = rs.getString("name");
                String age = rs.getString("age");
                //String name = rs.getString("name");
                System.out.println("Name: "+name+"  "+"Age: "+age);
            }

        }catch(Exception ex){
            System.out.println(ex);
        }
    }
}

错误:

运行:

错误:com.mysql.jdbc.exceptions.jdbc4。CommunicationsException: Communications link failure

最后一个成功发送到服务器的数据包是0毫秒前。驱动程序没有收到来自服务器的任何数据包。java.lang.NullPointerExceptionСБОРКАУСПЕШНОЗАВЕРШЕНА(общеевремя:22секунды)

MySQL服务器是否侦听其外部ip或可能仅在本地主机或甚至本地socketfile上?

与web空间一起提供的大多数MySQL服务器都被配置为无法从外部网络访问

相关内容

  • 没有找到相关文章

最新更新