Example usage for java.sql PreparedStatement setShort

List of usage examples for java.sql PreparedStatement setShort

Introduction

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

Prototype

void setShort(int parameterIndex, short x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java short value.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;//from   w  ww .  j a  v a  2s  .c  o  m

    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT datatypes VALUES(?,?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setByte(1, (byte) 5);
    prest.setShort(2, (short) 65);
    prest.setLong(3, (long) 254);
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affected)");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, id2 tinyint, id3 smallint, id4 bigint, id5 real);");
    String sql = "INSERT INTO survey (id2,id3,id4,id5) VALUES(?,?,?,?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    byte b = 1;/*from   w  ww  .j  av  a  2  s  . co m*/
    short s = 2;
    pstmt.setByte(1, b);
    pstmt.setShort(2, s);
    pstmt.setInt(3, 3);
    pstmt.setLong(4, 4L);
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getString(2));
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, id2 tinyint, id3 smallint, id4 bigint, id5 real);");

    String sql = "INSERT INTO survey (id2,id3,id4,id5) VALUES(?,?,?,?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    byte b = 1;/* w  w  w  .j a v  a  2s  .c  o m*/
    short s = 2;
    pstmt.setByte(1, b);
    pstmt.setShort(2, s);
    pstmt.setInt(3, 3);
    pstmt.setLong(4, 4L);
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getString(2));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   w  w w .  j a  va2s.c om

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    String sql = "INSERT INTO mysql_all_table(" + "col_boolean," + "col_byte," + "col_short," + "col_int,"
            + "col_long," + "col_float," + "col_double," + "col_bigdecimal," + "col_string," + "col_date,"
            + "col_time," + "col_timestamp," + "col_asciistream," + "col_binarystream," + "col_blob) "
            + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    pstmt.setBoolean(1, true);
    pstmt.setByte(2, (byte) 123);
    pstmt.setShort(3, (short) 123);
    pstmt.setInt(4, 123);
    pstmt.setLong(5, 123L);
    pstmt.setFloat(6, 1.23F);
    pstmt.setDouble(7, 1.23D);
    pstmt.setBigDecimal(8, new BigDecimal(1.23));
    pstmt.setString(9, "a string");
    pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis()));
    pstmt.setTime(11, new Time(System.currentTimeMillis()));
    pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis()));

    File file = new File("infilename1");
    FileInputStream is = new FileInputStream(file);
    pstmt.setAsciiStream(13, is, (int) file.length());

    file = new File("infilename2");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(14, is, (int) file.length());

    file = new File("infilename3");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(15, is, (int) file.length());

    pstmt.executeUpdate();
}

From source file:DemoPreparedStatementSetIntegers.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    byte byteValue = 1;
    short shortValue = 1;
    int intValue = 12345;
    long longValue = 100000000L;

    Connection conn = null;//from   w  ww .  j a  va  2s. c om
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into integer_table(id, byte_column, "
                + "short_column, int_column, long_column) values(?, ?, ?, ?, ?)";

        // create PrepareStatement object
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setByte(2, byteValue);
        pstmt.setShort(3, shortValue);
        pstmt.setInt(4, intValue);
        pstmt.setLong(5, longValue);

        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:org.mapbuilderfreq.FrequencyMapClient.java

public static void setRefreshStatus(int status) {
    try {//from   w  ww  .j  a va  2  s.c  o m
        String updateQuery = "Update public.\"SysParams\" SET \"RefreshStatus\" = ? WHERE \"Id\" = 2;";
        PreparedStatement preparedStatement = db.prepareStatement(updateQuery);
        preparedStatement.setShort(1, (short) status);
        preparedStatement.executeUpdate();

    } catch (SQLException ex) {
        System.err.println("RefreshStatus updating failed...");
        System.err.println(ex);
        System.exit(0);
    }
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * set a java.lang.Integer value in the given preparedStatement object, or set it to null if the
 * given Integer is null//from  ww  w  .java 2  s  .co  m
 *
 * @param stmt
 * @param value
 * @param index
 * @throws SQLException
 */
public static void setShort(PreparedStatement stmt, Short value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.SMALLINT);
        return;
    }
    stmt.setShort(index, value);
}

