都是老虎啊的gravatar头像
都是老虎啊 2016-08-29 14:58:39

为什么本地WEB项目无法访问服务器Mysql数据库?

服务器操作系统为centos7,防火墙状态为关闭。本地 navicat for mysql可以连接上服务器的mysql数据库,但是本地web项目无法访问数据库!求建议,该怎么去解决,还有那些因素影响?

所有回答列表(3)
最代码官方的gravatar头像
最代码官方  LV167 2016年8月29日

1.检测web应用中的连接配置

2.写个简单的Main类来测试下

package com.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Mysql {

	public static void main(String[] args) {
		Connection conn = null;
		try {
			String userName = "root";
			String password = "111111";
			String jdbcurl = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(jdbcurl, userName, password);
			String sql = "select id,name,status from test";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			String result = "";
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int status = rs.getInt("status");
				result += id + "\t" + name + "\t" + status + "\n";
			}
			System.out.println(result);
			pstmt.close();
		} catch (Exception e) {
			System.err.println("Cannot connect to database server,Exception:"
					+ e.getMessage());
		} finally {
			if (conn != null) {
				try {
					conn.close();
					conn = null;
				} catch (Exception e) { /* ignore close errors */
				}
			}
		}
	}

}  			
评论(2) 最佳答案
最代码温柔的gravatar头像
最代码温柔  LV18 2016年8月30日

本地工具能连,web项目连不了,拿肯定不是数据库权限问题了。看一下报的什么错,把连接数据库的账号密码,和地址,让web程序打出来。

都是老虎啊的gravatar头像
都是老虎啊  LV12 2016年8月30日

谢谢,问题解决了,项目能跑起来了

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友