来自 spring mvc 的更改未反映在 oracle 数据库中

Changes from spring mvc not getting reflected in oracle database

我正在学习从 Spring MVC 建立到 oracle 数据库的连接。所以这是我到目前为止所做的

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="org.springdemo" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@192.168.0.8:1521:xe"/>
        <property name="username" value="sys as sysdba"/>
        <property name="password" value="7299"/>
    </bean>



</beans> 

Circle.java

package org.springdemo.model;

public class Circle {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Circle(int id , String name){

        setId(id);
        setName(name);
    }


}

JdbcDaoImpl.java

package org.springdemo.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class JdbcDaoImpl {

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate = new JdbcTemplate();

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void getCircle(){


        String sql = "SELECT COUNT(*) FROM circle";
        jdbcTemplate.setDataSource(getDataSource());
        int count = jdbcTemplate.queryForObject(sql,Integer.class);
        System.out.println("count is"+count);

    }

    public void delete(int id) {
        JdbcTemplate delete = new JdbcTemplate(dataSource);
        delete.update("DELETE from CIRCLE where ID= ?",
                new Object[] {id});
        System.out.println("deleted successfully");
    }

}

JdbcDemo.java

package org.springdemo;

import org.springdemo.dao.JdbcDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcDemo {



    public static void main(String[] args) {

        ApplicationContext ctxt = new ClassPathXmlApplicationContext("spring.xml");
        JdbcDaoImpl circleDao =(JdbcDaoImpl) ctxt.getBean("jdbcDaoImpl", JdbcDaoImpl.class);
        circleDao.getCircle();
        circleDao.delete(2);

    }

}

一切似乎都工作正常,没有任何错误,我得到了以下输出。

但是在数据库中我有 3 行并且我的 table 没有得到更新。

当我试图查询一个不存在的 table 以确认我是否连接到数据库时,我收到异常指示我仅连接到数据库。谁能解释一下我的更改没有反映在数据库中的可能原因是什么。

让我们Spring实例化您的 jdbctemplate

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">    
<property name="dataSource" ref="dataSource"></property>    
</bean> 

@Autowired
private JdbcTemplate jdbcTemplate

不要使用 Obect[],只使用 id。

方法 delete.update 返回什么?