2012-06-06 68 views
0

當我嘗試用java連接我的mysql數據庫和JDBC時,它不會去我的web服務器。JDBC去錯誤的地址

下面是該代碼應該去我的Web服務器的代碼

String dbtime; 
String dbUrl = "jdbc:mysql://184.172.176.18:3306/dcsoft_dcsoft_balloon"; 
String dbUser = "myuser"; 
String dcPass = "mypass"; 
String dbClass = "com.mysql.jdbc.Driver"; 
String query = "Select * FROM users"; 

try { 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con = DriverManager.getConnection(dbUrl, dbUser, dcPass); 
    Statement stmt = con.createStatement(); 
    ResultSet rs = stmt.executeQuery(query); 

    while (rs.next()) { 
     dbtime = rs.getString(1); 
     System.out.println(dbtime); 
    } 

    con.close(); 

} catch(ClassNotFoundException e) { 
    e.printStackTrace(); 
} catch(SQLException e) { 
    e.printStackTrace(); 
} 

,但它給這個錯誤 值java.sql.SQLException:拒絕訪問用戶dcsoft_dcsoft_java'@'jamesposse.force9。 (使用密碼:是)

jamesposse.force9.co.uk是不是地址我試圖連接到我試圖連接到184.172.176.18:3306。

謝謝。


HOSTS文件裏的內容是 -

# Copyright (c) 1993-2009 Microsoft Corp. 
# 
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows. 
# 
# This file contains the mappings of IP addresses to host names. Each 
# entry should be kept on an individual line. The IP address should 
# be placed in the first column followed by the corresponding host name. 
# The IP address and the host name should be separated by at least one 
# space. 
# 
# Additionally, comments (such as these) may be inserted on individual 
# lines or following the machine name denoted by a '#' symbol. 
# 
# For example: 
# 
#  102.54.94.97  rhino.acme.com   # source server 
#  38.25.63.10  x.acme.com    # x client host 

# localhost name resolution is handled within DNS itself. 
# 127.0.0.1  localhost 
# ::1    localhost 

和LMHOSTS是 -

# Copyright (c) 1993-1999 Microsoft Corp. 
# 
# This is a sample LMHOSTS file used by the Microsoft TCP/IP for Windows. 
# 
# This file contains the mappings of IP addresses to computernames 
# (NetBIOS) names. Each entry should be kept on an individual line. 
# The IP address should be placed in the first column followed by the 
# corresponding computername. The address and the computername 
# should be separated by at least one space or tab. The "#" character 
# is generally used to denote the start of a comment (see the exceptions 
# below). 
# 
# This file is compatible with Microsoft LAN Manager 2.x TCP/IP lmhosts 
# files and offers the following extensions: 
# 
#  #PRE 
#  #DOM:<domain> 
#  #INCLUDE <filename> 
#  #BEGIN_ALTERNATE 
#  #END_ALTERNATE 
#  \0xnn (non-printing character support) 
# 
# Following any entry in the file with the characters "#PRE" will cause 
# the entry to be preloaded into the name cache. By default, entries are 
# not preloaded, but are parsed only after dynamic name resolution fails. 
# 
# Following an entry with the "#DOM:<domain>" tag will associate the 
# entry with the domain specified by <domain>. This affects how the 
# browser and logon services behave in TCP/IP environments. To preload 
# the host name associated with #DOM entry, it is necessary to also add a 
# #PRE to the line. The <domain> is always preloaded although it will not 
# be shown when the name cache is viewed. 
# 
# Specifying "#INCLUDE <filename>" will force the RFC NetBIOS (NBT) 
# software to seek the specified <filename> and parse it as if it were 
# local. <filename> is generally a UNC-based name, allowing a 
# centralized lmhosts file to be maintained on a server. 
# It is ALWAYS necessary to provide a mapping for the IP address of the 
# server prior to the #INCLUDE. This mapping must use the #PRE directive. 
# In addtion the share "public" in the example below must be in the 
# LanManServer list of "NullSessionShares" in order for client machines to 
# be able to read the lmhosts file successfully. This key is under 
# \machine\system\currentcontrolset\services\lanmanserver\parameters\nullsessionshares 
# in the registry. Simply add "public" to the list found there. 
# 
# The #BEGIN_ and #END_ALTERNATE keywords allow multiple #INCLUDE 
# statements to be grouped together. Any single successful include 
# will cause the group to succeed. 
# 
# Finally, non-printing characters can be embedded in mappings by 
# first surrounding the NetBIOS name in quotations, then using the 
# \0xnn notation to specify a hex value for a non-printing character. 
# 
# The following example illustrates all of these extensions: 
# 
# 102.54.94.97  rhino   #PRE #DOM:networking #net group's DC 
# 102.54.94.102 "appname \0x14"     #special app server 
# 102.54.94.123 popular   #PRE    #source server 
# 102.54.94.117 localsrv   #PRE    #needed for the include 
# 
# #BEGIN_ALTERNATE 
# #INCLUDE \\localsrv\public\lmhosts 
# #INCLUDE \\rhino\public\lmhosts 
# #END_ALTERNATE 
# 
# In the above example, the "appname" server contains a special 
# character in its name, the "popular" and "localsrv" server names are 
# preloaded, and the "rhino" server name is specified so it can be used 
# to later #INCLUDE a centrally maintained lmhosts file if the "localsrv" 
# system is unavailable. 
# 
# Note that the whole file is parsed including comments on each lookup, 
# so keeping the number of comments to a minimum will improve performance. 
# Therefore it is not advisable to simply add lmhosts file entries onto the 
# end of this file. 
+0

