【发布时间】:2010-12-31 12:13:28
【问题描述】:
/**
*
*/
package ORM;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author Gwilym
* @version 0.0
*/
public class DatabaseConnection {
private String userName="";
private String password="";
private String host="";
Connection conn;
/**
* @param userName
* @param password
* @param host
*/
public DatabaseConnection(String userName, String password, String host) {
this.userName = userName;
this.password = password;
this.host = host;
}
public DatabaseConnection(String userName, String password, String host,boolean autoConnect) {
this.userName = userName;
this.password = password;
this.host = host;
if (autoConnect)
{
try {
Connect();
} catch (DatabaseConnectionException e) {
e.printStackTrace();
}
}
}
/**
* @return the connection
*/
public Connection getConn() {
return conn;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* Connect, attempts to connect to the MySQL database
* with sun JDBC
* & MySQL driver
* @param none
* @return True iff connected;
* @return False for all else;
* @throws DatabaseConnectionException
*/
public boolean Connect() throws DatabaseConnectionException
{
// Attempt to load database driver
try
{
String url = "jdbc:mysql:"+host;
System.out.println(url);
//Load driver
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
}
catch (ClassNotFoundException cnfe) // driver not found
{
conn=null;
System.err.println ("Unable to load database driver");
throw new DatabaseConnectionException(cnfe);
}
catch(InstantiationException ie)
{
conn=null;
System.err.println ("Unable to Create database driver");
throw new DatabaseConnectionException(ie);
}
catch (IllegalAccessException iae)
{
conn=null;
System.err.println ("Unable to Create database driver");
throw new DatabaseConnectionException(iae);
} catch (SQLException sqle) {
conn=null;
System.err.println ("SQL error");
throw new DatabaseConnectionException(sqle);
}
if (conn!=null)
{
System.out.println ("Database connection established");
return true;
}
else
{
System.out.println ("Database connection Failed");
return false;
}
}
/**
* Disconnects the System from the mySQL database
*
* @param none
* @return true, if successful
* @return false if not connection in existance
*/
public boolean Disconnect()
{
if (conn != null)
{
try
{
conn.close ();
conn=null;
System.out.println ("Database connection terminated normally");
return true;
}
catch (Exception e) {
//Ignore these errors as they all result in conn.close anyway
}
finally
{
conn=null;
System.gc();
// my removing the refrance to conncetion all calling the Garbage collecter we insure it is destoryed.
}
System.out.println ("Database connection terminated with errors");
return true;
}
else
{
System.out.println ("No Database connection present");
return true;
}
}
}
以上代码由
调用DatabaseConnection db =new DatabaseConnection("USERNAME","PASSWORD","//tel2.dur.ac.uk:3306/dcs8s07_SEG",true);
出于显而易见的原因,我删除了用户名和密码,但可以认为它们是正确的。
就问题本身而言,我得到一个 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException,当此代码运行时,详细信息“最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序有没有收到来自服务器的任何数据包。”
我目前的主要问题是试图找出实际出了什么问题。
据我所知,驱动程序正在正确加载,因为我的代码没有抛出 ClassNotFoundException,而是某种 SQLException。
所以问题几乎可以肯定是某种方式的连接。我可以通过位于同一台服务器上的 phpMyadmin 连接和查询该数据库,因此我可以假设
1)服务器在线 2)mysql正在工作 3)用户名和密码正确 4) 数据库存在并且我的名称正确
从此和“驱动程序没有收到来自服务器的任何数据包。”我想知道 URL 是否格式错误?
URL= jdbc:mysql://tel2.dur.ac.uk:3306/dcs8s07_SEG
或者服务器上有一个不正确的简单设置不允许我连接?
我已经思考过这个问题并尝试了几次谷歌都无济于事,所以任何想法都会有很大帮助
提前致谢!
【问题讨论】:
标签: java mysql sql-server jdbc communication