Guest User

Untitled

a guest
Nov 22nd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. package models;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class MySQLConnection {
  8.  
  9. //mysql parameters
  10. private static final String SERVER = "localhost";
  11. private static final String DATABASE = "logistics";
  12. private static final String USER = "root";
  13. private static final String PASSWORD = "";
  14.  
  15. //get connection
  16. public static Connection getConnection() {
  17. //connection
  18. Connection connection = null;
  19. try {
  20. //connection string
  21. String connectionString = "jdbc:mysql://" + SERVER + "/" + DATABASE + "?user=" + USER + "&serverTimezone=UTC";
  22. //add password to connection string if needed
  23. if (PASSWORD != "") {
  24. connectionString += "&password=" + PASSWORD;
  25. }
  26. //MySQL Java Drivers
  27. //Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  28. Class.forName("com.mysql.jdbc.Driver").newInstance();
  29. //MySQL connection
  30. connection = DriverManager.getConnection(connectionString);
  31.  
  32. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) {
  33. System.out.println(ex.getMessage());
  34.  
  35. }
  36. return connection;
  37. }
  38.  
  39. public static ArrayList<TrailerBox> getAll() {
  40. ArrayList<TrailerBox> list = new ArrayList<>();
  41. PreparedStatement command;
  42. ResultSet result;
  43. String query = "select s.id, s.description, s.ipAddress from trailerboxes as s order by s.id";
  44. try {
  45. //prepare statement
  46. command = MySQLConnection.getConnection().prepareStatement(query);
  47. //execute query
  48. result = command.executeQuery();
  49. //read rows
  50. while (result.next()) {
  51. //read fields
  52. int id = result.getInt("id");
  53. String description = result.getString("description");
  54. String ipAddress = result.getString("ipAddress");
  55. //add new order to list
  56. list.add(new TrailerBox(id, description, ipAddress));
  57. }
  58. } catch (SQLException ex) {
  59. System.out.println(ex.getMessage());
  60. }
  61. //return list
  62. return list;
  63. }
  64.  
  65. } finally {
  66. if (connection != null) {
  67. try {
  68. connection.close();
  69. } catch (SQLException ignore) {
  70. }
  71. }
  72. }
  73.  
  74. package Genericos;
  75.  
  76. import Preferencias.Preferencias;
  77. import java.sql.Connection;
  78. import java.sql.DriverManager;
  79. import java.sql.SQLException;
  80. import javax.swing.JOptionPane;
  81.  
  82. public class ConexionDB {
  83. private static ConexionDB INSTANCE = null;
  84. private static Connection dbcon;
  85. private String host;
  86. private String db;
  87. private String user;
  88. private String pass;
  89.  
  90. private static String msg;
  91.  
  92. public static enum TR {INICIAR, FINALIZAR, CANCELAR};
  93.  
  94. private ConexionDB() {
  95. if (INSTANCE == null) {
  96. if(procesar() == false){
  97. System.out.println(msg);
  98. }
  99. }
  100. }
  101.  
  102. public static Connection getDBcon() {
  103. return dbcon;
  104. }
  105.  
  106. public static ConexionDB getInstancia() {
  107. if (INSTANCE == null) {
  108. INSTANCE = new ConexionDB();
  109. }
  110. return INSTANCE;
  111. }
  112.  
  113. public String getMsg() {
  114. return msg;
  115. }
  116.  
  117. private boolean procesar(){
  118. if(datosTxt() == false){
  119. host = Preferencias.GET_IP();
  120. pass = Preferencias.GET_BD_PASS();
  121. db = Preferencias.GET_DB_NAME();
  122. user = Preferencias.GET_BD_USER();
  123. }
  124. return conectar();
  125. }
  126.  
  127. private boolean datosTxt(){
  128. return false;
  129. }
  130.  
  131. private boolean conectar(){
  132. String sCon = "jdbc:mysql://"+ host +"/"+ db;
  133. try{
  134. Class.forName("com.mysql.jdbc.Driver");
  135.  
  136. dbcon = DriverManager.getConnection(sCon, user, pass);
  137. System.err.println("Parametros de Conexion....");
  138. System.err.println("host:"+host);
  139. System.err.println("db:"+db);
  140. System.err.println("user:"+user);
  141. System.err.println("pass:"+pass);
  142. if (dbcon != null) {
  143. msg = "Se conectó correctamente a la base de datos.";
  144. System.out.println(msg);
  145. return true;
  146. }else{
  147. msg = "Error al conectar a la base de datos.";
  148.  
  149. JOptionPane.showMessageDialog(null,"Error al conectarse a la base de datos");
  150.  
  151. return false;
  152. }
  153. } catch(ClassNotFoundException | SQLException e){
  154. msg = "Error driver! "+ e.getMessage();
  155. JOptionPane.showMessageDialog(null,"Error Al conectarse con el Sevidor:"+e.getMessage());
  156. return false;
  157. }
  158. }
  159.  
  160. public static void cerrar(){
  161. try{
  162. dbcon.close();
  163. }catch(SQLException e){
  164. JOptionPane.showMessageDialog(null,"ERROR AL TERMINAR LA CONEXION");
  165. }
  166. }
  167.  
  168. public static void Transaccion(TR accion){
  169. try {
  170. switch(accion){
  171. case INICIAR:
  172. dbcon.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  173. dbcon.setAutoCommit(false);
  174. break;
  175. case FINALIZAR:
  176. dbcon.commit();
  177. dbcon.setAutoCommit(true);
  178. break;
  179. case CANCELAR:
  180. dbcon.rollback();
  181. dbcon.setAutoCommit(true);
  182. break;
  183. }
  184. } catch (SQLException ex) {
  185. msg = "Error al establecer estado de transacciones.";
  186. JOptionPane.showMessageDialog(null,"Error al establecer estado de transacciones.:"+ex.getMessage());
  187.  
  188. }
  189. }
  190.  
  191. }
  192.  
  193. public class AccesoCTR {
  194. private final Acceso acceso;
  195. private PreparedStatement pst;
  196. private ResultSet rst;
  197. private String Sql;
  198.  
  199. public AccesoCTR(Acceso accesovm) {
  200. /*Abrimos la conexion en el Contructor de la clase que necesitamos usar*/
  201. ConexionDB.getInstancia();
  202. }
  203.  
  204. /*Ejemplo de Select con la clase*/
  205.  
  206. public boolean ValidarUsuarios(){
  207. try {
  208.  
  209. Sql ="SELECT cod_usuario, usuario, cod_perfil,cod_funcionario FROM usuarios
  210. n" +
  211. "WHERE estado and usuario=? and clave=?";
  212. pst=ConexionDB.getDBcon().prepareStatement(Sql);
  213. pst.setString(1, acceso.txtusuario.getText().trim());
  214. pst.setString(2, acceso.txtclave.getText().trim());
  215. rst=pst.executeQuery();
  216. if (rst.next()) {
  217. Principal.permiso=rst.getInt("cod_perfil");
  218. VentaCTR.funcionario=rst.getInt("cod_funcionario");
  219.  
  220. return true;
  221. }
  222. } catch (SQLException ex) {
  223. JOptionPane.showMessageDialog(null,"Error Al iniciar Sesion " +ex);
  224. return false;
  225. }
  226.  
  227. return false;
  228. }
Add Comment
Please, Sign In to add comment