List of usage examples for java.sql PreparedStatement setByte
void setByte(int parameterIndex, byte x) throws SQLException;
byte
value. From source file:Main.java
public static void main(String[] argv) throws Exception { Connection con = null;/* www . j ava 2 s . co 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 w w. ja v a2 s.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[] 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 . ja v a 2 s . c om 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);//w w w . j a v a 2 s. 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 ww w . j a va2 s . 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.rhq.plugins.database.DatabasePluginUtil.java
private static void bindParameters(PreparedStatement statement, Object... parameters) throws SQLException { int i = 1;/*w ww .ja v a 2s.c o 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:org.apache.phoenix.coprocessor.TaskRegionObserver.java
public static void addTask(PhoenixConnection conn, TaskType taskType, String tenantId, String schemaName, String tableName, boolean accessCheckEnabled) throws IOException { PreparedStatement stmt = null; try {/*from w ww .j a va 2 s .c o m*/ stmt = conn.prepareStatement("UPSERT INTO " + PhoenixDatabaseMetaData.SYSTEM_TASK_NAME + " ( " + PhoenixDatabaseMetaData.TASK_TYPE + ", " + PhoenixDatabaseMetaData.TENANT_ID + ", " + PhoenixDatabaseMetaData.TABLE_SCHEM + ", " + PhoenixDatabaseMetaData.TABLE_NAME + " ) VALUES(?,?,?,?)"); stmt.setByte(1, taskType.getSerializedValue()); if (tenantId != null) { stmt.setString(2, tenantId); } else { stmt.setNull(2, Types.VARCHAR); } if (schemaName != null) { stmt.setString(3, schemaName); } else { stmt.setNull(3, Types.VARCHAR); } stmt.setString(4, tableName); } catch (SQLException e) { throw new IOException(e); } mutateSystemTaskTable(conn, stmt, accessCheckEnabled); }
From source file:org.apache.phoenix.coprocessor.TaskRegionObserver.java
public static void deleteTask(PhoenixConnection conn, TaskType taskType, Timestamp ts, String tenantId, String schemaName, String tableName, boolean accessCheckEnabled) throws IOException { PreparedStatement stmt = null; try {//w ww . ja v a 2 s .c o m stmt = conn.prepareStatement("DELETE FROM " + PhoenixDatabaseMetaData.SYSTEM_TASK_NAME + " WHERE " + PhoenixDatabaseMetaData.TASK_TYPE + " = ? AND " + PhoenixDatabaseMetaData.TASK_TS + " = ? AND " + PhoenixDatabaseMetaData.TENANT_ID + (tenantId == null ? " IS NULL " : " = '" + tenantId + "'") + " AND " + PhoenixDatabaseMetaData.TABLE_SCHEM + (schemaName == null ? " IS NULL " : " = '" + schemaName + "'") + " AND " + PhoenixDatabaseMetaData.TABLE_NAME + " = ?"); stmt.setByte(1, taskType.getSerializedValue()); stmt.setTimestamp(2, ts); stmt.setString(3, tableName); } catch (SQLException e) { throw new IOException(e); } mutateSystemTaskTable(conn, stmt, accessCheckEnabled); }
From source file:com.wabacus.system.datatype.ByteType.java
public void setPreparedStatementValue(int iindex, String value, PreparedStatement pstmt, AbsDatabaseType dbtype) throws SQLException { log.debug("setByte(" + iindex + "," + value + ")"); pstmt.setByte(iindex, (Byte) label2value(value)); }
From source file:com.jagornet.dhcp.db.JdbcIdentityAssocDAO.java
public void update(final IdentityAssoc ia) { String updateQuery = "update identityassoc" + " set duid=?," + " iatype=?," + " iaid=?," + " state=?" + " where id=?"; getJdbcTemplate().update(updateQuery, new PreparedStatementSetter() { @Override/*w w w.j a v a 2s. c om*/ public void setValues(PreparedStatement ps) throws SQLException { ps.setBytes(1, ia.getDuid()); ps.setByte(2, ia.getIatype()); ps.setLong(3, ia.getIaid()); ps.setByte(4, ia.getState()); ps.setLong(5, ia.getId()); } }); }