简体   繁体   English

我的选择查询,其中where子句不起作用

[英]My Select Query with where clause not working

I am trying to fetch data from postgres database using where clause. 我试图使用where子句从postgres数据库中获取数据。 It is returning null values. 它返回空值。 But running the same query in postgres worksheet, it is giving the datas. 但是在postgres工作表中运行相同的查询,它会提供数据。

And one more thing, I cannot able to use without double quotes(\\"USER_INFO\\"),while using without double quotes I am getting the error "column name does not exist". 还有一件事,我不能使用没有双引号(\\“USER_INFO \\”),而使用没有双引号我得到错误“列名称不存在”。

But most of the online examples are without double quotes only. 但是大多数在线示例都没有双引号。

Please help on this to resolve. 请帮忙解决这个问题。 Thanks in advance. 提前致谢。

    public class PostGreDB {
static final String JDBC_DRIVER = "org.postgresql.Driver";
    static final String DB_URL = "jdbc:postgresql://localhost:5432/";

public static void main(String[] args) throws SQLException
{
Connection conn = null;
//Statement st = null;
try{
    //STEP 2: Register JDBC driver
    Class.forName("org.postgresql.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/","postgres","XXXX@1904");

    //STEP 4: Execute a query
    System.out.println("Creating statement...");
    //st = conn.createStatement();
    String sql;
    sql = "SELECT * FROM public." + "\"USER_INFO\" where \"USER_INFO\".\"EMAIL_ID\" = ?";
    System.out.println(sql);
    try(PreparedStatement pst= conn.prepareStatement(sql)){
        pst.setString(1 , "cppandi33@gmail.com");
        pst.executeQuery();

    try(ResultSet rs = pst.getResultSet()){
    //STEP 5: Extract data from result set
    while(rs.next()){
        //Retrieve by column name
        String first = rs.getString("USER_ID");
        String last = rs.getString("USER_NAME");
        String email = rs.getString("EMAIL_ID");

        //Display values
        System.out.print("User ID: " + first+"\n");
        System.out.println("User Name: " + last+"\n");
        System.out.println("Email: " + email);
    }
    rs.close();
    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    //STEP 6: Clean-up environment

   // st.close();
    conn.close();
}catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
}catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
}
finally
{
    try{
        if(conn!=null)
            conn.close();
    }
    catch(SQLException se){
        se.printStackTrace();
    }//end finally try
}
}
}

When column contains "null" as data. 当列包含“null”作为数据时。 It gives null only. 它只给出null。 Try to insert some data using insert query and after that we can try to fetch. 尝试使用插入查询插入一些数据,之后我们可以尝试获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM