Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servermysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JComboBox;
- /**
- *
- * @author Enrique
- */
- public class Conexion {
- public static Connection Conexion() {
- Connection conexion = null;
- //VARIABLES DE CONEXION
- String servidor = "";
- String database = "";
- String usuario = "";
- String password = "";
- String url = "";
- System.out.println("---------------------------------------------------------");
- try {
- //Endica al interprete de Java que en tiempo real mande a cargar la libreria jdbc
- Class.forName("com.mysql.jdbc.Driver");
- //cadena con datos de servidor
- url = "jdbc:mysql://" + servidor + "/" + database;
- //variable de conexion obteniendo el servidor , usuario y password
- conexion = DriverManager.getConnection(url, usuario, password);
- System.out.println(" Conexion a Base de Datos: " + url + " --> Ok ");
- } //mandamos mensaje de error por url o error de conexion
- catch (SQLException ex) {
- System.out.println("Error Cadena: " + ex);
- } catch (ClassNotFoundException ex) {
- System.out.println("Error Clase Conexion: " + ex);
- }
- return conexion;
- }
- public static JComboBox CmbFull(JComboBox selector, String query) {
- selector.removeAll();
- selector.addItem("Selecciona...");
- try {
- Connection c1 = Conexion.Conexion();
- if (c1 != null) {
- PreparedStatement p1 = c1.prepareStatement(query);
- ResultSet r1 = p1.executeQuery();
- if (r1.next()) {
- do {
- selector.addItem(r1.getString(1));
- } while (r1.next());
- return selector;
- } else {
- return null;
- }
- }
- }catch(Exception er){
- System.out.println("Error:"+er);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement