Guest User

Untitled

a guest
Dec 26th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package CalculadorDeExpressões.Sistema;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.script.ScriptEngine;
  13. import javax.script.ScriptEngineManager;
  14. import javax.script.ScriptException;
  15.  
  16. public class ProjetoSD {
  17.  
  18. static double res;
  19. static int id ;
  20.  
  21. public ProjetoSD() {
  22. }
  23. public static Connection con = null;
  24.  
  25. public static void Conectar() {
  26. System.out.println("Conectando ao banco...");
  27. try {
  28. Class.forName("com.mysql.jdbc.Driver");
  29. con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/sist_dist", "root", "123456");
  30. System.out.println("Conectado.");
  31. } catch (ClassNotFoundException ex) {
  32. System.out.println("Classe não encontrada, adicione o driver nas bibliotecas.");
  33. Logger.getLogger(ProjetoSD.class.getName()).log(Level.SEVERE, null, ex);
  34. } catch (SQLException erro) {
  35. System.out.println("Erro: " + erro);
  36. throw new RuntimeException(erro);
  37. }
  38. }
  39.  
  40. public String retiraExpr() {
  41. Conectar();
  42.  
  43. String nome = "David e Tergina";
  44. String expr = null;
  45. List<String> x = new ArrayList<>();
  46. try {
  47. PreparedStatement ps = con.prepareStatement("{call retira_exp(?)}");
  48. ps.setString(1, nome);
  49. ResultSet rs = ps.executeQuery();
  50.  
  51. while (rs.next()) {
  52. x.add(rs.getString(2));
  53. id = Integer.parseInt(rs.getString(1));
  54.  
  55. }
  56. // for (String exp : x) {
  57. // }
  58. } catch (SQLException e) {
  59. System.out.println("Erro: " + e);
  60. }
  61. expr = x.get(0);
  62.  
  63.  
  64. return expr;
  65.  
  66. }
  67.  
  68.  
  69. public static double Calc() throws ScriptException {
  70. ProjetoSD obj = new ProjetoSD();
  71. String expr = obj.retiraExpr();
  72. System.out.println("Expressão Retirada: " + expr);
  73.  
  74. //double a = Double.parseDouble(expr);
  75. ScriptEngineManager factory = new ScriptEngineManager();
  76. ScriptEngine engine = factory.getEngineByName("JavaScript");
  77.  
  78. double result = Double.parseDouble(engine.eval(expr).toString());
  79. String sql = "CALL entrega_res(' " + 1 + " ',' " + result + "'); ";
  80.  
  81.  
  82.  
  83.  
  84.  
  85. return result;
  86. }
  87.  
  88. public void Resultado() throws ScriptException {
  89.  
  90. Double result = Calc();
  91. System.out.println("Resultado Devolvido: " + result);
  92. try {
  93. PreparedStatement pstm = con.prepareStatement("{call entrega_res(?,?)}");
  94. pstm.setInt(1, id);
  95. pstm.setDouble(2, result);
  96. pstm.executeUpdate();
  97.  
  98. } catch (SQLException e) {
  99. System.out.println("Erro: " + e);
  100.  
  101. {
  102. try {
  103. PreparedStatement dead = con.prepareStatement("{call libera_deadlocks(?)}");
  104. } catch (SQLException ex) {
  105. Logger.getLogger(ProjetoSD.class.getName()).log(Level.SEVERE, null, ex);
  106. }
  107. }
  108. }
  109. }
  110.  
  111. public static void main(String[] args) throws ScriptException {
  112. //SD.Conectar();
  113.  
  114. ProjetoSD sd = new ProjetoSD();
  115. int contador = 0;
  116. while (contador < 1) {
  117. sd.Resultado();
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124. }
  125.  
  126. }
Add Comment
Please, Sign In to add comment