java、jdbc操作oracle数据库问题、急啊 求助啊!!
求助啊用户名密码没错sid没错pl/sql正常连接但是javajdbc连接就连接错误网上试了些方法无解、、谁能帮助下还有jdbc驱动也换了几个都不行下面是myeclips...
求助啊 用户名密码没错 sid没错 pl/sql正常连接 但是java jdbc连接就连接错误
网上试了些方法 无解、、谁能帮助下
还有 jdbc 驱动也换了 几个 都不行
下面是 myeclipse 控制台报错 (数据库是oracle 11G 系统windows7)
java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
大哥们帮帮忙吧、我都纠结死了! 展开
网上试了些方法 无解、、谁能帮助下
还有 jdbc 驱动也换了 几个 都不行
下面是 myeclipse 控制台报错 (数据库是oracle 11G 系统windows7)
java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
大哥们帮帮忙吧、我都纠结死了! 展开
4个回答
展开全部
Java用JDBC实现对Oracle数据库操作
import java.sql.Connection;
import java.sql.DriverManager;
public class UtilDbForOracle {
public static Connection getConnection(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@192.168.0.10:1521:ora920";
String username = "shihuan";
String password = "zznode";
Connection conn = DriverManager.getConnection(url, username, password);
// System.out.println(conn + "------return conn");
return conn;
}
catch (Exception e){
System.out.println(e.getMessage());
return null;
}
}
}
----------------------------------------------------------------------
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DBOperaRule {
private Connection conn = null;
// private Statement stmt = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private boolean hadErrors = false;
//创建数据库连接
public void createConnection(){
conn = UtilDbForOracle.getConnection();
}
//发起事务
public void beginTransaction(){
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行事务
public void commitTransaction() throws SQLException{
if(!hadErrors){
conn.commit();
}else{
conn.rollback();
hadErrors = false ;
}
hadErrors = false ;
conn.setAutoCommit(true);
}
//标记出错
public void errorOccur(){
hadErrors = true ;
}
//执行插入、删除和更新操作
synchronized public void execute(String sql) throws SQLException {
this.pstmt = conn.prepareStatement(sql);
if(pstmt != null){
pstmt.executeUpdate(sql);
}else{
Log log = LogFactory.getLog("mylog");
log.error("数据库插入数据出错");
}
}
//执行查询操作
synchronized public ResultSet read(String sql) throws SQLException {
this.pstmt = conn.prepareStatement(sql);
// Statement stmt = conn.createStatement();
if(pstmt != null){
// if(stmt != null){
ResultSet tmp = null ;
tmp = pstmt.executeQuery(sql);
// tmp = stmt.executeQuery(sql);
//下面为打印数据表中的字段名称
// ResultSetMetaData md = tmp.getMetaData();
// for(int i=1; i<=md.getColumnCount(); i++){
// System.out.println(md.getColumnName(i));
// }
//循环打印数据库中的值
// while(tmp.next()){
// System.out.println("tmp是否在最后一行:" + tmp.isLast());
// System.out.println("row: " + tmp.getRow());
// System.out.println(tmp.getString(1) + " --> " + tmp.getString(2));
// }
return tmp;
}else{
return null;
}
}
//执行查询个数操作
synchronized public int readCount(String sql) throws SQLException {
this.pstmt = conn.prepareStatement(sql);
int nCount = 0;
try{
if(pstmt != null){
ResultSet tmp = null;
tmp = pstmt.executeQuery(sql);
if(tmp != null && tmp.next()){
nCount = tmp.getInt(1);
}else{
nCount = 0;
}
}
}catch(SQLException e){
nCount = 0;
}
return nCount;
}
//创建数据库连接
/*
public boolean createConnection(){
try{
conn = UtilDbForOracle.getConnection();
conn.setAutoCommit(false);
}catch(SQLException e){
System.out.println("createConnectionError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
//进行数据库增,删除,更新操作
/*
public boolean executeUpdate(String sql){
if(conn == null){
createConnection();
}
try{
stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,操作影响的记录数:" + String.valueOf(iCount));
}catch(SQLException e){
System.out.println("executeUpdateError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
/*
public boolean executeUpdateBatch(List list){
if(conn == null) createConnection();
try{
stmt = conn.createStatement();
for(int i = 0; i < list.size(); ++i)
stmt.addBatch((String) list.get(i));
int i[] = stmt.executeBatch();
System.out.println("操作成功,操作影响的记录数:" + i.length);
}catch(SQLException e){
System.out.println("executeUpdateBatchError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
//进行数据库查询操作
/*
public ResultSet executeQuery(String sql){
if(conn == null) createConnection();
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException e){
System.out.println("executeQueryError!");
System.out.println(e.getMessage());
return null;
}
return rs;
}
*/
/*
public List executeQueryForList(String s) throws SQLException{
ResultSet rs = this.executeQuery(s);
List list = new ArrayList();
HashMap hash;
String tmp = null;
int cols = rs.getMetaData().getColumnCount();
while(rs.next()){
hash = new HashMap();
for(int i = 1; i <= cols; i++){
tmp = rs.getString(i);
if(tmp == null)
tmp = "";
hash.put(rs.getMetaData().getColumnName(i).toLowerCase(), tmp);
}
list.add(hash);
hash = null;
}
return list;
}
*/
/*
public HashMap executeQueryForMap(String s) throws SQLException{
ResultSet rs = this.executeQuery(s);
HashMap hash = new HashMap();
String tmp = null;
int cols = rs.getMetaData().getColumnCount();
while(rs.next()){
hash = new HashMap();
for(int i = 1; i <= cols; i++){
tmp = rs.getString(i);
if(tmp == null)
tmp = "";
hash.put(rs.getMetaData().getColumnName(i).toLowerCase(), tmp);
}
}
return hash;
}
*/
/*
public ResultSet executeQueryForPage(String sql){
if(conn == null) createConnection();
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}catch(SQLException e){
System.out.println("executeQueryError!");
System.out.println(e.getMessage());
return null;
}
return rs;
}
*/
//对数据库操作进行提交
/*
public boolean commit(){
try{
conn.commit();
}catch(SQLException e){
System.out.println("commitError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
//关闭数据库连接
public void closeDBConnection(){
if(conn != null){
try{
// stmt.close();
pstmt.close();
conn.close();
}catch(SQLException e){
System.out.println("closeDBConnectionError!");
e.printStackTrace();
}finally{
try{
// stmt.close();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
conn = null;
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
public class UtilDbForOracle {
public static Connection getConnection(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@192.168.0.10:1521:ora920";
String username = "shihuan";
String password = "zznode";
Connection conn = DriverManager.getConnection(url, username, password);
// System.out.println(conn + "------return conn");
return conn;
}
catch (Exception e){
System.out.println(e.getMessage());
return null;
}
}
}
----------------------------------------------------------------------
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DBOperaRule {
private Connection conn = null;
// private Statement stmt = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private boolean hadErrors = false;
//创建数据库连接
public void createConnection(){
conn = UtilDbForOracle.getConnection();
}
//发起事务
public void beginTransaction(){
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行事务
public void commitTransaction() throws SQLException{
if(!hadErrors){
conn.commit();
}else{
conn.rollback();
hadErrors = false ;
}
hadErrors = false ;
conn.setAutoCommit(true);
}
//标记出错
public void errorOccur(){
hadErrors = true ;
}
//执行插入、删除和更新操作
synchronized public void execute(String sql) throws SQLException {
this.pstmt = conn.prepareStatement(sql);
if(pstmt != null){
pstmt.executeUpdate(sql);
}else{
Log log = LogFactory.getLog("mylog");
log.error("数据库插入数据出错");
}
}
//执行查询操作
synchronized public ResultSet read(String sql) throws SQLException {
this.pstmt = conn.prepareStatement(sql);
// Statement stmt = conn.createStatement();
if(pstmt != null){
// if(stmt != null){
ResultSet tmp = null ;
tmp = pstmt.executeQuery(sql);
// tmp = stmt.executeQuery(sql);
//下面为打印数据表中的字段名称
// ResultSetMetaData md = tmp.getMetaData();
// for(int i=1; i<=md.getColumnCount(); i++){
// System.out.println(md.getColumnName(i));
// }
//循环打印数据库中的值
// while(tmp.next()){
// System.out.println("tmp是否在最后一行:" + tmp.isLast());
// System.out.println("row: " + tmp.getRow());
// System.out.println(tmp.getString(1) + " --> " + tmp.getString(2));
// }
return tmp;
}else{
return null;
}
}
//执行查询个数操作
synchronized public int readCount(String sql) throws SQLException {
this.pstmt = conn.prepareStatement(sql);
int nCount = 0;
try{
if(pstmt != null){
ResultSet tmp = null;
tmp = pstmt.executeQuery(sql);
if(tmp != null && tmp.next()){
nCount = tmp.getInt(1);
}else{
nCount = 0;
}
}
}catch(SQLException e){
nCount = 0;
}
return nCount;
}
//创建数据库连接
/*
public boolean createConnection(){
try{
conn = UtilDbForOracle.getConnection();
conn.setAutoCommit(false);
}catch(SQLException e){
System.out.println("createConnectionError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
//进行数据库增,删除,更新操作
/*
public boolean executeUpdate(String sql){
if(conn == null){
createConnection();
}
try{
stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,操作影响的记录数:" + String.valueOf(iCount));
}catch(SQLException e){
System.out.println("executeUpdateError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
/*
public boolean executeUpdateBatch(List list){
if(conn == null) createConnection();
try{
stmt = conn.createStatement();
for(int i = 0; i < list.size(); ++i)
stmt.addBatch((String) list.get(i));
int i[] = stmt.executeBatch();
System.out.println("操作成功,操作影响的记录数:" + i.length);
}catch(SQLException e){
System.out.println("executeUpdateBatchError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
//进行数据库查询操作
/*
public ResultSet executeQuery(String sql){
if(conn == null) createConnection();
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException e){
System.out.println("executeQueryError!");
System.out.println(e.getMessage());
return null;
}
return rs;
}
*/
/*
public List executeQueryForList(String s) throws SQLException{
ResultSet rs = this.executeQuery(s);
List list = new ArrayList();
HashMap hash;
String tmp = null;
int cols = rs.getMetaData().getColumnCount();
while(rs.next()){
hash = new HashMap();
for(int i = 1; i <= cols; i++){
tmp = rs.getString(i);
if(tmp == null)
tmp = "";
hash.put(rs.getMetaData().getColumnName(i).toLowerCase(), tmp);
}
list.add(hash);
hash = null;
}
return list;
}
*/
/*
public HashMap executeQueryForMap(String s) throws SQLException{
ResultSet rs = this.executeQuery(s);
HashMap hash = new HashMap();
String tmp = null;
int cols = rs.getMetaData().getColumnCount();
while(rs.next()){
hash = new HashMap();
for(int i = 1; i <= cols; i++){
tmp = rs.getString(i);
if(tmp == null)
tmp = "";
hash.put(rs.getMetaData().getColumnName(i).toLowerCase(), tmp);
}
}
return hash;
}
*/
/*
public ResultSet executeQueryForPage(String sql){
if(conn == null) createConnection();
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}catch(SQLException e){
System.out.println("executeQueryError!");
System.out.println(e.getMessage());
return null;
}
return rs;
}
*/
//对数据库操作进行提交
/*
public boolean commit(){
try{
conn.commit();
}catch(SQLException e){
System.out.println("commitError!");
System.out.println(e.getMessage());
return false;
}
return true;
}
*/
//关闭数据库连接
public void closeDBConnection(){
if(conn != null){
try{
// stmt.close();
pstmt.close();
conn.close();
}catch(SQLException e){
System.out.println("closeDBConnectionError!");
e.printStackTrace();
}finally{
try{
// stmt.close();
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
conn = null;
}
}
}
展开全部
请楼主注意一下几点
第一,服务是否开启(包括监听等)
第二,Oracle驱动包是否加入lib下
第三,检查服务名,用户名和密码是否正确
第四,查看oracle配置文件
修改listener.ora文件
原listener.ora文件:
# listener.ora Network Configuration File: D:/oracle/product/10.1.0/Db_4/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:/oracle/product/10.1.0/Db_4)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
)
修改后的文件:
---------------------------------------------------------------------------------------
# listener.ora Network Configuration File: D:/oracle/product/10.1.0/Db_4/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:/oracle/product/10.1.0/Db_4)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = D:/oracle/product/10.1.0/Db_4)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
)
第一,服务是否开启(包括监听等)
第二,Oracle驱动包是否加入lib下
第三,检查服务名,用户名和密码是否正确
第四,查看oracle配置文件
修改listener.ora文件
原listener.ora文件:
# listener.ora Network Configuration File: D:/oracle/product/10.1.0/Db_4/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:/oracle/product/10.1.0/Db_4)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
)
修改后的文件:
---------------------------------------------------------------------------------------
# listener.ora Network Configuration File: D:/oracle/product/10.1.0/Db_4/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:/oracle/product/10.1.0/Db_4)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = D:/oracle/product/10.1.0/Db_4)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接写: localhost:1521:orcl 试一下。
追问
不行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据库的实例名可能不是orcl
更多追问追答
追问
是 我查看了的
追答
那你的数据库有没有配置监听程序呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询