Advertisement
Guest User

Untitled

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