List of usage examples for java.sql Connection prepareCall
CallableStatement prepareCall(String sql) throws SQLException;
CallableStatement
object for calling database stored procedures. 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 va2 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 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 ww . j a 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 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 { String URL = "jdbc:microsoft:sqlserver://yourServer:1433;databasename=pubs"; String userName = "yourUser"; String password = "yourPassword"; Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); Connection con = DriverManager.getConnection(URL, userName, password); CallableStatement callstmt = con .prepareCall("INSERT INTO myIdentTable (col2) VALUES (?);SELECT @@IDENTITY"); callstmt.setString(1, "testInputBatch"); callstmt.execute();/*from w w w.j a v a 2 s. c o m*/ int iUpdCount = callstmt.getUpdateCount(); boolean bMoreResults = true; ResultSet rs = null; int myIdentVal = -1; // to store the @@IDENTITY while (bMoreResults || iUpdCount != -1) { rs = callstmt.getResultSet(); if (rs != null) { rs.next(); myIdentVal = rs.getInt(1); } bMoreResults = callstmt.getMoreResults(); iUpdCount = callstmt.getUpdateCount(); } callstmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); String simpleProc = "{ call simpleproc(?) }"; CallableStatement cs = conn.prepareCall(simpleProc); cs.registerOutParameter(1, java.sql.Types.INTEGER); cs.execute();/*from w w w . j a v a 2 s . c om*/ int param1 = cs.getInt(1); System.out.println("param1=" + param1); ParameterMetaData pmeta = cs.getParameterMetaData(); if (pmeta == null) { System.out.println("Vendor does not support ParameterMetaData"); } else { System.out.println(pmeta.getParameterType(1)); } 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); // Call a procedure with no parameters CallableStatement cs = connection.prepareCall("{call myprocin(?)}"); // Set the value for the IN parameter cs.setString(1, "a string"); cs.execute(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID", "PASSWORD"); CallableStatement proc_stmt = con.prepareCall("{ call generateID(?) }"); proc_stmt.setString(1, "employee"); ResultSet rs = proc_stmt.executeQuery(); if (rs.next()) { int employeeId = rs.getInt(1); System.out.println("Generated employeeId: " + employeeId); } else {//ww w.j a v a2s . com System.out.println("Stored procedure couldn't generate new Id"); } }
From source file:Test.java
public static void main(String[] args) throws Exception { Connection conn = DriverManager.getConnection("...", "username", "password"); String query = "{CALL GETDATE(?,?)}"; CallableStatement callableStatement = (CallableStatement) conn.prepareCall(query); callableStatement.setInt(1, 123);//from w w w .jav a 2s. c o m callableStatement.registerOutParameter(1, Types.DATE); callableStatement.executeQuery(); Date date = callableStatement.getObject(2, Date.class); }
From source file:CallableStmt.java
public static void main(String args[]) throws Exception { String storedProc = "create procedure SHOW_ORDERS_BY_STATE @State CHAR (2) as " + "select c.Last_Name+', '+c.First_Name AS Name,o.Order_Number " + "from CUSTOMERS c, ORDERS o where c.Customer_Number = o.Customer_Number " + "AND c.State = @State order by c.Last_Name;"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Customers"); Statement stmt = con.createStatement(); stmt.executeUpdate(storedProc);//from w w w .j av a2 s. c o m CallableStatement cs = con.prepareCall("{call SHOW_ORDERS_BY_STATE(?)}"); cs.setString(1, "NJ"); ResultSet rs = cs.executeQuery(); while (rs.next()) { String name = rs.getString("Name"); int orderNo = rs.getInt("Order_Number"); System.out.println(name + ": " + orderNo); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); // Step-2: identify the stored procedure String simpleProc = "{ call simpleproc(?) }"; // Step-3: prepare the callable statement CallableStatement cs = conn.prepareCall(simpleProc); // Step-4: register output parameters ... cs.registerOutParameter(1, java.sql.Types.INTEGER); // Step-5: execute the stored procedures: proc3 cs.execute();// w ww .j a va2s. c om // Step-6: extract the output parameters int param1 = cs.getInt(1); System.out.println("param1=" + param1); // Step-7: get ParameterMetaData ParameterMetaData pmeta = cs.getParameterMetaData(); if (pmeta == null) { System.out.println("Vendor does not support ParameterMetaData"); } else { System.out.println(pmeta.getParameterType(1)); } conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName(DB_DRIVER);/* w ww . j ava 2 s .c om*/ Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); CallableStatement callableStatement = null; String getPERSONByUserIdSql = "{call getPERSONByUserId(?,?,?,?)}"; callableStatement = dbConnection.prepareCall(getPERSONByUserIdSql); callableStatement.setInt(1, 10); callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR); callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR); callableStatement.registerOutParameter(4, java.sql.Types.DATE); callableStatement.executeUpdate(); String userName = callableStatement.getString(2); String createdBy = callableStatement.getString(3); Date createdDate = callableStatement.getDate(4); System.out.println("UserName : " + userName); System.out.println("CreatedBy : " + createdBy); System.out.println("CreatedDate : " + createdDate); callableStatement.close(); dbConnection.close(); }