necesito poder conectar esto a mysql y no logro entender como se hace y lo k debo cambiar.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Conectar extends JInternalFrame implements ActionListener {
static final long serialVersionUID = 2444868053607556698L;
EjemploJDBCFrame framePadre;
JTextField txtServidor, txtUsuario;
JPasswordField txtPassword;
JButton btnConectar, btnCancelar;
public Conectar(EjemploJDBCFrame frame) {
super("Conectar al servidor", false, true, false, false);
framePadre = frame;
setDefaultCloseOperation(WindowConstants.HIDE_ON_C LOSE);
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
GridBagConstraints constraints = new GridBagConstraints();
JPanel pnlDatos = new JPanel(new GridBagLayout());
//Label Servidor
JLabel lab = new JLabel("Servidor:");
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets(2, 2, 2, 2);
constraints.anchor = constraints.LINE_START;
pnlDatos.add(lab, constraints);
//Label Usuario
lab = new JLabel("Usuario:");
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
pnlDatos.add(lab, constraints);
//Label Password
lab = new JLabel("Password:");
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
pnlDatos.add(lab, constraints);
//TextField Servidor
txtServidor = new JTextField(15);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
pnlDatos.add(txtServidor, constraints);
//TextField Usuario
txtUsuario = new JTextField();
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.fill = constraints.HORIZONTAL;
pnlDatos.add(txtUsuario, constraints);
//TextField Password
txtPassword = new JPasswordField();
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.fill = constraints.HORIZONTAL;
pnlDatos.add(txtPassword, constraints);
pnlDatos.setBorder(BorderFactory.createEmptyBorder (5, 5, 5, 5));
//Panel de botones
JPanel pnlBotones = new JPanel(new FlowLayout());
//Botón Conectar
btnConectar = new JButton("Conectar");
btnConectar.setMnemonic(KeyEvent.VK_N);
btnConectar.setActionCommand("conect");
btnConectar.addActionListener(this);
getRootPane().setDefaultButton(btnConectar);
pnlBotones.add(btnConectar);
//Botón Cancelar
btnCancelar = new JButton("Cancelar");
btnCancelar.setMnemonic(KeyEvent.VK_C);
btnCancelar.setActionCommand("cancel");
btnCancelar.addActionListener(this);
pnlBotones.add(btnCancelar);
getContentPane().add(pnlDatos);
getContentPane().add(pnlBotones);
pack();
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand() == "conect") {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String strURL = "jdbc:jtds:sqlserver://" +
txtServidor.getText() + "/Discos" +
";user=" + txtUsuario.getText() +
";password=" + String.copyValueOf(txtPassword.getPassword()) +
";TDS=7.0" +
";namedPipe=true";
Connection conSQL = DriverManager.getConnection(strURL);
framePadre.setConexion(conSQL);
framePadre.setUsuario(txtUsuario.getText());
framePadre.setConectado(true);
txtPassword.setText("");
setVisible(false);
} catch (ClassNotFoundException e) {
JOptionPane.showInternalMessageDialog(this,
"Error: " + e.getLocalizedMessage() + "\n" +
"al cargar el driver JDBC", "Error de JDBC",
JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
JOptionPane.showInternalMessageDialog(this,
"Error: " + e.getMessage() + "\n" +
"Código: " + e.getErrorCode() + "\n" +
"Estado: " + e.getSQLState(),
"Error de conexión",
JOptionPane.ERROR_MESSAGE);
}
} else if (evt.getActionCommand() == "cancel") {
setVisible(false);
}
}
}