什麼是你'等/ hosts'文件的內容是什麼? – npe

+0

主機和lmhosts.sam文件只有散列,因此主機文件是空的。 – DCSoft

+0

該目錄應該是C:\ Windows \ System32 \ drivers \ etc,對不對? – DCSoft

回答

4

jamesposse.force9.co.uk是您要連接的機器。

當MySQL抱怨Access denied for user 'dcsoft_dcsoft_java'@'jamesposse.force9.co.uk',這意味着用戶dcsoft_dcsof_java未被授權從主機jamesposse.force9.co.uk連接。

在MySQL中,您通過您嘗試連接的用戶名和您嘗試連接的主機名進行身份驗證。這就是爲什麼你授予訪問權限的用戶,如:

grant access to 'dcsoft_dcsoft_java'@'localhost' identified by 'whatever' 

該用戶只能在同一臺機器連接的MySQL服務器(例如,從託管在同一臺機器上的webapps,或當你以SSH到機器,並使用mysql從那裏的命令行)。

如果要授予對特定計算機的訪問權限,請通過主機名或IP地址對其進行標識。

grant access to 'dcsoft_dcsoft_java'@'184.172.176.18' identified by 'whatever' 

授予從任何地方訪問Internet上,使用'%'爲主機:

grant access to 'dcsoft_dcsoft_java'@'%' identified by 'whatever' 
+0

更好的答案! –

+0

感謝您的回答,但我不完全確定如何授予訪問權限,這是我會在cPanel中執行的操作。 – DCSoft

+0

@DCSoft:我從來沒有使用過cPanel,但我認爲它一定是可能的。 –

0

這可能是在RedHat密碼問題一個mysql。我不知道爲什麼它不使用的用戶名你雖然提供它,除非你不實際使用:使用 dcsoft_dcsoft_java 爲您的用戶名

String dbUser = "myuser"; 
String dcPass = "mypass"; 

和。 你可以提供一些關於你的環境的更多細節嗎? 在mysql中確保你有anyUserNameYourUsing @%否則,你會得到這個錯誤。您也可以使用IP地址和/或域名替換%,但它必須是數據庫允許訪問的每個域名。

+0

在發佈他的問題時,他可能在一個地方修改了用戶名,但忘記了其他用戶名。 –

+0

是的,我把它刪除在一個地方,但不是其他。 – DCSoft

+0

這是我的想法。但你永遠不知道。 –