Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package servermysql;
  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.sql.Statement;
  9. import javax.swing.JComboBox;
  10. /**
  11. *
  12. * @author Enrique
  13. */
  14. public class Conexion {
  15.  
  16. public static Connection Conexion() {
  17. Connection conexion = null;
  18. //VARIABLES DE CONEXION
  19. String servidor = "";
  20. String database = "";
  21. String usuario = "";
  22. String password = "";
  23. String url = "";
  24. System.out.println("---------------------------------------------------------");
  25. try {
  26. //Endica al interprete de Java que en tiempo real mande a cargar la libreria jdbc
  27. Class.forName("com.mysql.jdbc.Driver");
  28. //cadena con datos de servidor
  29. url = "jdbc:mysql://" + servidor + "/" + database;
  30. //variable de conexion obteniendo el servidor , usuario y password
  31. conexion = DriverManager.getConnection(url, usuario, password);
  32. System.out.println(" Conexion a Base de Datos: " + url + " --> Ok ");
  33. } //mandamos mensaje de error por url o error de conexion
  34. catch (SQLException ex) {
  35. System.out.println("Error Cadena: " + ex);
  36. } catch (ClassNotFoundException ex) {
  37. System.out.println("Error Clase Conexion: " + ex);
  38. }
  39. return conexion;
  40. }
  41. public static JComboBox CmbFull(JComboBox selector, String query) {
  42. selector.removeAll();
  43. selector.addItem("Selecciona...");
  44. try {
  45. Connection c1 = Conexion.Conexion();
  46. if (c1 != null) {
  47. PreparedStatement p1 = c1.prepareStatement(query);
  48. ResultSet r1 = p1.executeQuery();
  49. if (r1.next()) {
  50. do {
  51. selector.addItem(r1.getString(1));
  52. } while (r1.next());
  53. return selector;
  54. } else {
  55. return null;
  56. }
  57. }
  58. }catch(Exception er){
  59. System.out.println("Error:"+er);
  60. }
  61. return null;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement