Example usage for java.sql PreparedStatement setEscapeProcessing

List of usage examples for java.sql PreparedStatement setEscapeProcessing

Introduction

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

Prototype

void setEscapeProcessing(boolean enable) throws SQLException;

Source Link

Document

Sets escape processing on or off.

Usage

From source file:cascading.jdbc.db.DBOutputFormat.java

/** {@inheritDoc} */
public RecordWriter<K, V> getRecordWriter(FileSystem filesystem, JobConf job, String name,
        Progressable progress) throws IOException {
    DBConfiguration dbConf = new DBConfiguration(job);

    String tableName = dbConf.getOutputTableName();
    String[] fieldNames = dbConf.getOutputFieldNames();
    String[] updateNames = dbConf.getOutputUpdateFieldNames();
    int batchStatements = dbConf.getBatchStatementsNum();

    Connection connection = dbConf.getConnection();

    configureConnection(connection);//from  w  ww .j  av  a 2s  . c o  m

    String sqlInsert = constructInsertQuery(tableName, fieldNames);
    PreparedStatement insertPreparedStatement;

    try {
        insertPreparedStatement = connection.prepareStatement(sqlInsert);
        insertPreparedStatement.setEscapeProcessing(true); // should be on by default
    } catch (SQLException exception) {
        throw new IOException("unable to create statement for: " + sqlInsert, exception);
    }

    String sqlUpdate = updateNames != null ? constructUpdateQuery(tableName, fieldNames, updateNames) : null;
    PreparedStatement updatePreparedStatement = null;

    try {
        updatePreparedStatement = sqlUpdate != null ? connection.prepareStatement(sqlUpdate) : null;
    } catch (SQLException exception) {
        throw new IOException("unable to create statement for: " + sqlUpdate, exception);
    }

    return new DBRecordWriter(connection, insertPreparedStatement, updatePreparedStatement, batchStatements);
}

From source file:com.twitter.maple.jdbc.db.DBOutputFormat.java

/** {@inheritDoc} */
public RecordWriter<K, V> getRecordWriter(FileSystem filesystem, JobConf job, String name,
        Progressable progress) throws IOException {
    DBConfiguration dbConf = new DBConfiguration(job);

    String tableName = dbConf.getOutputTableName();
    String[] fieldNames = dbConf.getOutputFieldNames();
    String[] updateNames = dbConf.getOutputUpdateFieldNames();
    int batchStatements = dbConf.getBatchStatementsNum();
    boolean replaceOnInsert = dbConf.getReplaceOnInsert();

    Connection connection = dbConf.getConnection();

    configureConnection(connection);/*ww  w.j  av a  2 s. c  o  m*/

    String sqlInsert = constructInsertQuery(tableName, fieldNames, replaceOnInsert);
    PreparedStatement insertPreparedStatement;

    try {
        insertPreparedStatement = connection.prepareStatement(sqlInsert);
        insertPreparedStatement.setEscapeProcessing(true); // should be on by default
    } catch (SQLException exception) {
        throw new IOException("unable to create statement for: " + sqlInsert, exception);
    }

    String sqlUpdate = updateNames != null ? constructUpdateQuery(tableName, fieldNames, updateNames) : null;
    PreparedStatement updatePreparedStatement = null;

    try {
        updatePreparedStatement = sqlUpdate != null ? connection.prepareStatement(sqlUpdate) : null;
    } catch (SQLException exception) {
        throw new IOException("unable to create statement for: " + sqlUpdate, exception);
    }

    if (insertPreparedStatement != null) {
        LOG.info("Executing insert statement:\n " + sqlInsert);
    }
    if (updatePreparedStatement != null) {
        LOG.info("Executing update statement:\n " + sqlUpdate);
    }
    return new DBRecordWriter(connection, insertPreparedStatement, updatePreparedStatement, batchStatements);
}

From source file:com.example.georg.theupub.AdministratorActivity.java

