繁体   English   中英

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

[英]My Select Query with where clause not working

我试图使用where子句从postgres数据库中获取数据。 它返回空值。 但是在postgres工作表中运行相同的查询,它会提供数据。

还有一件事,我不能使用没有双引号(\\“USER_INFO \\”),而使用没有双引号我得到错误“列名称不存在”。

但是大多数在线示例都没有双引号。

请帮忙解决这个问题。 提前致谢。

    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
}
}
}

当列包含“null”作为数据时。 它只给出null。 尝试使用插入查询插入一些数据,之后我们可以尝试获取。

暂无
暂无

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

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