小编Den*_*eng的帖子

JPA字符串比较

我使用JPQL查询编写了一个简单的登录系统,它总是不返回结果:

public boolean check(String name, String password) {
    final String qstring="SELECT e FROM Muser e WHERE e.name = '"+name+"'";
    Muser user;
    try{
        user = em.createQuery(qstring, Muser.class).getSingleResult();  
    }
    catch(NoResultException e){
        return false;
    }
    return password.equals(user.getPassword());
}
Run Code Online (Sandbox Code Playgroud)

当我将其更改为本机查询时:

user = (Muser) em.createNativeQuery(qstring, Muser.class).getSingleResult();
Run Code Online (Sandbox Code Playgroud)

或者一个int表达式:

final String qstring="SELECT e FROM Muser e WHERE e.id = "+id; 
Run Code Online (Sandbox Code Playgroud)

没事.有什么问题?太感谢了!

string comparison jpa equals jpql

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

2D数组,输出不在接近正确的位置

import java.util.Scanner;

public class Maze {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int rows = 0;
        int cols = 0;
        String arrayLine = "";
        int counter = 0;

        rows = sc.nextInt();
        cols = sc.nextInt();
        arrayLine = sc.next();

        char[][] array = new char[rows][cols];

        for(int r=0; r<rows; r++){
            for (int c=0; c<cols; c++){
                array[r][c] = arrayLine.charAt(counter);
                counter ++;
            }
        }

        System.out.println(array);
        System.out.println();
    }
}
Run Code Online (Sandbox Code Playgroud)

我带来的信息是:

