Guest User

Untitled

a guest
Feb 15th, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. package dbConnection;
  2.  
  3. import java.sql.*;
  4. import java.text.DateFormat;
  5. import java.text.SimpleDateFormat;
  6.  
  7. import org.example.www.serviciopractica1.GenerarPresupuesto;
  8.  
  9. public class DBConnect {
  10. private Connection con;
  11. private Statement st;
  12. private ResultSet result;
  13.  
  14. public ResultSet getResult() {
  15. return result;
  16. }
  17.  
  18. public DBConnect() {
  19. String url = "jdbc:mysql://localhost:3306/mtis";
  20. String user = "root";
  21. String password = "";
  22.  
  23. try{
  24. Class.forName("com.mysql.jdbc.Driver");
  25.  
  26. con = DriverManager.getConnection(url, user, password);
  27. st = con.createStatement();
  28. } catch(Exception e) {
  29. System.out.println("Error: " + e);
  30. }
  31. }
  32.  
  33. public void getCodigoPostal(String cp) {
  34. try {
  35. String query = "select * from codigospostales where codigoPostal='" + cp + "';";
  36. result = st.executeQuery(query);
  37. } catch(Exception e) {
  38. System.out.println("Error: " + e);
  39. }
  40. }
  41.  
  42. public boolean checkSoapKey(String soapKey) throws SQLException{
  43. boolean valido = false;
  44.  
  45. try {
  46. String query = "select * from soapkey where soapKey='" + soapKey + "';";
  47. result = st.executeQuery(query);
  48. } catch(Exception e) {
  49. System.out.println("Error: " + e);
  50. }
  51.  
  52. if(result.next())
  53. valido = true;
  54.  
  55. return valido;
  56. }
  57.  
  58. public boolean savePresupuesto(GenerarPresupuesto presupuesto) throws SQLException {
  59. boolean valido = true;
  60. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  61.  
  62. String fecha = df.format(presupuesto.getInfoInicial().getFechaPresupuesto());
  63.  
  64. try {
  65. String query = "insert into presupuestos (fechaPresupuesto, idCliente, referenciaProducto, cantidadProducto)"
  66. + " values ('" + fecha + "', "
  67. + presupuesto.getInfoInicial().getIdCliente() + ", '" + presupuesto.getInfoInicial().getReferenciaProducto() + "', "
  68. + presupuesto.getInfoInicial().getCantidadProducto() + ");";
  69. st.executeUpdate(query);
  70. } catch(Exception e) {
  71. System.out.println("Error: " + e);
  72. valido = false;
  73. }
  74.  
  75. return valido;
  76. }
  77.  
  78. public void getPresupuestos() {
  79. try {
  80. String query = "select * from presupuestos;";
  81. result = st.executeQuery(query);
  82. } catch(Exception e) {
  83. System.out.println("Error: " + e);
  84. }
  85. }
  86. }
Add Comment
Please, Sign In to add comment