public boolean addPointss(int numpoints) {
    int currentpoints = 0;
    Connection conn = null;//from   w  ww. ja v a  2s  . c om
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String dbURL = "jdbc:jtds:sqlserver://apollo.in.cs.ucy.ac.cy:1433";
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        return false;
    }
    Properties properties = new Properties();
    properties.put("user", "upub");
    properties.put("password", "XuZ3drup");
    properties.put("databaseName", "upub");
    try {
        conn = DriverManager.getConnection(dbURL, properties);
    } catch (SQLException e) {
        return false;
    }
    String SQL = "Select * From [dbo].[User] where ID='" + re + "'";
    Statement stmt = null;
    try {
        stmt = conn.createStatement();
    } catch (SQLException e) {
        return false;
    }
    ResultSet rs = null;
    try {
        rs = stmt.executeQuery(SQL);
    } catch (SQLException e) {
        return false;
    }
    try {
        if (rs.next()) {
            System.out.print(rs.getInt(7));
            currentpoints = rs.getInt(7);
        }
    } catch (SQLException e) {
        return false;
    }
    String pro = "EXEC dbo.addpoints ?,?";
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(pro);
        ps.setEscapeProcessing(true);
        ps.setInt(1, Integer.parseInt(re));
        ps.setInt(2, currentpoints + numpoints);
        ps.execute();
    } catch (SQLException e) {
        return false;
    }
    return true;

}

From source file:com.example.georg.theupub.AdministratorActivity.java

public boolean RemovePointss(int numpoints) {

    int currentpoints = 0;
    Connection conn = null;/*from  w ww .jav a  2s  .co  m*/
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String dbURL = "jdbc:jtds:sqlserver://apollo.in.cs.ucy.ac.cy:1433";
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        return false;
    }
    Properties properties = new Properties();
    properties.put("user", "upub");
    properties.put("password", "XuZ3drup");
    properties.put("databaseName", "upub");
    try {
        conn = DriverManager.getConnection(dbURL, properties);
    } catch (SQLException e) {
        return false;
    }
    String SQL = "Select * From [dbo].[User] where ID='" + re + "'";
    Statement stmt = null;
    try {
        stmt = conn.createStatement();
    } catch (SQLException e) {
        return false;
    }
    ResultSet rs = null;
    try {
        rs = stmt.executeQuery(SQL);
    } catch (SQLException e) {
        return false;
    }
    try {
        if (rs.next()) {
            System.out.print(rs.getInt(7));
            currentpoints = rs.getInt(7);
        }
    } catch (SQLException e) {
        return false;
    }
    String pro = "EXEC dbo.addpoints ?,?";
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(pro);
        ps.setEscapeProcessing(true);
        ps.setInt(1, Integer.parseInt(re));
        ps.setInt(2, currentpoints - numpoints);
        ps.execute();
    } catch (SQLException e) {
        return false;
    }

    return true;
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRdbmsClientImpl.java

/**
 * This method gets the API usage data per application
 *
 * @param tableName name of the required table in the database
 * @param keyString concatenated key set of applications
 * @return a collection containing the data related to per App API usage
 * @throws APIMgtUsageQueryServiceClientException if an error occurs while querying the database
 *//* w ww.  j  a  va  2s  .c o m*/
