Example usage for java.sql PreparedStatement setString

List of usage examples for java.sql PreparedStatement setString

Introduction

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

Prototype

void setString(int parameterIndex, String x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java String value.

Usage

From source file:las.DBConnector.java

public static void insertMemberIntoTable(Member member) throws SQLException {
    String data = "INSERT INTO MEMBERS(MEMBER_ID,NAME,EMAIL,PRIVILEGE,ISSTAFF)" + "Values (?,?,?,?,?)";
    PreparedStatement pt = conn.prepareStatement(data);
    pt.setInt(1, member.getID());//w w  w.ja v  a 2 s  . c o  m
    pt.setString(2, member.getName());
    pt.setString(3, member.getEmail());
    pt.setString(4, member.getPrivilege());
    pt.setBoolean(5, member.isIsStaff());
    pt.executeUpdate();
}

From source file:DeliverWork.java

private static String saveIntoWeed(RemoteFile remoteFile, String projectId)
        throws SQLException, UnsupportedEncodingException {
    String url = "http://59.215.226.174/WebDiskServerDemo/doc?doc_id="
            + URLEncoder.encode(remoteFile.getFileId(), "utf-8");
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    String res = "";
    try {/*from  ww w.  ja  v  a2s  .c  o m*/
        httpClient = HttpClients.createSystem();
        // http(get?)
        HttpGet httpget = new HttpGet(url);
        response = httpClient.execute(httpget);
        HttpEntity result = response.getEntity();
        String fileName = remoteFile.getFileName();
        FileHandleStatus fileHandleStatus = getFileTemplate().saveFileByStream(fileName,
                new ByteArrayInputStream(EntityUtils.toByteArray(result)));
        System.out.println(fileHandleStatus);
        File file = new File();
        if (result != null && result.getContentType() != null && result.getContentType().getValue() != null) {
            file.setContentType(result.getContentType().getValue());
        } else {
            file.setContentType("application/error");
        }
        file.setDataId(Integer.parseInt(projectId));
        file.setName(fileName);
        if (fileName.contains(".bmp") || fileName.contains(".jpg") || fileName.contains(".jpeg")
                || fileName.contains(".png") || fileName.contains(".gif")) {
            file.setType(1);
        } else if (fileName.contains(".doc") || fileName.contains(".docx")) {
            file.setType(2);
        } else if (fileName.contains(".xlsx") || fileName.contains("xls")) {
            file.setType(3);
        } else if (fileName.contains(".pdf")) {
            file.setType(4);
        } else {
            file.setType(5);
        }
        String accessUrl = "/" + fileHandleStatus.getFileId().replaceAll(",", "/").concat("/").concat(fileName);
        file.setUrl(accessUrl);
        file.setSize(fileHandleStatus.getSize());
        file.setPostTime(new java.util.Date());
        file.setStatus(0);
        file.setEnumValue(findFileType(remoteFile.getFileType()));
        file.setResources(1);
        //            JdbcFactory jdbcFactoryChanye=new JdbcFactory("jdbc:mysql://127.0.0.1:3306/fp_guimin?useUnicode=true&characterEncoding=UTF-8","root","111111","com.mysql.jdbc.Driver");
        //            Connection connection = jdbcFactoryChanye.getConnection();
        DatabaseMetaData dmd = connection.getMetaData();
        PreparedStatement ps = connection.prepareStatement(insertFile, new String[] { "ID" });
        ps.setString(1, sdf.format(file.getPostTime()));
        ps.setInt(2, file.getType());
        ps.setString(3, file.getEnumValue());
        ps.setInt(4, file.getDataId());
        ps.setString(5, file.getUrl());
        ps.setString(6, file.getName());
        ps.setString(7, file.getContentType());
        ps.setLong(8, file.getSize());
        ps.setInt(9, file.getStatus());
        ps.setInt(10, file.getResources());
        ps.executeUpdate();
        if (dmd.supportsGetGeneratedKeys()) {
            ResultSet rs = ps.getGeneratedKeys();
            while (rs.next()) {
                System.out.println(rs.getLong(1));
            }
        }
        ps.close();
        res = "success";
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        res = e.getMessage();
    } catch (IOException e) {
        e.printStackTrace();
        res = e.getMessage();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return res;
}

From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

public static void updateDateRepositoryImport(Connection con, String date) throws SQLException {
    PreparedStatement pStmt = con.prepareStatement(
            "delete from PM_DP_CONFIG where CLE = ? " + "insert into PM_DP_CONFIG (CLE, VALEUR) values (?, ?)");
    try {/*  w ww. j a va 2 s.c o m*/
        pStmt.setString(1, DataProcessConstants.KEY_DATE_LAST_IMPORT_REPOSITORY);
        pStmt.setString(2, DataProcessConstants.KEY_DATE_LAST_IMPORT_REPOSITORY);
        pStmt.setString(3, date);
        pStmt.executeUpdate();
    } finally {
        pStmt.close();
    }
}

From source file:edu.jhu.pha.vospace.oauth.MySQLOAuthProvider2.java

public static synchronized Token getAccessToken(final String tokenStr) {
    Token tokenObj = DbPoolServlet.goSql("Get oauth token",
            "select access_token, token_secret, consumer_key, callback_url, identity, container_name, accessor_write_permission, share_id "
                    + "from oauth_accessors "
                    + "join oauth_consumers on oauth_consumers.consumer_id = oauth_accessors.consumer_id "
                    + "left outer join containers on containers.container_id = oauth_accessors.container_id "
                    + "left outer join container_shares on oauth_accessors.share_key = container_shares.share_key "
                    + "left outer join users on users.user_id = containers.user_id "
                    + "left outer join user_identities on users.user_id = user_identities.user_id "
                    + "where access_token = ? limit 1",
            new SqlWorker<Token>() {
                @Override//from   w w w . ja va  2  s. c  o  m
                public Token go(Connection conn, PreparedStatement stmt) throws SQLException {
                    Token token = null;

                    stmt.setString(1, tokenStr);
                    ResultSet rs = stmt.executeQuery();
                    if (rs.next()) {

                        Set<USER_ROLES> rolesSet = new HashSet<USER_ROLES>();
                        if (null == rs.getString("share_id")) {
                            rolesSet.add(USER_ROLES.user);
                        } else {
                            if (rs.getBoolean("accessor_write_permission"))
                                rolesSet.add(USER_ROLES.rwshareuser);
                            else
                                rolesSet.add(USER_ROLES.roshareuser);
                        }
                        token = new Token(rs.getString("access_token"), rs.getString("token_secret"),
                                rs.getString("consumer_key"), rs.getString("callback_url"),
                                new SciDriveUser(rs.getString("identity"), rs.getString("container_name"),
                                        rs.getBoolean("accessor_write_permission")),
                                rolesSet, new MultivaluedMapImpl());

                    }
                    return token;
                }
            });

    return tokenObj;
}

From source file:FacultyAdvisement.StudentRepository.java

public static void update(DataSource ds, Student student) throws SQLException {
    String studentSQL = "UPDATE STUDENT SET STUID = ?, FIRSTNAME = ?, LASTNAME = ?, MAJORCODE = ?, PHONE = ? WHERE EMAIL = ?";

    if (ds == null) {
        throw new SQLException("ds is null; Can't get data source");
    }/*from www.  j a v  a2s.c  om*/

    Connection conn = ds.getConnection();

    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }

    try {
        //Student Information
        PreparedStatement sqlStatement = conn.prepareStatement(studentSQL);
        sqlStatement.setString(1, student.getId());
        sqlStatement.setString(2, student.getFirstName());
        sqlStatement.setString(3, student.getLastName());
        sqlStatement.setString(4, student.getMajorCode());
        sqlStatement.setString(5, student.getPhoneNumber());
        sqlStatement.setString(6, student.getUsername());

        sqlStatement.executeUpdate();

    } finally {
        conn.close();
    }
}

From source file:com.wso2telco.util.DbUtil.java

public static void incrementSuccessPinAttempts(String sessionId) throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update multiplepasswords set attempts=attempts +1 where ussdsessionid = ?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, sessionId);
    ps.execute();//  w w  w .ja  v  a2s .  c om

    if (connection != null) {
        connection.close();
    }
}

