2015-09-06 60 views
0

我尝试运行从Java查询,我运行PostgreSQL的编辑和这项工作的查询,但在运行的应用程序不能正常工作时,运行该查询返回数据查询在java中不工作

这是代码:

private synchronized Perfil consultarPerfil(String nombre) { 
    Perfil perfil = null; 
    String sql = "SELECT * FROM PERFIL WHERE NOMBRE = ?"; 
    Connection connection = conexion.getConnection(); 
    try { 
     PreparedStatement statement = connection.prepareStatement(sql); 
     statement.setString(1, nombre); 
     ResultSet rs = statement.executeQuery(); 
     while (rs.next()) { 
      perfil = new Perfil(rs.getString("nombre"), rs.getString("descripcion"), rs.getBoolean("estado")); 
     } 
    } catch (SQLException ex) { 
     Logger.getLogger(ColeccionPerfiles.class.getName()).log(Level.SEVERE, null, ex); 
    } finally { 
     conexion.close(connection); 
    } 
    return perfil; 
} 
+0

您连接到了正确的数据库吗? – zegkljan

+0

不相关,但为什么该方法是“synchronized”? – Andreas

回答

0

补充一点:后= 这样

字符串SQL = “SELECT * FROM PERFIL WHERE NOMBRE =?”;

注:该参数必须具有相同的数据类型字段“农布雷”有

0

如果列NOMBRE表PERFIL是一个数字,那么当您尝试设置您的参数诠释是这样的:

statement.setInt(1, nombre); 

而不是

statement.setString(1, nombre); 

问候,

+0

Nombre是“姓名”而不是“数字”的西班牙语。 https://translate.google.com/#es/en/nombre –

+0

感谢您提供信息,但这不回答最初的问题 – IBJ

0

我尝试添加: after =但显示异常,NOMBRE是字符串类型,我只有一个数据库。连接正常工作。

我可以有时从java运行查询,但后来停止工作,调试器不显示错误。重新启动计算机并清理缓存不工作netbeans。

我最近在java上花费了很多这样的错误。