private List<PerAppApiCountDTO> getPerAppAPIUsageData(String tableName, String keyString, String fromDate,
        String toDate, int limit) throws APIMgtUsageQueryServiceClientException {

    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    List<PerAppApiCountDTO> perAppUsageDataList = new ArrayList<PerAppApiCountDTO>();
    try {
        connection = dataSource.getConnection();
        String query;
        //check whether table exist first
        if (isTableExist(tableName, connection)) {
            //ignoring sql injection for keyString since it construct locally and no public access
            if (connection.getMetaData().getDatabaseProductName().contains("DB2")) {
                query = "SELECT " + APIUsageStatisticsClientConstants.API + ","
                        + APIUsageStatisticsClientConstants.API_VERSION + ","
                        + APIUsageStatisticsClientConstants.VERSION + ","
                        + APIUsageStatisticsClientConstants.API_PUBLISHER + ","
                        + APIUsageStatisticsClientConstants.CONSUMERKEY + ","
                        + APIUsageStatisticsClientConstants.USER_ID + ","
                        + APIUsageStatisticsClientConstants.CONTEXT + ","
                        + APIUsageStatisticsClientConstants.MAX_REQUEST_TIME + ","
                        + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ","
                        + APIUsageStatisticsClientConstants.HOST_NAME + ","
                        + APIUsageStatisticsClientConstants.YEAR + "," + APIUsageStatisticsClientConstants.MONTH
                        + "," + APIUsageStatisticsClientConstants.DAY + ","
                        + APIUsageStatisticsClientConstants.TIME + ",SUM("
                        + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") AS total_calls " + " FROM "
                        + APIUsageStatisticsClientConstants.API_REQUEST_SUMMARY + " WHERE "
                        + APIUsageStatisticsClientConstants.CONSUMERKEY + " IN (" + keyString + ") AND "
                        + APIUsageStatisticsClientConstants.TIME + " BETWEEN ? AND ?  GROUP BY "
                        + APIUsageStatisticsClientConstants.API + ","
                        + APIUsageStatisticsClientConstants.API_VERSION + ","
                        + APIUsageStatisticsClientConstants.VERSION + ","
                        + APIUsageStatisticsClientConstants.API_PUBLISHER + ","
                        + APIUsageStatisticsClientConstants.CONSUMERKEY + ","
                        + APIUsageStatisticsClientConstants.USER_ID + ","
                        + APIUsageStatisticsClientConstants.CONTEXT + ","
                        + APIUsageStatisticsClientConstants.MAX_REQUEST_TIME + ","
                        + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ","
                        + APIUsageStatisticsClientConstants.HOST_NAME + ","
                        + APIUsageStatisticsClientConstants.YEAR + "," + APIUsageStatisticsClientConstants.MONTH
                        + "," + APIUsageStatisticsClientConstants.DAY + ","
                        + APIUsageStatisticsClientConstants.TIME;
            } else {
                query = "SELECT " + APIUsageStatisticsClientConstants.API + ","
                        + APIUsageStatisticsClientConstants.API_PUBLISHER + ","
                        + APIUsageStatisticsClientConstants.CONSUMERKEY + "," + " SUM("
                        + APIUsageStatisticsClientConstants.TOTAL_REQUEST_COUNT + ") AS total_calls " + " FROM "
                        + APIUsageStatisticsClientConstants.API_REQUEST_SUMMARY + " WHERE "
                        + APIUsageStatisticsClientConstants.CONSUMERKEY + " IN (" + keyString + ")  AND "
                        + APIUsageStatisticsClientConstants.TIME + " BETWEEN ? AND ?  GROUP BY "
                        + APIUsageStatisticsClientConstants.API + ","
                        + APIUsageStatisticsClientConstants.API_PUBLISHER + ","
                        + APIUsageStatisticsClientConstants.CONSUMERKEY;
            }

            statement = connection.prepareStatement(query);
            int index = 1;
            statement.setEscapeProcessing(true);
            statement.setString(index++, fromDate);
            statement.setString(index, toDate);
            resultSet = statement.executeQuery();
            PerAppApiCountDTO apiUsageDTO;
            while (resultSet.next()) {
                String apiName = resultSet.getString(APIUsageStatisticsClientConstants.API);
                String publisher = resultSet.getString(APIUsageStatisticsClientConstants.API_PUBLISHER);
                apiName = apiName + " (" + publisher + ")";
                long requestCount = resultSet.getLong("total_calls");
                String consumerKey = resultSet.getString(APIUsageStatisticsClientConstants.CONSUMERKEY);
                String appName = subscriberAppsMap.get(consumerKey);
                boolean found = false;
                for (PerAppApiCountDTO dto : perAppUsageDataList) {
                    if (dto.getAppName().equals(appName)) {
                        dto.addToApiCountArray(apiName, requestCount);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    apiUsageDTO = new PerAppApiCountDTO();
                    apiUsageDTO.setAppName(appName);
                    apiUsageDTO.addToApiCountArray(apiName, requestCount);
                    perAppUsageDataList.add(apiUsageDTO);
                }
            }
        }
    } catch (SQLException e) {
        throw new APIMgtUsageQueryServiceClientException(
                "Error occurred while querying per App usage data from JDBC database", e);
    } finally {
        closeDatabaseLinks(resultSet, statement, connection);
    }
    return perAppUsageDataList;
}