Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mport java.sql.*;
- public class Main {
- public static void main(String[] args) {
- String sql = "{? = call fn_test()}";
- try (Connection conn = getConnection(); CallableStatement callableStatement = conn.prepareCall(sql);
- PreparedStatement preparedStatement = conn.prepareStatement("select fn_test()")) {
- // Execute stored procedured CallableStatement (recommend way)
- callableStatement.registerOutParameter(1, Types.VARCHAR);
- callableStatement.execute();
- System.out.println("Response: " + callableStatement.getString(1));
- // Execute stored procedured using PreparedStatement
- ResultSet rs = preparedStatement.executeQuery();
- // ResultSet rs = preparedStatement.getResultSet();
- rs.next();
- System.out.println("Response: " + rs.getString(1));
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- System.err.println("Error al conectarse: " + e.getSQLState() + "-" + e.getMessage());
- } catch (NullPointerException e) {
- e.printStackTrace();
- System.err.println("Error al conectarse: " + e.getMessage());
- }
- }
- static Connection getConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:postgresql://localhost/dbtest", "juan", "12345");
- }
- }
Add Comment
Please, Sign In to add comment