From source file:org.mskcc.cbio.portal.dao.DaoCnaEvent.java

/**
 * Add new event directly and return the auto increment value.
 * // w w  w  . j  a  v a2s. co m
 * @param cnaEvent
 * @return
 * @throws DaoException 
 */
private static long addCnaEventDirectly(CnaEvent cnaEvent) throws DaoException {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = JdbcUtil.getDbConnection(DaoClinicalAttribute.class);
        pstmt = con.prepareStatement(
                "INSERT INTO cna_event (" + "`ENTREZ_GENE_ID`," + "`ALTERATION` )" + " VALUES(?,?)",
                Statement.RETURN_GENERATED_KEYS);
        pstmt.setLong(1, cnaEvent.getEntrezGeneId());
        pstmt.setShort(2, cnaEvent.getAlteration().getCode());
        pstmt.executeUpdate();
        rs = pstmt.getGeneratedKeys();
        rs.next();
        long newId = rs.getLong(1);
        return newId;
    } catch (SQLException e) {
        throw new DaoException(e);
    } finally {
        JdbcUtil.closeAll(DaoClinicalAttribute.class, con, pstmt, rs);
    }
}

From source file:org.rhq.plugins.database.DatabasePluginUtil.java

private static void bindParameters(PreparedStatement statement, Object... parameters) throws SQLException {
    int i = 1;/*from  w  ww  .j  a v a 2  s. co m*/
    for (Object p : parameters) {
        if (p instanceof String) {
            statement.setString(i++, (String) p);
        } else if (p instanceof Byte) {
            statement.setByte(i++, (Byte) p);
        } else if (p instanceof Short) {
            statement.setShort(i++, (Short) p);
        } else if (p instanceof Integer) {
            statement.setInt(i++, (Integer) p);
        } else if (p instanceof Long) {
            statement.setLong(i++, (Long) p);
        } else if (p instanceof Float) {
            statement.setFloat(i++, (Float) p);
        } else if (p instanceof Double) {
            statement.setDouble(i++, (Double) p);
        } else {
            statement.setObject(i++, p);
        }
    }
}

From source file:com.srotya.tau.wraith.silo.sql.TestSQLRulesStore.java

@Before
public void before() throws SQLException, IOException {
    System.setProperty("derby.stream.error.field", DerbyUtil.class.getCanonicalName() + ".DEV_NULL");
    File db = new File(TARGET_RULES_DB);
    if (db.exists()) {
        System.out.println("Deleting database");
        FileUtils.deleteDirectory(db);//from w w  w .  j a v a 2s .c  o m
    }
    String createTable = "create table testRules(" + SQLRulesStore.COLUMN_RULE_ID + " smallint primary key,"
            + SQLRulesStore.COLUMN_RULE_GROUP_ID + " varchar(100)," + SQLRulesStore.COLUMN_RULE_CONTENT
            + " varchar(3000))";
    runSQL(CONNECTION_STRING, createTable);

    Condition condition = new JavaRegexCondition("tst", "\\d+");
    Action action = new TemplatedAlertAction((short) 2, (short) 2);
    Rule testRule = new SimpleRule((short) 1233, "testRule", true, condition, action);

    Connection conn = DriverManager.getConnection(CONNECTION_STRING);
    PreparedStatement insert = conn.prepareStatement("insert into testRules values(?, ?, ?)");
    insert.setShort(1, testRule.getRuleId());
    insert.setString(2, "all");
    insert.setString(3, RuleSerializer.serializeRuleToJSONString(testRule, false));
    insert.execute();
    conn.close();
}