편집 기록

편집 기록
  • 프로필 알 수 없는 사용자님의 편집
    날짜2018.06.21

    자바에서 String이 null이 아닌지 안 비었는지 어떻게 확인하죠?


    발생하는 문제 및 실행환경

    제 웹앱에 문자열 몇개를 받는 검색 필드랑 콤보박스가 있는데요. 그래서 인자 두개를 리모트 함수로 보내요. 제가 원하는건 입력값이 null이거나 비었는지를 확인해서 쿼리를 구성하는거에요..

    소스코드

    public ArrayList findEmployees(String str, int dep)
           throws ClassNotFoundException, SQLException{
    
        System.out.println("List IN");
        ArrayList list = new ArrayList();
        java.sql.Statement stmt;
        java.sql.ResultSet rs;
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/general";
        java.sql.Connection con = DriverManager.getConnection(url, "root", "1234");
        System.out.println("URL: " + url);
        System.out.println("Connection: " + con);
        stmt = con.createStatement();
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        String qry = "SELECT * FROM PERSON ";
        String werstr = "WHERE";
        if(str!= null && str != "**here i want to check the 'str' is empty or not." )
        {
            qry += werstr + " NAME LIKE '%"+str+"%'";
            System.out.println(qry);
            werstr = "AND";
        }
        if(dep != 0)
        {
            qry += werstr + "dept="+dep;
        }
        qry += ";";
        System.out.println(qry);
        rs = stmt.executeQuery(qry);
        while (rs.next()) {
            Employee employee = new Employee();
            String name = rs.getString(2);
            employee.setName(name);
            int id = rs.getInt(1);
            employee.setId(id);
            int dept = rs.getInt(4);
            employee.setDept(dept);
            int age = rs.getInt(3);
            employee.setAge(age);
            list.add(employee);
        }
        System.out.println("List Out");
        return list;
    }
    

    이건 제 소스인데, 어떻게 하면 될까요?

  • 프로필 김대운님의 편집
    날짜2016.01.14

    자바에서 String이 null이 아닌지 안 비었는지 어떻게 확인하죠?


    발생하는 문제 및 실행환경

    제 웹앱에 문자열 몇개를 받는 검색 필드랑 콤보박스가 있는데요. 그래서 인자 두개를 리모트 함수로 보내요. 제가 원하는건 입력값이 null이거나 비었는지를 확인해서 쿼리를 구성하는거에요..

    소스코드

    public ArrayList findEmployees(String str, int dep)
           throws ClassNotFoundException, SQLException{
    
        System.out.println("List IN");
        ArrayList list = new ArrayList();
        java.sql.Statement stmt;
        java.sql.ResultSet rs;
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/general";
        java.sql.Connection con = DriverManager.getConnection(url, "root", "1234");
        System.out.println("URL: " + url);
        System.out.println("Connection: " + con);
        stmt = con.createStatement();
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        String qry = "SELECT * FROM PERSON ";
        String werstr = "WHERE";
        if(str!= null && str != "**here i want to check the 'str' is empty or not." )
        {
            qry += werstr + " NAME LIKE '%"+str+"%'";
            System.out.println(qry);
            werstr = "AND";
        }
        if(dep != 0)
        {
            qry += werstr + "dept="+dep;
        }
        qry += ";";
        System.out.println(qry);
        rs = stmt.executeQuery(qry);
        while (rs.next()) {
            Employee employee = new Employee();
            String name = rs.getString(2);
            employee.setName(name);
            int id = rs.getInt(1);
            employee.setId(id);
            int dept = rs.getInt(4);
            employee.setDept(dept);
            int age = rs.getInt(3);
            employee.setAge(age);
            list.add(employee);
        }
        System.out.println("List Out");
        return list;
    }
    

    이건 제 소스인데, 어떻게 하면 될까요?