8
7
000000011111S0000000110101111010101100010110111011010E00
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到的输出是 [[C@252f0999

请帮助,我刚刚开始学习java!

java arrays 2d output

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

在C中是否有一个数组中变量的模式,它被赋值为0?

我想知道是否在C中声明了一个int元素数组.是否有一个模式,根据该模式,只有数组的某些值被赋值为0而其他值存储垃圾值?例如:

#include <stdio.h>

void main()
{
    int a[5];
    int i;
    for (i=0;i<=4;i++)
    {
        printf("%d\n",a[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译并运行程序后,我得到了这个输出,即

0
0
4195344
0
2107770384
Run Code Online (Sandbox Code Playgroud)

所以零存在a[0], a[1]并且a[3]while a[2]每次编译和运行时包含相同的值,而a[4]值保持变化(包括负数).为什么只发生一个数组的某些固定索引被初始化为零并且它是否与过去的内存空间分配有关?

c arrays initialization

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

更改下一个元素时,ArrayList/List 中的 Java 前一个元素被覆盖

我对Java有点陌生。在下面的 forLoop 中,我循环遍历 arraylist 的元素,并尝试更改位置对象。当 forLoop 完成时,所有元素的位置等于最后一个元素中的值。我对此进行了调试,但可以弄清楚为什么会发生这种情况。

编辑:下面的函数是我初始化和填充选项的地方。它还包含为每个选项设置位置的逻辑。

protected void InitializeOptions() {
    options = new ArrayList<Button>();
    options.add(new Button("button.png", "Quick Fire"));
    options.add(new Button("button.png", "20 Questions"));
    options.add(new Button("button.png", "Decisions! Decisions!"));
    options.add(new Button("button.png", "OMG"));

    for(int i = 0; i < OPTIONCOUNT; ++i) {
        options.get(i).SetPosition(i*35, i*35);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的对象元素被声明为一个通用的 Button 对象列表,如下所示。我不确定这是否有所作为。

List<Button> options = new ArrayList<Button>();
Run Code Online (Sandbox Code Playgroud)

编辑:我有一个按钮类,它有两个重要的对象:PositionedTexture 背景和 PositionedText 文本。这些对象中的每一个都有一个用于位置的 Vector2。每个类的代码如下

public class PositionedTexture {
    public Texture Texture;
    public Vector2 Position;

    public PositionedTexture(String texturePath) {
        Texture = new Texture(Gdx.files.internal(texturePath));
        Position = Vector2.Zero;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java list arraylist

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

为什么占用核心数高于线程池大小?

Java程序占用的内核数量是否高于线程池的大小?我将线程池大小指定为4,但是我看到我的java代码在一段时间后占用6或7个核心(即顶部显示600).我这台机器上有8个核心.我做错了什么或是java决定在进行多线程时自动使用其他未使用的内核?如果是,是否有办法强制多线程期间使用的核心数量?

我正在使用的代码片段如下所示:

ExecutorService executor = Executors.newFixedThreadPool(4);
for (int i = 0; i < N; i++) {
   executor.execute(do something);                
}
executor.shutdown();
while (!executor.isTerminated()) {}
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

为什么这个功能会发生故障?

这让我困扰了一段时间; 这个功能甚至没有返回,它只是段错误.我指定了正确的文件位置,我在函数的每个可能的点检查错误,我不明白?

GLchar* getShaderString(const GLchar* file_path){
    FILE* srcfile = NULL;
    if(!(srcfile = fopen(file_path, "r")))
        return(NULL);
    fseek(srcfile, 0l, SEEK_END);
    long len;
    if((len = ftell(srcfile)) == -1)
        return (NULL);
    fseek(srcfile, 0l, SEEK_SET);
    GLchar* buff;
    if(!(buff = malloc(len + 1)))
        return (NULL);
    fread((GLvoid*)buff, len, 1, srcfile);
    fclose(srcfile);
    buff[len + 1] = '\0';
    return (buff);
}
Run Code Online (Sandbox Code Playgroud)

c file-io segmentation-fault

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

如何使用两个迭代器迭代HashMap?

我想在HashMap中搜索重复项.目前这是我的HashMap:

HashMap<String, HashMap<String, String>>

我打算创建两个迭代器,一个i和另一个j,以及两个循环.第一while循环将具有的索引i,然后第二环路将具有的索引j,但j==i在循环开始之前.

Iterator<Entry<String, HashMap<String, String>>> i = listings.entrySet().iterator();
while(i.hasNext()) {
    HashMap<String, String> entry = i.next().getValue();
    Iterator<Entry<String, HashMap<String, String>>> j = i;

    while(j.hasNext()) {
        j.next();
        // DO STUFF
    }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为当我打电话时j.next(),它也会改变索引i.

java hashmap map

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

java.lang.ClassFormatError:类中的非法字段修饰符

我正在尝试使用环境服务实现一个简单的TabGroup .我的代码看起来像这样:

public class TabPanel
{
  ...
  @Inject
  private Environment environment;
  ...
  void beginRender()
  {
      environment.push(TabContext.class, new TabContext()
      {

          public boolean isActiveTab(String tabId)
          {
             return active != null && active.equals(tabId);
          }

      });
   }

}


public interface TabContext
{
   boolean isActiveTab(String tabId);
}


public class Tab
{
   ...
   @Environmental
   private TabContext tabContext;

   @Inject
   private ComponentResources resources;
   ...

   private boolean isActiveAndEnabled()
   {
      return tabContext.isActiveTab(resources.getId()) && !disabled;
   }

}
Run Code Online (Sandbox Code Playgroud)

执行此代码时,它会抛出以下异常:

java.lang.ClassFormatError: Illegal field modifiers in class TabContext
Run Code Online (Sandbox Code Playgroud)

我在旧线程中找到了部分答案:"......这样的接口不应该在组件包中开始."

我的问题是:我应该在哪里移动TabContext界面?

遵循规则#1我发布整个堆栈跟踪:

java.lang.ClassFormatError: …
Run Code Online (Sandbox Code Playgroud)

java tapestry

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

Ucanaccess SQL异常:不支持的功能(Access和Netbeans)

我已经研究了这个问题好几天,并决定在这里问这个问题,看看是否有人可以帮我指出正确的方向.

我正在尝试使用MS Access 2013表中的数据填充Netbeans 8.0.2程序中的组合框.

我正在使用最新的"Ucanaccess"及其所有必要的组件来获得两者之间的连接,并且我可以告诉它连接是好的.但是,当我运行程序时,会弹出一条错误异常消息,读取:

net.ucanaccess.jdbc.UcanaccessSQLException: feature not supported

就是这样 - 没有其他字母,字符,数字......没有.

我老实说迷路了.有谁知道为什么我可能会收到此异常消息?

另外,我在Mac上运行它,但是我使用parallels并且实际上是在Microsoft Windows 7虚拟平台上运行它.从那时起它一直没有给我任何麻烦.64位.

这是我编码的内容.

import java.sql.*;
import javax.swing.*;

public class NewJFrame extends javax.swing.JFrame {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    private void FillCombo() {
        String sql = "Select [Description] from [Doors]";
        try {
            String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
            Class.forName(driver);
            conn = DriverManager.getConnection("jdbc:ucanaccess://C:/Test/DB.accdb");
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery(sql);

            while (rs.next()) {
                String nme = rs.getString("Description");
                cmb1.addItem(nme);
            }
            conn.close();
        } catch (Exception …
Run Code Online (Sandbox Code Playgroud)

java ms-access netbeans sqlexception ucanaccess

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

请求成员'next'的东西不是结构或联合错误

我在这条线上遇到了问题 c->next=var;

我的职责是:

{
    struct film
    {  
        int id;
        char nom[50];
        char typeFilm[50];
        int duree;
        int dateSortie;
        struct film *next;
    };

#include<stdio.h>
#include<string.h>
#include "structure_film.h"    
#include<stdlib.h>

void ajouter_film(struct film **h,struct film **c)
{
    struct film *var;
    int s, genre;
    do
    {
        var=(struct film*) malloc(sizeof(struct film));        
        printf("quel est le nom du film que vous voulez ajouter ? \n");
        scanf("%s",var->nom);  
        printf("\nquel est le type de ce film \n 1-action \n 2-aventure \n 3-romantique \n 4-comedie \n");
        scanf("%d",&genre);

        switch(genre)
        {
            case 1 : …
Run Code Online (Sandbox Code Playgroud)

c

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