Example usage for java.sql Types VARCHAR

List of usage examples for java.sql Types VARCHAR

Introduction

In this page you can find the example usage for java.sql Types VARCHAR.

Prototype

int VARCHAR

To view the source code for java.sql Types VARCHAR.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type VARCHAR.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from   ww w. j  av a  2 s  . c o m*/

    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);
    CallableStatement cs = connection.prepareCall("{? = call myfuncin(?)}");

    cs.registerOutParameter(1, Types.VARCHAR);

    // Set the value for the IN parameter
    cs.setString(2, "a string");

    // Execute and retrieve the returned value
    cs.execute();
    String retValue = cs.getString(1);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from  www.j a  v  a 2  s.  c o m*/

    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);
    CallableStatement cs = connection.prepareCall("{call myprocout(?)}");

    // Register the type of the OUT parameter
    cs.registerOutParameter(1, Types.VARCHAR);

    cs.execute();
    String outParam = cs.getString(1); // OUT parameter
    System.out.println(outParam);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//w ww  .  j  ava2s  .  c  o m

    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);
    CallableStatement cs = connection.prepareCall("{call myprocinout(?)}");

    // Register the type of the IN/OUT parameter
    cs.registerOutParameter(1, Types.VARCHAR);

    // Set the value for the IN/OUT parameter
    cs.setString(1, "a string");

    // Execute the stored procedure and retrieve the IN/OUT value
    cs.execute();
    String outParam = cs.getString(1); // OUT parameter
    System.out.println(outParam);

}

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  v  a  2 s  .  c o m

    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);
    CallableStatement cs = connection.prepareCall("{? = call myfuncout(?)}");

    // Register the types of the return value and OUT parameter
    cs.registerOutParameter(1, Types.VARCHAR);
    cs.registerOutParameter(2, Types.VARCHAR);

    // Execute and retrieve the returned values
    cs.execute();
    String retValue = cs.getString(1); // return value
    String outParam = cs.getString(2); // OUT parameter
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*  ww w.  j ava  2s. c  o m*/

    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);
    CallableStatement cs = connection.prepareCall("{? = call myfuncinout(?)}");

    // Register the types of the return value and OUT parameter
    cs.registerOutParameter(1, Types.VARCHAR);
    cs.registerOutParameter(2, Types.VARCHAR);

    // Set the value for the IN/OUT parameter
    cs.setString(2, "a string");

    // Execute and retrieve the returned values
    cs.execute();
    String retValue = cs.getString(1); // return value
    String outParam = cs.getString(2); // IN/OUT parameter

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system",
            "manager");
    String plsql = " declare " + "    p_id varchar2(20) := null; " + "    l_rc sys_refcursor;" + " begin "
            + "    p_id := ?; " + "    ? := 'input parameter was = ' || p_id;" + "    open l_rc for "
            + "        select 1 id, 'abc' name from dual; " + "    ? := l_rc;" + " end;";

    CallableStatement cs = c.prepareCall(plsql);
    cs.setString(1, "12345");
    cs.registerOutParameter(2, Types.VARCHAR);
    //  cs.registerOutParameter(3, OracleTypes.CURSOR);

    cs.execute();//from w ww  . j  ava  2 s. c  om

    System.out.println("Result = " + cs.getObject(2));

    ResultSet cursorResultSet = (ResultSet) cs.getObject(3);
    while (cursorResultSet.next()) {
        System.out.println(cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2));
    }
    cs.close();
    c.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("create view surveyView as (select * from survey);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    CachedRowSet crs = null;/*w  w w  .j a  v a2s  .c  o  m*/
    RowSetMetaData rsMD = new RowSetMetaDataImpl();
    rsMD.setColumnCount(2);
    rsMD.setColumnName(1, "id");
    rsMD.setColumnType(1, Types.VARCHAR);
    rsMD.setColumnName(2, "name");
    rsMD.setColumnType(2, Types.VARCHAR);
    // sets the designated column's table name, if any, to the given String.
    rsMD.setTableName(1, "survey");
    rsMD.setTableName(2, "survey");

    // use a custom made RowSetMetaData object for CachedRowSet object
    crs = new CachedRowSetImpl();
    crs.setMetaData(rsMD);

    crs.moveToInsertRow();
    crs.updateString(1, "1111");
    crs.updateString(2, "alex");
    crs.insertRow();

    crs.moveToInsertRow();
    crs.updateString(1, "2222");
    crs.updateString(2, "jane");
    crs.insertRow();
    crs.moveToCurrentRow();

    crs.acceptChanges(conn);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("create view surveyView as (select * from survey);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    CachedRowSet crs = null;/* w  w  w. j av  a 2s  .co  m*/
    RowSetMetaData rsMD = new RowSetMetaDataImpl();
    rsMD.setColumnCount(2);
    rsMD.setColumnName(1, "id");
    rsMD.setColumnType(1, Types.VARCHAR);
    rsMD.setColumnName(2, "name");
    rsMD.setColumnType(2, Types.VARCHAR);
    // sets the designated column's table name, if any, to the given String.
    rsMD.setTableName(1, "survey");
    rsMD.setTableName(2, "survey");

    // use a custom made RowSetMetaData object for CachedRowSet object
    crs = new CachedRowSetImpl();
    crs.setMetaData(rsMD);

    crs.moveToInsertRow();
    crs.updateString(1, "1111");
    crs.updateString(2, "alex");
    crs.insertRow();

    crs.moveToInsertRow();
    crs.updateString(1, "2222");
    crs.updateString(2, "jane");
    crs.insertRow();

    // if you want to commit changes from a CachedRowSet
    // object to your desired datasource, then you must
    // create a Connection object.
    //
    //conn = getHSQLConnection();

    // moves the cursor to the remembered cursor position, usually
    // the current row. This method has no effect if the cursor is
    // not on the insert row.
    crs.moveToCurrentRow();

    // when the method acceptChanges() is executed, the CachedRowSet
    // object's writer, a RowSetWriterImpl object, is called behind the
    // scenes to write the changes made to the rowset to the underlying
    // data source. The writer is implemented to make a connection to
    // the data source and write updates to it.
    crs.acceptChanges(conn);
    conn.close();
}

From source file:Main.java

/**
 * Sets the optional string./* w  w w.  java  2 s.com*/
 *
 * @param statement the statement
 * @param string the string
 * @param optionIndex the option index
 * @return the prepared statement
 * @throws SQLException the sQL exception
 */
static PreparedStatement setOptionalString(PreparedStatement statement, String string, int optionIndex)
        throws SQLException {
    if (string != null)
        statement.setString(optionIndex, string);
    else
        statement.setNull(optionIndex, Types.VARCHAR);

    return statement;
}

From source file:Main.java

public static PreparedStatement createFieldsInsert(Connection conn, int layerId, String name,
        String description, String fieldId, String fieldType, String sid, String sname, String sdesc,
        boolean indb, boolean enabled, boolean namesearch, boolean defaultlayer, boolean intersect,
        boolean layerbranch, boolean analysis, boolean addToMap) throws SQLException {
    // TOOD slightly different statement if sdesc is null...

    PreparedStatement stFieldsInsert = conn.prepareStatement(
            "INSERT INTO fields (name, id, \"desc\", type, spid, sid, sname, sdesc, indb, enabled, last_update, namesearch, defaultlayer, \"intersect\", layerbranch, analysis, addtomap)"
                    + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
    stFieldsInsert.setString(1, name);/*from w w  w  .  ja va2s .co m*/
    stFieldsInsert.setString(2, fieldId);
    stFieldsInsert.setString(3, description);
    stFieldsInsert.setString(4, fieldType);
    stFieldsInsert.setString(5, Integer.toString(layerId));
    stFieldsInsert.setString(6, sid);
    stFieldsInsert.setString(7, sname);

    if (sdesc == null || sdesc.isEmpty()) {
        stFieldsInsert.setNull(8, Types.VARCHAR);
    } else {
        stFieldsInsert.setString(8, sdesc);
    }

    stFieldsInsert.setBoolean(9, indb);
    stFieldsInsert.setBoolean(10, enabled);
    stFieldsInsert.setTimestamp(11, new Timestamp(System.currentTimeMillis()));
    stFieldsInsert.setBoolean(12, namesearch);
    stFieldsInsert.setBoolean(13, defaultlayer);
    stFieldsInsert.setBoolean(14, intersect);
    stFieldsInsert.setBoolean(15, layerbranch);
    stFieldsInsert.setBoolean(16, analysis);
    stFieldsInsert.setBoolean(17, addToMap);

    return stFieldsInsert;
}