小编Rom*_*man的帖子

无法将连接强制转换为oracle.jdbc.OracleConnection

为什么java.sql.Connection无法在下面的代码中强制转换为oracle.jdbc.OracleConnection?

我的主要目标是传递给Oracle连接新用户名并将其保存在例如'osuser'列的'SESSION'表中,因为我想跟踪数据库用户更改并将其显示在表中.

@Repository
public class AuditLogDAOImpl implements AuditLogDAO {

    @PersistenceContext(unitName="myUnitName")
    EntityManager em;

    @Resource(name = "dataSource")
    DataSource dataSource;

    public void init() {

        try {
            Connection connection = DataSourceUtils.getConnection(dataSource);
            OracleConnection oracleConnection = (OracleConnection) connection; //Here I got cast exception!

            String metrics[] = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
            metrics[OracleConnection.END_TO_END_CLIENTID_INDEX] = "my_new_username";

            oracleConnection.setEndToEndMetrics(metrics, (short) 0);

            java.util.Properties props = new java.util.Properties();
            props.put("osuser", "newValue");

            oracleConnection.setClientInfo(props);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误日志:

10:42:29,251 INFO  [STDOUT] org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@bcc8cb
10:42:51,701 ERROR [STDERR] java.lang.ClassCastException: $Proxy286 cannot be cast to oracle.jdbc.OracleConnection …
Run Code Online (Sandbox Code Playgroud)

java oracle hibernate jndi jdbc

16
推荐指数
2
解决办法
6万
查看次数

如何使用struts2-json-plugin将JSON绑定到Struts2中的Java对象

我想将JSON反序列化(绑定)到java对象.如何在Struts2中做到这一点?

我正在尝试使用struts2-json-plugin,你可以在下面的代码中看到,但是从前端发送的JSON没有绑定到我的java对象.你能帮我吗,请问如何使这段代码正常工作?

请看一下我的Action类,我不确定我是否在这个Action中正确处理JSON,或者我错过了什么?

我试图绑定的JSON:

{"data":[
    {"active":true,"color":"orange","date":"2008-01-01","id":1,"name":"Chris"},
    {"active":false,"color":"blue","date":"2013-03-03","id":2,"name":"Kate"},
    {"active":true,"color":"black","date":"2013-05-03","id":3,"name":"Blade"},
    {"active":false,"color":"yellow","date":"2013-01-01","id":4,"name":"Zack"}]
}
Run Code Online (Sandbox Code Playgroud)

通过Ajax发送JSON:

$.ajax({
  url: "../json/saveJSONDataAction.action",
  data: {"data": handsontable.getData()}, //returns all cells' data
  dataType: 'json',
  type: 'POST',
  success: function (res) {
    if (res.result === 'ok') {
      $console.text('Data saved');
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

在Struts2中接收JSON:

我可以在调试中使用execute()方法,但不幸的data是,该字段始终为null.我应该怎么做才能使这个字段充满JSON的数据?JSON的格式是否正确绑定到List<Report> data

@ParentPackage("json-default")
@Action(value="saveJSONDataAction")
@Result(type="json")
public class JSONSaveAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private List<Report> data;

    public JSONSaveAction(){
    }

    public String execute() {
        try {
            System.out.println(data);
        } catch (Exception e) …
Run Code Online (Sandbox Code Playgroud)

java struts2 struts2-json-plugin

10
推荐指数
1
解决办法
2万
查看次数

如何在表上选择并计算出现一些值

我请求你帮忙,因为我不太了解SQL.

我需要从表列中计算出现一些值来实现像统计表一样的效果,如下图所示:

需要的结果:

在此输入图像描述

评论:

我的结果表需要有前两列(contry和site)来自第一个表"Violations"和接下来的5个列,它们将在Status表的每个可能值的"Violations"中包含出现status_id的数量(计数).

说明:

所以,我有两个表:违规和状态.请看我的sqlfiddle

违规行为:

  • id很长,
  • country varchar(20),
  • site varchar(20),
  • status_id long,< - 这是状态表中状态的ID.
  • ......在这种情况下,其他列不重要

状态:

  • id很长,
  • status long列"status"具有值(1-4),它们映射到字符串值:可疑违规(1),确认违规(2),确认无违规(3),未确定(4)

在我的连接(或仅基于一个表违反)的结果是具有应包含列的表:

  • 来自违规表:"国家"和"网站"
  • 来自状态表:"可疑违规","确认违规","确认无违规","未确定","总计"(此列为违规表中出现的计数器).

现状和新要求:

首先尝试完成(感谢bluefeet),几乎完美...

select v.country,
v.site,
    SUM(case when s.id = 1 then 1 else 0 end) Total_SuspectedViolations,
    SUM(case when s.id = 2 then 1 else 0 end) Total_ConfirmedViolations,
    SUM(case when s.id = 3 then 1 else 0 end) Total_ConfirmedNoViolations,
    SUM(case when s.id = 4 then 1 else 0 end) Total_NotDetermined,
    COUNT(*) Total
from violations v …
Run Code Online (Sandbox Code Playgroud)

sql jpa join count oracle11g

8
推荐指数
1
解决办法
2860
查看次数

通过JSON从Handsontable检索数据到Struts2 Action无法正常工作

我使用struts2-json插件生成JSON数据和Ajax,用来自该JSON的数据(根据源)填充表(handsontable ).

现在,我需要使用JSON的Ajax从表中检索数据到Struts2 Action.首先,我已经使用JSON从Struts2 Action传递给Handsontable的数据实现了填充表,这非常简单且有效.但为什么保存不起作用,你可以在下面的附加代码中看到?

正如我在firebug中看到的那样发送了POST,并且在调试中,我的JSONSaveAction操作中检索到了请求,但是字段数据没有填充JSON数据,为什么?不应该通过struts2-json插件自动将数据绑定到java对象?我究竟做错了什么?

在handontable部分,该函数handsontable.getData()负责从表中获取整个数据.所以我这样使用它但没有成功:

$.ajax({
        url: "../json/saveJSONDataAction.action",
        data: {data: handsontable.getData()}, //returns all cells' data
        dataType: 'json',
        type: 'POST',
        success: function (res) {
            if (res.result === 'ok') {
                $console.text('Data saved');
            }
            else {
                $console.text('Save error');
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

该函数handsontable.getData()实际上检索了我检查的所有数据,但是List<Report> data在我的JSONSaveAction操作中数据没有绑定到java对象.你知道为什么吗?

这是POST请求后我的表和firebug信息的屏幕截图: 在此输入图像描述

将JSON发送到handontable的动作(正常工作):

@ParentPackage("json-default")
@Action(value="getJSONDataAction")
@Result(name="success", type="json")
public class JSONDataAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private List<Report> data = new ArrayList<Report>();

    public JSONDataAction(){
        data.add(new Report(1, "Chris", …
Run Code Online (Sandbox Code Playgroud)

ajax jquery struts2 handsontable struts2-json-plugin

8
推荐指数
1
解决办法
6086
查看次数

如何在Spring LDAP中添加LDAP缓存?

我想在本地缓存LDAP用户数据以允许更快的查询.Spring LDAP提供了这样的功能吗?我怎样才能做到这一点?

我使用Spring Security 3.1和Spring LDAP 1.3.1进行身份验证和授权.如果存在,使用内置机制为LDAP建立缓存会很好.

Spring LDAP配置:

的applicationContext-ldap.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:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd
    ">

    <!-- Ldap -->
    <jee:jndi-lookup id="ldapUrl" jndi-name="appName/ldapUrl" expected-type="java.lang.String" />
    <jee:jndi-lookup id="ldapUser" jndi-name="appName/ldapUser" expected-type="java.lang.String" />
    <jee:jndi-lookup id="ldapPassword" jndi-name="appName/ldapPassword" expected-type="java.lang.String" />

    <!-- for authentication and search purpose -->
    <bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
        <property name="url" ref="ldapUrl" />
        <property name="userDn" ref="ldapUser" />
        <property name="password" ref="ldapPassword" />
        <property name="pooled" value="true" />
    </bean>

    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
        <property name="contextSource" ref="ldapContextSource" />
    </bean>

    <!-- for pagination search …
Run Code Online (Sandbox Code Playgroud)

spring ldap spring-security spring-ldap

6
推荐指数
1
解决办法
5247
查看次数

Spring Data Elasticsearch 需要名为 id 的属性

我开始使用 Spring Data Elasticsearch 并发现问题。在通过存储库调用 findAll() 的运行测试期间,我得到:

No id property found for class com.example.domain.entity.Project!
Run Code Online (Sandbox Code Playgroud)

当我@Transient private Long id;在我的项目实体中添加字段时,我能够正确检索结果。但我不想添加这个字段,因为我已经定义了名为projectId. @Id该字段也有注释,为什么我的字段projectId不被视为 ID?看起来@Id注解不适用于 spring-data-elasticsearch,这可能吗?

我应该怎么做才能避免向id实体添加瞬态字段?它更像是解决方法而不是解决方案......

项目类别:

@Entity
@Document(indexName = "project_list", type = "external")
public class Project implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "PROJECT_ID_GENERATOR", sequenceName = "PROJECT_SEQ", initialValue = 100, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROJECT_ID_GENERATOR")
    @Column(name = "PROJECT_ID")
    private Long projectId;

    .... …
Run Code Online (Sandbox Code Playgroud)

spring elasticsearch spring-data-elasticsearch

5
推荐指数
1
解决办法
6810
查看次数

Spring Data Elasticsearch 是否支持来自 JPA 的 @Id 注释?

我开始使用 Spring Data Elasticsearch。我读:

类的属性之一需要是 id,通过使用 @Id 对其进行注释或使用自动找到的名称 id 或 documentId 之一。

但是当我用 @Id 标记我的项目实体字段 projectId 时,elasticsearch 仍然说:

No id property found for class com.example.domain.entity.Project!
Run Code Online (Sandbox Code Playgroud)

我发现我正在使用 JPA 包中的 @Id 注释:javax.persistence.Id. 当我@org.springframework.data.annotation.Id为我的字段添加另一个 @Id 注释时,从存储库中获取正在工作!

问题是我不想同时使用 2 种 @Id 注释。此外,我想使用 JPA 注释只是因为其他模块正在使用基于 JPA 的存储库层(Spring Data JPA)。

Spring Data Elasticsearch 是否支持来自 JPA 的 @Id 注释?了解这一点非常重要,因为进一步了解嵌入式 ID 呢?Spring Data Elasticsearch 是否支持 @EmbeddedId 注解?

我的实体:

@Entity
@Document(indexName = "project_list", type = "external")
public class Project implements Serializable {

    @Id
    @org.springframework.data.annotation.Id <-- …
Run Code Online (Sandbox Code Playgroud)

spring-data-elasticsearch

5
推荐指数
1
解决办法
2640
查看次数

如何强制SQL查询计数max/min/avg正常工作?

我有一个问题是编写正确的sql查询,该查询应根据国家和网站选择两个日期之间的最小/最大/平均持续时间.

我的查询:

SELECT v.country as country, v.site as site,
  COUNT(*) as N --,
  --MAX(list of durations in days between discovered date to repored date on each violation by country and site) as "Maximum",
  --MIN(list of durations in days between discovered date to repored date on each violation by country and site) as "Minimum",
  --AVG(list of durations in days between discovered date to repored date on each violation by country and site) as "Mean"
FROM violations v
WHERE v.trial_id = 3 …
Run Code Online (Sandbox Code Playgroud)

sql oracle

2
推荐指数
1
解决办法
770
查看次数

如何调用struts2中一个动作中定义的不同方法?

我不熟悉struts2,但我知道方法execute()在按名称调用操作期间在Action中被默认调用.但是如何调用同一动作类中定义的其他方法?

在下面的示例中,当我在ajax中设置url link时调用execute()方法:saveJSONDataAction.action感谢@Action注释.

网址应该如何通过ajax调用otherMethod()?

行动类:

@ParentPackage("json-default")
@Action(value="getJSONDataAction")
@Result(name="success", type="json")
public class JSONDataAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private List<Report> data = new ArrayList<Report>();

    public JSONDataAction(){
        data.add(new Report(1, "Chris", true, "2008-01-01", "orange"));
    }

    public String execute() {
        return SUCCESS;
    }

    public String otherMethod() {
        //do something else ..
        return SUCCESS;
    }

    // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

Ajax调用:

$.ajax({
    url: "../json/saveJSONDataAction.action",
    data: data,
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
    success: function (res) {
      if (res.result …
Run Code Online (Sandbox Code Playgroud)

java ajax struts2

2
推荐指数
1
解决办法
1万
查看次数

如何通过正则表达式删除 URL 的某些部分?

我有这样的完整链接:

http://localhost:8080/suffix/rest/of/link
Run Code Online (Sandbox Code Playgroud)

如何在 Java 中编写正则表达式,它只返回带有后缀的 url 的主要部分:http://localhost/suffix而没有:/rest/of/link

  • 可能的协议:http、https
  • 可能的端口:多种可能性

我假设我需要在第三次出现'/'标记(包括)后删除整个文本。我想按以下方式进行,但我不太了解正则表达式,请您帮忙如何正确编写正则表达式?

String appUrl = fullRequestUrl.replaceAll("(.*\\/{2})", ""); //this removes 'http://' but this is not my case
Run Code Online (Sandbox Code Playgroud)

java regex string

1
推荐指数
1
解决办法
4246
查看次数