Example usage for java.sql Connection prepareCall

List of usage examples for java.sql Connection prepareCall

Introduction

In this page you can find the example usage for java.sql Connection prepareCall.

Prototype

CallableStatement prepareCall(String sql) throws SQLException;

Source Link

Document

Creates a CallableStatement object for calling database stored procedures.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection conn = null;
    String query = "begin proc(?,?,?); end;";
    CallableStatement cs = conn.prepareCall(query);
    cs.setString(1, "string parameter");
    cs.setInt(2, 1);//from w w  w  .j  av a  2 s . com
    cs.registerOutParameter(2, Types.INTEGER);
    cs.registerOutParameter(3, Types.INTEGER);
    cs.execute();

    int parm2 = cs.getInt(2); // get the result from OUTPUT #2
    int parm3 = cs.getInt(3); // get the result from OUTPUT #3
}

From source file:CallableStmt.java

    public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    CallableStatement cstmt = conn.prepareCall("{call getEmpName (?,?)}");
    cstmt.setInt(1, 111111111);/*from  www .j  ava  2 s . c o m*/
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();
    String empName = cstmt.getString(2);
    System.out.println(empName);
    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 va  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 myproc}");
    cs.execute();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    CallableStatement stmt = conn.prepareCall("{call CREATE_USERS (?, ?, ?, ?, ?, ?)}");

    stmt.setString(1, "a");
    stmt.setString(2, "b");
    stmt.setString(3, "c");
    stmt.setString(4, "d");
    stmt.setString(5, "e");
    stmt.setString(6, "f");
    stmt.execute();//www.  ja  v  a 2s  . c o  m
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    CallableStatement cstmt = conn.prepareCall("{call getEmpName (?,?)}");
    cstmt.setInt(1, 111111111);/*from   w w w  . j  a  va 2 s.  c om*/
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();
    String empName = cstmt.getString(2);
    System.out.println(empName);
    conn.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);// w ww.j a  v a2 s .  c o m

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    CallableStatement cs = connection.prepareCall("{? = call myfuncinout(?)}");

    Statement stmt = connection.createStatement();

    // Create procedure myproc with no parameters
    String procedure = "CREATE OR REPLACE PROCEDURE myproc IS " + "BEGIN "
            + "INSERT INTO oracle_table VALUES('string 1'); " + "END;";
    stmt.executeUpdate(procedure);
}

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 av  a  2  s  .  co  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  w w  .  j  a  va2s  . 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 myfunc}");

    // Register the type of the return value
    int intValue = 0;
    cs.registerOutParameter(1, intValue);

    // 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);/* w  ww . ja v  a  2  s  . co  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);//from  w  ww  . jav a 2s.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);
    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);

}