使用java从另一个系统访问mysql数据库 [英] Access mysql data base from another system using java
问题描述
我正在处理 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 denied
或 Connection timed out
或 MySQL 特定的 CommunicationsException:通信链路失败
,则表示数据库根本无法访问.这可能有以下一种或多种原因:
- JDBC URL 中的 IP 地址或主机名错误.
- 本地 DNS 服务器无法识别 JDBC URL 中的主机名.
- JDBC URL 中的端口号丢失或错误.
- 数据库服务器已关闭.
- 数据库服务器不接受 TCP/IP 连接.
- 数据库服务器的连接用完了.
- Java 和 DB 之间的某些东西正在阻止连接,例如防火墙或代理.
要解决其中一个,请遵循以下建议:
- 使用
ping
验证和测试它们. - 刷新 DNS 或改用 JDBC URL 中的 IP 地址.
- 根据MySQL DB的
my.cnf
进行验证. - 启动数据库.
- 验证是否在没有
--skip-networking 选项
的情况下启动了 mysqld. - 重新启动数据库并相应地修复您的代码,使其在
finally
中关闭连接. - 禁用防火墙和/或配置防火墙/代理以允许/转发端口.
更新 2
- 如果您的系统是 Windows,请转到开始>>运行.
- 输入
command
.这将打开命令提示符. - 输入ping 192.168.1.61"
- 您可能会收到以下格式的回复.
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 步操作.
另请查看以下链接.这些可能会帮助你...
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
orConnection timed out
or a MySQL specificCommunicationsException: Communications link failure
, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
- IP address or hostname in JDBC URL is wrong.
- Hostname in JDBC URL is not recognized by local DNS server.
- Port number is missing or wrong in JDBC URL.
- DB server is down.
- DB server doesn't accept TCP/IP connections.
- DB server has run out of connections.
- 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:
- Verify and test them with
ping
.- Refresh DNS or use IP address in JDBC URL instead.
- Verify it based on
my.cnf
of MySQL DB.- Start the DB.
- Verify if mysqld is started without the
--skip-networking option
.- Restart the DB and fix your code accordingly that it closes connections in
finally
.- Disable firewall and/or configure firewall/proxy to allow/forward the port.
Update 2
- If your system is Windows, go to Start>>Run.
- Type
command
. This will open command prompt. - Type "ping 192.168.1.61"
- 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...
这篇关于使用java从另一个系统访问mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!