JDBC无法通过连接池打印本地oracle数据,在用户名密码正确的情况下仍然报ora-01017

JDBC无法通过连接池打印本地oracle数据,在用户名密码正确的情况下仍然报ora-01017

开发环境是jdk 1.7,oracle 11g,使用原生jdbc连接本地数据库,不使用连接池能打印数据,但是使用连接池,在用户名密码绝对正确的情况下仍然报ora-01017

贴上使用连接池连接oracle的代码,包括三个文件,ConnectionSource.java,EmpDAO.java,db.properties

提问之前已经google,已经排除密码大小写字母被Oracle错误转换的问题(数字密码仍然报错),ojdbc6.jar包使用oracle 11g自带的仍然报错。不知道common-dbcp.jar或者common-pool.jar这两个包是否存在问题。

stackoverflow上跟我报错类似的两个问题链接,但是他们是把连接配在tomcat里面的,所以他们的解决方案对我无效
http://stackoverflow.com/ques...
http://stackoverflow.com/ques...

哪位大侠能帮我看一看问题出在哪儿,感激不尽

ConnectionSource.java

package com.java.oracle.pool;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class ConnectionSource {
    private static BasicDataSource dataSource = null;
    public ConnectionSource(){
        
    }
    public static void init(){
        Properties dbProps = new Properties();
        try{
            dbProps.load(ConnectionSource.class.getClassLoader().getResourceAsStream(
                    "com/java/oracle/pool/db.properties"));
        }catch(IOException e){
            e.printStackTrace();
        }
        try{
            String driveClassName = dbProps.getProperty("jdbc.driverClassName");
            String url = dbProps.getProperty("jdbc.url");
            String username = dbProps.getProperty("jdbc.username");
            String password = dbProps.getProperty("jdbc.password");
            
            String initialSize = dbProps.getProperty("dataSource.initialSize");
            String minIdle = dbProps.getProperty("dataSource.minIdle");
            String maxIdle = dbProps.getProperty("dataSource.maxIdle");
            String maxWait = dbProps.getProperty("dataSource.maxWait");
            String maxActive = dbProps.getProperty("dataSource.maxActive");
            
            dataSource = new BasicDataSource();
            dataSource.setDriverClassName(driveClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            
            //初始化连接数
            if(initialSize!=null){
                dataSource.setInitialSize(Integer.parseInt(initialSize));
            }
            //最小空闲连接
            if(minIdle!=null){
                dataSource.setMinIdle(Integer.parseInt(minIdle));
            }
            //最大空闲连接
            if(maxIdle!=null){
                dataSource.setMaxIdle(Integer.parseInt(maxIdle));
            }
            //超时回收时间
            if(maxWait!=null){
                dataSource.setMaxWait(Long.parseLong(maxWait));
            }
            //最大连接数
            if(maxActive!=null){
                if(!maxActive.trim().equals("0")){
                    dataSource.setMaxWait(Integer.parseInt(maxActive));
                }
            }
                
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("创建连接池失败!请检查设置");
        }
    }
    
    public static synchronized Connection getConnection() throws SQLException{
        if(dataSource == null){
            init();
        }
        Connection conn = null;
        if(dataSource!=null){
            conn = dataSource.getConnection();
        }
        return conn;
    }
}

EmpDAO.java

package com.java.oracle.pool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.java.oracle.pool.ConnectionSource;


public class EmpDAO {
    public static void main(String[] args){
        EmpDAO dao = new EmpDAO();
        dao.findAll();
    }
    
    public void findAll(){
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try{
            con = ConnectionSource.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery("select empno,ename,sal,hiredate from emp");
            while(rs.next()){
                System.out.println(rs.getInt("empno")+","+rs.getString("ename")+","+rs.getDouble("sal")+","
                        +rs.getDate("hiredate"));
            }
        }catch(SQLException e){
            System.out.println("数据库访问异常");
            throw new RuntimeException(e);
        }finally{
            try{
                if(rs!=null){
                    rs.close();
                }
                if(stmt!=null){
                    stmt.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch(SQLException e){
                System.out.println("释放连接资源时发生异常");
            }
        }
    }
}

db.properties

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.user=user
jdbc.password=282828

#<!--initialsize the connection-->
dataSource.initialSize=10
#<!--max free connection-->
dataSource.maxIdle=20
#<!--min free connection-->
dataSource.minIdle=5
#<!--max num of connection-->
dataSource.maxActive=50
#<!--time out wait-->
dataSource.maxWait=1000

附上console打印的报错信息

数据库访问异常
Exception in thread "main" java.lang.RuntimeException: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied
)
    at com.huawei.oracle.pool.EmpDAO.findAll(EmpDAO.java:32)
    at com.huawei.oracle.pool.EmpDAO.main(EmpDAO.java:14)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied
)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
    at com.huawei.oracle.pool.ConnectionSource.getConnection(ConnectionSource.java:76)
    at com.huawei.oracle.pool.EmpDAO.findAll(EmpDAO.java:23)
    ... 1 more
Caused by: java.sql.SQLException: ORA-01017: invalid username/password; logon denied

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:388)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:381)
    at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:564)
    at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:431)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:366)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:752)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:359)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
    at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
    at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
    at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
    ... 4 more
阅读 10.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