Java NetBeans8连接到HTTP mysql数据库

时间:2016-09-17 20:46:57

标签: java netbeans

如何从Java Netbeans8连接http(Web Hosting)mysql数据库? 我使用jdbc Driver。

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:通讯链接失败

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

1 个答案:

答案 0 :(得分:0)

MySQL服务器是在外部ip上侦听还是在localhost甚至本地socketfile上侦听?

随网络空间提供的大多数MySQL服务器都配置为无法从外部网络访问。