Example usage for java.sql PreparedStatement executeQuery

List of usage examples for java.sql PreparedStatement executeQuery

Introduction

In this page you can find the example usage for java.sql PreparedStatement executeQuery.

Prototype

ResultSet executeQuery() throws SQLException;

Source Link

Document

Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

Usage

From source file:dhbw.clippinggorilla.objects.user.UserUtils.java

/**
 * Checks whether user already exists in database
 *
 * @param username The username to be checked
 * @return true if user exists/*from  w ww .  j  ava 2  s  .c  o  m*/
 */
public static boolean checkUsernameExisting(String username) {
    try {
        String sql = "SELECT * FROM " + Tables.USER + " WHERE " + Columns.USERNAME + " = ?";
        PreparedStatement stat = Database.getConnection().prepareStatement(sql);
        stat.setString(1, username);
        return stat.executeQuery().next();
    } catch (SQLException ex) {
        Log.warning("SQL failed on check whether user exists", ex);
        return false;
    }
}

From source file:com.gdo.project.util.SqlUtils.java

@Deprecated
public static ResultSet preparedSelect(PreparedStatement stmt) throws SQLException {
    ResultSet rs = null;/*from ww  w.  j  a v  a 2s  .  c  om*/
    try {
        rs = stmt.executeQuery();
    } finally {
        /**/
    }
    return rs;
}

From source file:com.product.ProductResource.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)//from   ww w  .  ja v  a2s  .c o  m
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (conn == null) {
        return "not connected";
    } else {
        String query = "Select * from product where product_id = ?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        String result = "";
        JsonArrayBuilder productArr = Json.createArrayBuilder();
        while (rs.next()) {
            JsonObjectBuilder prodMap = Json.createObjectBuilder();
            prodMap.add("productID", rs.getInt("product_id"));
            prodMap.add("name", rs.getString("name"));
            prodMap.add("description", rs.getString("description"));
            prodMap.add("quantity", rs.getInt("quantity"));
            productArr.add(prodMap);
        }
        result = productArr.build().toString();

        return result;
    }

}

From source file:de.klemp.middleware.controller.Controller.java

/**
 * This method is used by the method informController(). It searches the
 * methods which should be invoked. The methods are saved in objects of the
 * class "MethodToInvoke"//from  ww  w. j  a  v a2 s  .  c  o  m
 * 
 * @param classes
 *            class of the first component
 * @param method
 *            method of the first component
 * @param name
 *            name of the input device which has sent data
 * @return a list of the methods which were found
 */
private static synchronized void controllerToList() {
    ArrayList<MethodToInvoke> methods = new ArrayList<MethodToInvoke>();
    controller.clear();
    createDBConnection();
    try {
        PreparedStatement st = conn.prepareStatement("select * from \"Controller\" ;");
        ResultSet r = st.executeQuery();
        while (r.next()) {
            String key = r.getString("classinput") + "," + r.getString("methodinput") + ","
                    + r.getString("nameinputdevice");
            ArrayList list = controller.get(key);

            Method m = getMethod(r.getString("classoutput"), r.getString("methodoutput"));
            MethodToInvoke m2 = new MethodToInvoke(r.getString("classoutput"), m, r.getString("data"),
                    r.getString("topic"));
            if (list != null) {
                list.add(m2);
            } else {
                list = new ArrayList();
                list.add(m2);
            }
            controller.put(key, list);
        }

    } catch (SQLException e) {
        logger.error("SQL Exception in controllerToList()", e);
    }
    closeDBConnection();

}

From source file:com.imagelake.control.InterfaceDAOImp.java

public String listPrivilages() {
    String sb = "";
    JSONArray ja = new JSONArray();
    try {/* www.j a  va  2s  .  co  m*/
        String sql3 = "SELECT * FROM user_type WHERE user_type_id!=4";
        PreparedStatement ps3 = DBFactory.getConnection().prepareStatement(sql3);
        ResultSet rs3 = ps3.executeQuery();
        while (rs3.next()) {
            JSONObject jo = new JSONObject();
            jo.put("id", rs3.getString(1));
            jo.put("type", rs3.getString(2));
            ja.add(jo);
        }
        sb = "json=" + ja.toJSONString();
    } catch (Exception e) {
        e.printStackTrace();
        sb = "msg=Internal server error,Please try again later.";
    }
    return sb;
}

From source file:com.imagelake.control.InterfaceDAOImp.java

public String listInterfaces() {
    String sb = "";
    JSONArray ja = new JSONArray();
    try {//from w w w . jav a  2s  .  c o  m
        String sql = "SELECT * FROM interfaces";
        PreparedStatement ps = DBFactory.getConnection().prepareStatement(sql);

        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            JSONObject jo = new JSONObject();
            jo.put("id", rs.getInt(1));
            jo.put("name", rs.getString(3));
            jo.put("state", rs.getInt(4));
            ja.add(jo);

        }
        sb = "json=" + ja.toJSONString();
    } catch (Exception e) {
        e.printStackTrace();
        sb = "msg=Internal server error,Please try again later.";
    }
    return sb;
}