使用java从另一个系统访问mysql数据库 [英] Access mysql data base from another system using java

查看:75
本文介绍了使用java从另一个系统访问mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 java swing 中的接口.我们有四个系统与局域网相连.该接口用于从同一局域网中的其他系统访问数据库,我使用以下代码通过给出访问数据库ip 地址、数据库名称、表名,但我无法连接其他系统的数据库.我该怎么做?

 public void dbconnection() {字符串名称 = "";字符串端口 = "3306";String user = "systech";String pass = "systech";String dbname = "cascade_demo";字符串主机="192.168.1.61";尝试 {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname;System.out.println("网址:" + url);Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection con = DriverManager.getConnection(url, user, pass);String qry2 = "select * from item_master";语句 st = con.createStatement();结果集 rs = st.executeQuery(qry2);而(rs.next()){名称 = rs.getString(1);System.out.println("姓名:" + 姓名);}rs.close();st.close();关闭();} 捕获(异常 e){System.out.println("异常:" + e);}}

解决方案

使用下面的代码

public void dbconnection() {字符串名称 = "";字符串端口 = "3306";String user = "systech";String pass = "systech";String dbname = "cascade_demo";字符串主机="192.168.1.61";尝试 {String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname;Class.forName("com.mysql.jdbc.Driver").newInstance();Connection con = DriverManager.getConnection(url, user, pass);String qry2 = "select * from item_master";语句 st = con.createStatement();结果集 rs = st.executeQuery(qry2);而(rs.next()){System.out.println("名称:" + rs.getString(1));}rs.close();st.close();关闭();} 捕获(异常 e){System.out.println("异常:" + e);}}

另外,请确保包含用于连接的 jar 文件.您将在此处获得 jar 文件.

更新 1:

所以,你有一个

<块引用>

CommunicationsException:通信链接失败

我引用了 这个答案,其中还包含一个步骤MySQL+JDBC分步教程:

<块引用>

如果您收到 SQLException: Connection deniedConnection timed out 或 MySQL 特定的 CommunicationsException:通信链路失败,则表示数据库根本无法访问.这可能有以下一种或多种原因:

  1. JDBC URL 中的 IP 地址或主机名错误.
  2. 本地 DNS 服务器无法识别 JDBC URL 中的主机名.
  3. JDBC URL 中的端口号丢失或错误.
  4. 数据库服务器已关闭.
  5. 数据库服务器不接受 TCP/IP 连接.
  6. 数据库服务器的连接用完了.
  7. Java 和 DB 之间的某些东西正在阻止连接,例如防火墙或代理.

要解决其中一个,请遵循以下建议:

  1. 使用 ping 验证和测试它们.
  2. 刷新 DNS 或改用 JDBC URL 中的 IP 地址.
  3. 根据MySQL DB的my.cnf进行验证.
  4. 启动数据库.
  5. 验证是否在没有 --skip-networking 选项 的情况下启动了 mysqld.
  6. 重新启动数据库并相应地修复您的代码,使其在finally 中关闭连接.
  7. 禁用防火墙和/或配置防火墙/代理以允许/转发端口.

更新 2

  1. 如果您的系统是 Windows,请转到开始>>运行.
  2. 输入command.这将打开命令提示符.
  3. 输入ping 192.168.1.61"
  4. 您可能会收到以下格式的回复.

Ping 192.168.1.61 [192.168.1.61] 32 字节数据:

来自 192.168.1.61 的回复:bytes=32 time=101ms TTL=124

如果你没有得到上述格式的东西,那么你的 ip 为 192.168.1.61 的 MYSQL 服务器是不可达的.请您的团队先启动服务器.:(

如果您有 Linux 版本,请打开终端并按照第 3 步操作.

另请查看以下链接.这些可能会帮助你...

  1. http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

I am working on a interface in java swing.we have four system connected with a lan.the interface is for accessing the database from the other system in the same local area network i used the following code to access the database by giving the ip address,database name,tablename but i could not connect the other systems database.how can i do this?

 public void dbconnection() {

        String name = "";
        String port = "3306";
        String user = "systech";
        String pass = "systech";
        String dbname = "cascade_demo";
        String host="192.168.1.61";

        try {

            Class.forName("com.mysql.jdbc.Driver");

              String url = "jdbc:mysql://"+host+":"+  port + "/" + dbname;
            System.out.println("URL:" + url);
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection(url, user, pass);
            String qry2 = "select * from item_master";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(qry2);
            while (rs.next()) {

                name = rs.getString(1);
                System.out.println("Name:" + name);

            }


            rs.close();
            st.close();
            con.close();


        } catch (Exception e) {
            System.out.println("Exception:" + e);
        }
    }

解决方案

Use below code

public void dbconnection() {

    String name = "";
    String port = "3306";
    String user = "systech";
    String pass = "systech";
    String dbname = "cascade_demo";
    String host="192.168.1.61";

    try {
        String url = "jdbc:mysql://"+host+":"+  port + "/" + dbname;
        Class.forName("com.mysql.jdbc.Driver").newInstance ();
        Connection con = DriverManager.getConnection(url, user, pass);
        String qry2 = "select * from item_master";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(qry2);
        while (rs.next()) {
            System.out.println("Name:" + rs.getString(1));
        }

        rs.close();
        st.close();
        con.close();


    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
}

Also, make sure to include jar file for connecting. You will get jar file here.

Update 1:

So, you have a

CommunicationsException: Communications link failure

I'm quoting from this answer which also contains a step-by-step MySQL+JDBC tutorial:

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. DB server has run out of connections.
  7. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

To solve the one or the other, follow the following advices:

  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnf of MySQL DB.
  4. Start the DB.
  5. Verify if mysqld is started without the --skip-networking option.
  6. Restart the DB and fix your code accordingly that it closes connections in finally.
  7. Disable firewall and/or configure firewall/proxy to allow/forward the port.

Update 2

  1. If your system is Windows, go to Start>>Run.
  2. Type command. This will open command prompt.
  3. Type "ping 192.168.1.61"
  4. You might get reply in below format.

Pinging 192.168.1.61 [192.168.1.61] with 32 bytes of data:

Reply from 192.168.1.61: bytes=32 time=101ms TTL=124

If you don't get something in above format, then your MYSQL Server with ip 192.168.1.61 is NOT REACHABLE. Ask your team to start the server first. :(

If you have Linux version, open terminal and follow step 3.

Also check below link. Those might help you...

  1. http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

这篇关于使用java从另一个系统访问mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