数据库本身能登上去,如下是我在MyEclipse中写的连接数据库的代码,求高人指点为什么连接不上数据库。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;/**
* 该类负责处理数据库相关操作
* @author Administrator
*
*/public class DBUtils {
private static DBUtils dBUtils;
private final static String url = "jdbc:oracle:thin:@localhost:1521:oracle";
private final static String UserName = "scott";
private final static String PassWD = "tiger"; private DBUtils() {
try {
// 注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
throw new ServiceException("Can not for name", e);
}
} /**
* 获取数据库连接
* @return
*/
public static Connection getConnection() {
Connection conn = null;
if (dBUtils == null) {
dBUtils = new DBUtils();
}
try {
conn = DriverManager.getConnection(url, UserName, PassWD);
} catch (SQLException e) {
throw new ServiceException("Can not get connection", e);
}
return conn;
} /**
* 开启事务
* @param conn
*/
public static void beginTransaction(Connection conn) {
try {
// 取消自动提交
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new ServiceException("Can not begin transaction", e);
}
}
/**
* 提交事务
* @param conn
*/
public static void commit(Connection conn) {
try {
// 提交
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new ServiceException("Can not commit transaction", e);
}
}
/**
* 回滚事务
* @param conn
*/
public static void rollback(Connection conn) {
try {
// 回滚
conn.rollback();
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new ServiceException("Can not rollback transaction", e);
}
} /**
* 关闭数据库连接
* @param conn
*/
public static void closeConnection(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 关闭statement对象
* @param stmt
*/
public static void closeStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
throw new ServiceException("Can not close statement", e);
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;/**
* 该类负责处理数据库相关操作
* @author Administrator
*
*/public class DBUtils {
private static DBUtils dBUtils;
private final static String url = "jdbc:oracle:thin:@localhost:1521:oracle";
private final static String UserName = "scott";
private final static String PassWD = "tiger"; private DBUtils() {
try {
// 注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
throw new ServiceException("Can not for name", e);
}
} /**
* 获取数据库连接
* @return
*/
public static Connection getConnection() {
Connection conn = null;
if (dBUtils == null) {
dBUtils = new DBUtils();
}
try {
conn = DriverManager.getConnection(url, UserName, PassWD);
} catch (SQLException e) {
throw new ServiceException("Can not get connection", e);
}
return conn;
} /**
* 开启事务
* @param conn
*/
public static void beginTransaction(Connection conn) {
try {
// 取消自动提交
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new ServiceException("Can not begin transaction", e);
}
}
/**
* 提交事务
* @param conn
*/
public static void commit(Connection conn) {
try {
// 提交
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new ServiceException("Can not commit transaction", e);
}
}
/**
* 回滚事务
* @param conn
*/
public static void rollback(Connection conn) {
try {
// 回滚
conn.rollback();
conn.setAutoCommit(true);
} catch (SQLException e) {
throw new ServiceException("Can not rollback transaction", e);
}
} /**
* 关闭数据库连接
* @param conn
*/
public static void closeConnection(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 关闭statement对象
* @param stmt
*/
public static void closeStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
throw new ServiceException("Can not close statement", e);
}
}
}
oracle的端口号是否为1521?实例名是否为:oracle?其他的,我也看不出什么端倪来了