使用spring框架连接到数据库时出错

时间:2015-11-06 10:25:17

标签: java spring

我收到错误:

java.sql.SQLException: Invalid Oracle URL specified
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:445)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:428)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:190)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
    at com.Templates.DBOperation.DBOperationBean.fetchName(DBOperationBean.java:34)
    at com.Templates.DBOperation.DBOperationClient.main(DBOperationClient.java:29)

这是我的代码:

ApplicationContext.xml文件:

<beans>
 <bean id = "drds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name = "driverClassName" value = "oracle.jdbc.driver.OracleDriver"/>
        <property name = "url" value = "jdbc:oracle:thin@localhost:1521:orcl"/>
        <property name = "username" value = "templates"/>
        <property name = "password" value = "templates"/>
    </bean>

    <bean id = "dob" class="com.Templates.DBOperation.DBOperationBean">
        <property name = "ds" ref = "drds"/>
    </bean>
</beans>

Bean类:

public class DBOperationBean implements DBOperation {

    //Bean Property
    DataSource ds;

    //Setter method to support injection
    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    //implement bean methods
    @Override
    public String fetchName(String ename) {
        //To change body of generated methods, choose Tools | Templates.
        String name = null;
        try {
            //get one connection object from connection pool
            Connection con = ds.getConnection();
            //get emp name
            PreparedStatement pst = con.prepareStatement("select name from employee where empno = ?");
            int empno = 1;
            pst.setInt(1, empno);

            ResultSet rs = pst.executeQuery();

            if (rs.next()) {
                name = rs.getString("NAME");
            } else {
                name = "record not found";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return name;
    }

    @Override
    public int fetchSalary(int empNo) {
        int sal = 0;

        try {

            Connection con = ds.getConnection();
            PreparedStatement pst = con.prepareStatement("select sal from employee where sal =?");
            pst.setInt(1, sal);
            ResultSet rs = pst.executeQuery();
            while(rs.next())
            {
                sal = rs.getInt("SAL");
            }
        } catch (Exception e) {
        }
        return sal;
    }
}

客户端类:

public class DBOperationClient {
    public static void main(String[] args) {
        Resource res = new ClassPathResource("applicationContext.xml");

        //Activate bean factory container
        BeanFactory fac = new XmlBeanFactory(res);

        //get spring bean from container
        DBOperation beanObject = (DBOperation)fac.getBean("dob");

        //call bean methods
        System.out.println("Employee name is: " + beanObject.fetchName("NAME"));
        System.out.println("Employee salary is: " + beanObject.fetchSalary(1));
    }
}

我在stackoverflow上经历了其他类似的问题,并修改了无法正常工作的网址。网址看起来没问题,但我仍然得到同样的错误:指定的Oracle URL无效

1 个答案:

答案 0 :(得分:5)

在&#34;瘦&#34;。

之后你错过了冒号

http://razorsql.com/docs/help_oracle.html

试试这样:

<beans>
 <bean id = "drds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name = "driverClassName" value = "oracle.jdbc.driver.OracleDriver"/>
        <property name = "url" value = "jdbc:oracle:thin:@localhost:1521:orcl"/>
        <property name = "username" value = "templates"/>
        <property name = "password" value = "templates"/>
    </bean>

    <bean id = "dob" class="com.Templates.DBOperation.DBOperationBean">
        <property name = "ds" ref = "drds"/>
    </bean>
</beans>