From source file:org.red5.server.plugin.admin.dao.UserDAO.java

public static UserDetails getUser(String username) {
    UserDetails details = null;/*  w  w  w. j  a v  a  2s.  c o m*/
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        // JDBC stuff
        DataSource ds = UserDatabase.getDataSource();

        conn = ds.getConnection();
        //make a statement
        stmt = conn.prepareStatement("SELECT * FROM APPUSER WHERE username = ?");
        stmt.setString(1, username);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            log.debug("User found");
            details = new UserDetails();
            details.setEnabled("enabled".equals(rs.getString("enabled")));
            details.setPassword(rs.getString("password"));
            details.setUserid(rs.getInt("userid"));
            details.setUsername(rs.getString("username"));
            //
            rs.close();
            //get role            
            stmt = conn.prepareStatement("SELECT authority FROM APPROLE WHERE username = ?");
            stmt.setString(1, username);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Collection<? extends GrantedAuthority> authorities;
                //                  authorities.addAll((Collection<?>) new GrantedAuthorityImpl(rs.getString("authority")));
                //                  details.setAuthorities(authorities);
                //
                //if (daoAuthenticationProvider != null) {
                //User usr = new User(username, details.getPassword(), true, true, true, true, authorities);
                //daoAuthenticationProvider.getUserCache().putUserInCache(usr);               
                //}
            }
        }
        rs.close();
    } catch (Exception e) {
        log.error("Error connecting to db", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return details;
}

From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

public static void insertRepository(Connection con, int repositoryId, String repositoryName)
        throws SQLException {
    PreparedStatement pStmt = con
            .prepareStatement("insert into PM_REPOSITORY (REPOSITORY_ID, REPOSITORY_NAME) values (?, ?)");
    try {//from w w w .  j  a v  a2  s.  c  o m
        pStmt.setInt(1, repositoryId);
        pStmt.setString(2, repositoryName);
        pStmt.executeUpdate();
    } finally {
        pStmt.close();
    }
}

From source file:fll.db.NonNumericNominees.java

/**
 * Clear the nominees for the specified category at the tournament.
 * //from w ww.j a v  a 2 s . co m
 * @param connection database connection
 * @param tournamentId the tournament
 * @param category the category
 * @throws SQLException
 */
public static void clearNominees(final Connection connection, final int tournamentId, final String category)
        throws SQLException {
    PreparedStatement delete = null;
    try {
        delete = connection.prepareStatement("DELETE FROM non_numeric_nominees"//
                + " WHERE tournament = ?"//
                + " AND category = ?");
        delete.setInt(1, tournamentId);
        delete.setString(2, category);
        delete.executeUpdate();
    } finally {
        SQLFunctions.close(delete);
    }
}

From source file:FacultyAdvisement.StudentRepository.java

public static Student read(DataSource ds, String key) throws SQLException {
    String studentSQL = "SElECT * FROM STUDENT JOIN USERTABLE on EMAIL = USERNAME WHERE EMAIL = ?";
    Student student = new Student();

    if (ds == null) {
        throw new SQLException("ds is null; Can't get data source");
    }/*from  ww w.j  a  v a  2s.c  o m*/

    Connection conn = ds.getConnection();

    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }

    try {

        PreparedStatement sqlStatement = conn.prepareStatement(studentSQL);

        sqlStatement.setString(1, key);

        ResultSet result = sqlStatement.executeQuery();
        while (result.next()) {
            student.setId(result.getString("STUID"));
            student.setFirstName(result.getString("firstname"));
            student.setLastName(result.getString("lastname"));
            student.setMajorCode(result.getString("majorcode"));
            student.setPhoneNumber(result.getString("phone"));
            student.setUsername(key);
            student.setPassword(result.getString("password"));
        }

    } finally {
        conn.close();
    }

    return student;
}