Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package com;
  2.  
  3. import java.sql.*;
  4.  
  5. public class TestSProc implements Runnable {
  6.  
  7. private final String DB_URL = "jdbc:mysql://localhost:3306/test";
  8. private final String DB_USER = "root";
  9. private final String DB_PASS = "root";
  10.  
  11. public static void main(String[] args) {
  12. new TestSProc().run();
  13. }
  14.  
  15. public void run() {
  16. Connection connection = null;
  17. try {
  18. connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
  19. CallableStatement cStatement = connection.prepareCall("{CALL addUser(?, ?, ?)}");
  20.  
  21. cStatement.setLong(1, 7);
  22. cStatement.setString(2, "Maria");
  23. cStatement.setString(3, "Semenova");
  24. cStatement.execute();
  25.  
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. } finally {
  29. try {
  30. connection.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37.  
  38. =====================================================
  39.  
  40. CREATE DEFINER=`root`@`localhost` PROCEDURE `addUser`(IN `iId` INT, IN `fName` VARCHAR(50), IN `lName` VARCHAR(50))
  41. LANGUAGE SQL
  42. NOT DETERMINISTIC
  43. CONTAINS SQL
  44. SQL SECURITY DEFINER
  45. COMMENT ''
  46. BEGIN
  47. INSERT INTO users (id, firstName, lastName) values (iId, fName, lName);
  48. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement